c - KeyboardHookProc in DLL doesn't do anything when called from python -


i've been trying write dll in c.

install hook sets keyboardproc. calling installhook() , uninstallhook() functions python returns 0, guess because callback function keyboardproc isn't working.

the following c code dll:

#include "stdafx.h" #include <windows.h> #include <string.h> #include <ctype.h> #include <stdio.h> #include "ourdll.h"  //#pragma comment(linker, "/section:.shared,rws") //#pragma data_seg(".shared") hhook hkeyboardhook = 0; int keypresses = 0;     hmodule hinstance = 0;  //#pragma data_seg()  bool winapi dllmain (handle hmodule, dword dwfunction, lpvoid lpnot) {             hinstance = hmodule;  //edit     return true; }  lresult callback keyboardproc(int hookcode, wparam vkeycode, lparam flags) {     if(hookcode < 0)     {         return callnexthookex(hkeyboardhook, hookcode, vkeycode, flags);     }      keypresses++;;      return callnexthookex(hkeyboardhook, hookcode, vkeycode, flags); }  __declspec(dllexport) void installhook(void) {     hkeyboardhook = setwindowshookex(wh_keyboard, keyboardproc, hinstance, 0); }  __declspec(dllexport) int uninstallhook(void) {     unhookwindowshookex(hkeyboardhook);     hkeyboardhook = 0;     return keypresses; } 

the python code use follows:

>>> ctypes import * >>> dll = cdll('c:\...\ourdll.dll') >>> dll.installhook() 

[type @ point]

>>> result = dll.uninstallhook() >>> result 0 

edit: should mention i've tried out lowlevelkeyboardhook. understand lowlevel hook global , catch keystrokes, caused dll.installhook() python code freeze second or 2 before returning zero.

i no expert in c, appreciated. thanks.

hkeyboardhook = setwindowshookex(wh_keyboard, keyboardproc, null, 0); 

setwindowshookex requires hmodule - save hmodule dllmain , pass here. (you can pass null if thread id own thread.)

one exception _ll hook types; these don't need hmodule param since these hook don't inject target process - that's why code using keyboard_ll 'succeeding'.

as why might blocking when use keyboard_ll - docs lowlevelkeyboardhookproc mention thread installs hook (ie. calls setwindowshookex) must have message loop, might not have in python code.

debugging tips: looks setwindowshookex should returning null (with getlasterror() returning suitable error code); while developing code, using combination of assert/printf/outputdebugstring appropriate check these return values way ensure assumptions correct , give clues things going wrong.

btw, 1 other thing watch keyboard vs keyboard_ll: keyboard hook gets loaded target process - if it's same bitness - 32-bit hook sees keys pressed other 32-bit processes. otoh, keyboard_ll called in own process, see keys - , don't need deal shared segment (though far know it's less efficient keyboard hook).


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -