python进程内hook的问题
最近刚学hook,写了个demoimport ctypes
from ctypes.wintypes import DWORD,HHOOK,WPARAM,LPARAM
from tkinter import messagebox
class Hook():
def __init__(self,mode='',func='',callback=None,hook=0):
self.mode = mode
self.callback = callback
self.func = func
self.hook = hook
self.kernel32 = ctypes.windll.kernel32
self.user32 = ctypes.windll.user32
def Create(self):
module = self.kernel32.LoadLibraryW(self.mode)
if module == None:
return False
funcp = self.kernel32.GetProcAddress(module,self.func)
if funcp == None:
return False
HOOKPROC = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_int,ctypes.POINTER(DWORD))
proc = HOOKPROC(self.callback)
hook = self.user32.SetWindowsHookExW(4,proc,module,0)
if not hook:
return False
self.hook = hook
return hook
def Remove(self):
self.user32.UnhookWindowsHookEx(self.hook)
hook = 0
return True
def MyMessageBoxW(nCode,wParam, lParam):
print(nCode,wParam, lParam)
return ctypes.windll.user32.CallNextHookEx(hooker.hook,nCode,wParam, lParam)
hooker = Hook("user32.dll","MessageBoxW",MyMessageBoxW)
print(hooker.Create())
本来看到一些C++都不需要SetHookEX的,但水平太菜,实在无奈
然后运行代码后所有窗口全没动静了,也没法重启,只能强制关机
新窗口也打不开
怀疑是WH_CALLWNDPROC的问题
这里想知道怎么解决,或者用其他方法实现这个需求 感觉还是用C++吧,python这块有些难 def MyMessageBoxW(nCode, wParam, lParam):
print(nCode, wParam, lParam)
# 调用下一个钩子或消息处理函数
return ctypes.windll.user32.CallNextHookEx(hooker.hook, nCode, wParam, lParam)
或者用WH_CBT或WH_GETMESSAGE试试
页:
[1]