python - How to use struct information from mouse/keyboard hook in ctypes -
so i've got c code looks this:
#pragma comment(linker, "/section:.shared,rws") #pragma data_seg(".shared") hmodule hinstance = 0; hhook hkeyboardhook = 0; int lastkey = 0; int keyflags = 0; hhook hmousehook = 0; int mousemsgid = 0; mousehookstruct *mhookpt; mousehookstruct mhookstruct; #pragma data_seg() bool winapi dllmain(handle hmodule, dword dwfunction, lpvoid lpnot) { hinstance = hmodule; return true; } lresult callback keyboardproc(int hookcode, wparam vkeycode, lparam flags) { if(hookcode < 0) { return callnexthookex(hkeyboardhook, hookcode, vkeycode, flags); } if(!hookcode) { lastkey = (int)vkeycode; keyflags = (int)flags; } return callnexthookex(hkeyboardhook, hookcode, vkeycode, flags); } lresult callback mouseproc(int hookcode, wparam msgid, lparam pmousehookstruct) { if(hookcode < 0) { return callnexthookex(hmousehook, hookcode, msgid, pmousehookstruct); } if(!hookcode) { mousemsgid = (int)msgid; mhookpt = (mousehookstruct *)pmousehookstruct; mhookstruct = *mhookpt; } return callnexthookex(hmousehook, hookcode, msgid, pmousehookstruct); } __declspec(dllexport) int getlastkey(void) { if(!lastkey) return 0; return lastkey; } __declspec(dllexport) int getlastflags(void) { if(!keyflags) return 0; return keyflags; } __declspec(dllexport) int getmsgid(void) { if(!mousemsgid) return 0; return mousemsgid; } __declspec(dllexport) mousehookstruct getmousestruct(void) { return mhookstruct; } __declspec(dllexport) void installhooks(void) { hkeyboardhook = setwindowshookex(wh_keyboard, keyboardproc, hinstance, 0); hmousehook = setwindowshookex(wh_mouse, mouseproc, hinstance, 0); } __declspec(dllexport) void uninstallhooks(void) { unhookwindowshookex(hkeyboardhook); hkeyboardhook = 0; lastkey = 0; keyflags = 0; unhookwindowshookex(hmousehook); mousemsgid = 0; mhookpt = null; }
it's compiled dll, loaded python using ctypes.cdll('blah.dll')
:
class mousehookstruct(ctypes.wintypes.structure): _fields_ = [("pt", ctypes.wintypes.point), ("hwnd", ctypes.wintypes.hwnd), ("dwextrainfo", ctypes.wintypes.pointer(ctypes.wintypes.c_ulong))] dll = ctypes.cdll('blah.dll') dll.getmousestruct.restype = mousehookstruct try: dll.installhooks() mstruct = mousehookstruct() while true: mstruct = dll.getmousestruct() print mstruct.pt.x, mstruct.pt.y except keyboardinterrupt: pass finally: dll.uninstallhooks()
all print out 0 0
regardless of mouse is. keyboard hook fine because callback proc not make use of struct. however, if try wh_keyboard_ll have same issue (all values of struct 0). missing passing structs between os, c , pythons ctypes?
Comments
Post a Comment