欢迎访问宙启技术站
智能推送

使用Python的ctypes.windll模块进行WindowsAPI调用的示例

发布时间:2023-12-28 01:01:27

ctypes是Python的一个标准库,用于调用C/C++编写的动态链接库(DLL)。在Windows中,有很多系统API可以通过ctypes.windll模块来调用。下面是一个使用ctypes.windll模块进行WindowsAPI调用的示例,包括创建进程、获取窗口标题和发送消息。

import ctypes

# 调用CreateProcessA函数创建一个新的进程
def create_process(exe_path):
    kernel32 = ctypes.windll.kernel32
    process_information = PROCESS_INFORMATION()
    startup_info = STARTUPINFO()
    startup_info.cb = ctypes.sizeof(startup_info)
    
    success = kernel32.CreateProcessA(
        None,
        exe_path,
        None,
        None,
        False,
        0,
        None,
        None,
        ctypes.byref(startup_info),
        ctypes.byref(process_information)
    )
    
    if success == 0:
        print("创建进程失败")
        return
    
    print("进程ID: ", process_information.dwProcessId)
    print("线程ID: ", process_information.dwThreadId)
    print("进程句柄: ", process_information.hProcess)
    print("线程句柄: ", process_information.hThread)

# 调用GetWindowTextA函数获取窗口标题
def get_window_text(hwnd):
    user32 = ctypes.windll.user32
    buffer_size = 512
    buffer = ctypes.create_string_buffer(buffer_size)
    
    length = user32.GetWindowTextA(hwnd, buffer, buffer_size)
    
    if length == 0:
        print("获取窗口标题失败")
        return ""
    
    return buffer.value.decode()

# 调用SendMessageA函数发送消息
def send_message(hwnd, message):
    user32 = ctypes.windll.user32
    result = user32.SendMessageA(hwnd, WM_SETTEXT, 0, message.encode())
    
    if result == 0:
        print("发送消息失败")
        return False
    
    return True

# 定义PROCESS_INFORMATION和STARTUPINFO结构体
class PROCESS_INFORMATION(ctypes.Structure):
    _fields_ = [("hProcess", ctypes.c_void_p),
                ("hThread", ctypes.c_void_p),
                ("dwProcessId", ctypes.c_ulong),
                ("dwThreadId", ctypes.c_ulong)]

class STARTUPINFO(ctypes.Structure):
    _fields_ = [("cb", ctypes.c_ulong),
                ("lpReserved", ctypes.c_char_p),
                ("lpDesktop", ctypes.c_char_p),
                ("lpTitle", ctypes.c_char_p),
                ("dwX", ctypes.c_ulong),
                ("dwY", ctypes.c_ulong),
                ("dwXSize", ctypes.c_ulong),
                ("dwYSize", ctypes.c_ulong),
                ("dwXCountChars", ctypes.c_ulong),
                ("dwYCountChars", ctypes.c_ulong),
                ("dwFillAttribute", ctypes.c_ulong),
                ("dwFlags", ctypes.c_ulong),
                ("wShowWindow", ctypes.c_ushort),
                ("cbReserved2", ctypes.c_ushort),
                ("lpReserved2", ctypes.c_char_p),
                ("hStdInput", ctypes.c_void_p),
                ("hStdOutput", ctypes.c_void_p),
                ("hStdError", ctypes.c_void_p)]

# 测试函数
if __name__ == '__main__':
    exe_path = "C:\Windows\System32
otepad.exe"
    create_process(exe_path)
    
    hwnd = 0x00000000  # 把hwnd设置为目标窗口的句柄,可以通过其他API获取
    window_title = get_window_text(hwnd)
    print("窗口标题: ", window_title)
    
    message = "Hello World!"
    send_message(hwnd, message)

这个示例演示了如何使用ctypes.windll模块来调用系统函数,包括创建进程、获取窗口标题和发送消息。你可以根据自己的需求修改示例代码,调用其他系统函数来完成其他操作,例如读写文件、播放音频等。