Python中如何通过句柄(handle)发送和接收消息?为什么样例取得的handle不一样?
如题,想通过 handle 让别程序关闭我编译为 exe 的程序, 原理是通过向 handle 发送关闭信号,程序检测到,就执行相关代码,顺便想看看一个程序是如何通过句柄控制其他程序的,暂时不使用其他进程间通讯技术 hinst = win32gui.GetModuleHandle(None)
hWindow = pywintypes.HANDLE()
这两个获取的句柄不一样,使用 spyxx 获取句柄后,手动发送信号也没反应,不知道是什么原因,在此请教,不胜感激。 代码如下
# -*- coding: utf-8 -*-
import time,pywintypes
import threading
from ctypes import *
from ctypes.wintypes import *
import win32con,win32gui
user32 = windll.user32
class ListenThread(threading.Thread):
def init(self):
threading.Thread.init(self)
def run(self):
#保存线程 ID
# self.ident = threading._get_ident()
msg = MSG()
#创建线程消息队列
user32.PeekMessageW(byref(msg), None, win32con.WM_USER,win32con.WM_USER, win32con.PM_NOREMOVE)
self.hinst = win32gui.GetModuleHandle(None)
print(self.hinst)
hWindow = pywintypes.HANDLE()
print("hWindow:", hWindow)
#消息循环
while True:
user32.GetMessageW(byref(msg), None, 0, 0)
if msg.message == win32con.WM_QUIT:
print ('thread quit' )
break
elif msg.message == win32con.WM_USER + 779:
print( 'I get it' )
else:
print("ddd")
time.sleep(1)
user32.TranslateMessage(byref(msg))
user32.DispatchMessageW(byref(msg))
print (‘create thread’ )
hThread = ListenThread()
print (‘start thread’)
hThread.start()
for i in range(5):
time.sleep(1)
user32.PostMessageW(hThread.hinst, win32con.WM_USER + 779, 0, 0)
print (“ddddd”)
else:
user32.PostThreadMessageW(hThread.hinst, win32con.WM_QUIT, 0, 0)
怕代码乱码在下面网站也贴了代码,内容一样的 https://paste.ubuntu.com/p/wNtwywrYRF/
Python中如何通过句柄(handle)发送和接收消息?为什么样例取得的handle不一样?
在Windows上通过句柄(handle)发送和接收消息,主要用user32.dll的SendMessageW和FindWindowW。你遇到的handle不一致问题,通常是因为查找窗口的条件不对,或者窗口有多个实例。
下面是一个完整示例,演示如何找到记事本窗口并发送消息:
import ctypes
from ctypes import wintypes
# 加载user32.dll
user32 = ctypes.windll.user32
# 定义SendMessageW函数原型
SendMessageW = user32.SendMessageW
SendMessageW.argtypes = [wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM]
SendMessageW.restype = wintypes.LPARAM
# 定义FindWindowW函数原型
FindWindowW = user32.FindWindowW
FindWindowW.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR]
FindWindowW.restype = wintypes.HWND
# 查找记事本窗口 - 通过类名
notepad_class = "Notepad"
hwnd = FindWindowW(notepad_class, None)
if hwnd:
print(f"找到记事本窗口,句柄: {hwnd}")
# 发送WM_CLOSE消息(0x0010)关闭窗口
# wParam和lParam都为0
SendMessageW(hwnd, 0x0010, 0, 0)
print("已发送关闭消息")
else:
print("未找到记事本窗口")
为什么handle会不一样?
-
查找条件不精确:
FindWindowW需要准确的类名或窗口标题。如果窗口标题变化(比如记事本打开不同文件),用标题查找就会失败。 -
多个窗口实例:如果有多个同类窗口(比如多个记事本),
FindWindowW只返回第一个匹配的。每次运行找到的可能是不同实例。 -
窗口状态变化:窗口在查找后可能被关闭/重建,导致句柄失效。
-
32/64位差异:在64位系统上,32位和64位进程看到的句柄值可能不同。
改进方案:
- 用
EnumWindows遍历所有窗口,根据多个条件筛选 - 使用
GetWindowText和GetClassName验证窗口属性 - 考虑用进程ID(PID)进行更精确的匹配
总结建议:确保使用准确的窗口类名和标题进行查找,或改用EnumWindows进行遍历匹配。
emmmmm win32gui 都快忘得差不多了
getmodulehandle 获取的是进程实例句柄,c++ api 里的类型是 HINSTANCE
然后,postmessage 的目标应该是一个窗口句柄,类型是 HWND,消息循环也必须有一个窗口才能工作
我不太清楚 python 里封装成啥样,但反正给 instance postmessage 肯定是没用的

