windows - How to hook external process with SetWindowsHookEx and WH_KEYBOARD -


I'm trying to hook for Notepad example without success

on XP SP2 testing.

EDIT: The revised code now works.

MyDLL code

  #include & lt; Windows.h & gt; # Include & lt; Iostream & gt; # Include & lt; Stdio.h & gt; HISTENCE HINT; #pragma data_seg ("Shared") HHOOK hhk; #pragma data_seg () // # pragma comment (linker, "/section:.shared, RWS") VC ++ Express 2008 LRESULT callback wireKeyboardProc (int code, WPARAM wParam, LPARAM lParam) compiler error {if (code & lt ; 0) {return callNextHookEx (0, code, wParam, lParam); } Beep (1000, 20); Return CallNextHookEx (hhk, code, wParam, lParam); } Extern "C" __declspec (dllexport) Zero installation (unsigned long thread ID) {hhk = SetWindowsHookEx (WH_KEYBOARD, wireKeyboardProc, hinst, threadID); } Extern "c" __declspec (dllexport) void uninstall () {UnhookWindowsHookEx (hhk); } BOOL WINAPI DllMain (histones hinstDLL, __in DWORD fdwReason, __in LPVOID lpv reserved in ___) {hinst = hinstDLL; Return TRUE; }  

My program

  #include & lt; Windows.h & gt; Unsigned long GetTargetThreadIdFromWindow (char * className, char * windowName) {HWND target Wnd; Handle H Procedure; Unsigned long processID = 0; TargetWnd = FindWindow (className, windowName); Return GetWindowThreadProcessId (targetWnd, and processID); } Int _tmain (int argc, _TCHAR * argv []) {unsigned long threadID = GetTargetProcessIdFromWindow ("Notepad", "Untitled - Notepad"); Printf ("TID:% i", ThreadID); Histine hinst = load library (_T ("MyDLL.dll")); If (hinst) {typedef zero (* install) (unsigned long); Typedef void (* uninstall) (); Install Install = (Install) GetProcAddress (hinst, "Install"); Uninstall Uninstall = (Uninstall) GetProcAddress (hinst, "uninstall"); Installed (threadid); Sleep (20000); Uninstall (); } Return 0; }  

Three problems:

You are using the process IDs should be used when using your thread ID.

Your HHOOK needs to go into shared memory:

  #pragma data_seg ("shared.") HHOOK hhk = NULL; #pragma data_seg () #pragma Comment (Linker, "/section:.shared, RWS")  

You need to pass from your HHOOK to CallNextHookEx :

  return callNextHookEx (hhk, code, wParam, lParam);  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -