Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

friendkey

About SetWindowsHookEx

This topic is 5218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to set a hook for keyboard events. But the program failed to call the function SetWindowsHookEx. Who can tell me where the problem is? CreateMutex(NULL, FALSE, "LstnMsg"); if(GetLastError()==ERROR_ALREADY_EXISTS) return ERROR_ALREADY_EXISTS; if(hInstDll = LoadLibrary((LPCTSTR)"HookInput.dll")) { if((InstallHook = (PINSTALLHOOK)GetProcAddress(hInstDll, "InstallHook")) && (UnInstallHook = (PINSTALLHOOK)GetProcAddress(hInstDll,"UnInstallHook"))) if(!InstallHook()) { MessageBox(NULL, "Install hook failed.", "Error!", 0); return 0; } else { MessageBox(NULL, "Can''t load procedure InstallHook or UnInstallHook.", "Error!", 0); return 0; } } else { MessageBox(NULL, "Can''t find \"HookInput.dll\".","Error!", 0); return 0; } The code above is in the beginning of the function _tWinMain. Run the program, a message box"Install hook failed" pops, so I''m sure SetWindowsHookEx(in InstallHook) returns 0. The code for "HookInput.Dll": // HookInput.cpp : Defines the entry point for the DLL application. // #include "stdafx.h" #include "HookInput.h" static HHOOK hkb=NULL; HINSTANCE hInst; BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } EXPORT BOOL InstallHook() { hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hInst,0); return hkb ? TRUE : FALSE; } EXPORT LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { POINT point={20,20}; HWND hWnd = FindWindow("Notepad", NULL); hWnd = ChildWindowFromPoint(hWnd,point); PostMessage(hWnd, WM_CHAR, wParam, lParam); return CallNextHookEx( hkb, nCode, wParam, lParam ); } EXPORT BOOL UnInstallHook() { return UnhookWindowsHookEx(hkb); }

Share this post


Link to post
Share on other sites
Advertisement
It might be something to do with policy / permissions on your machine. Also might depend on your operating system

What was the error code ( and english tranaslation) given by api call to getlasterror?

Share this post


Link to post
Share on other sites
I worked it out.
really stupid!
I fogot to assign for hInst in HookInput.cpp(for HookInput.dll).
And "if" statment in the main function was wrong nested...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!