Jump to content
  • Advertisement
Sign in to follow this  
GuanHai

A problem of setting windows hooks

This topic is 3976 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 try to test the following code in VC6.0,but the values of the lpfn and the hook are always 0. What is wrong with the code below and is it ok to set a windows hook like this? typedef LRESULT (CALLBACK *HOOKPROC)(int, WPARAM, LPARAM); void main() { HMODULE hDll = LoadLibrary("MyHook.dll"); HOOKPROC lpfn = (HOOKPROC)GetProcAddress(hDll, "HookFun"); HHOOK hook = SetWindowsHookEx(WH_SYSMSGFILTER, lpfn, hDll, 0); } LRESULT CALLBACK HookFun(int nCode, WPARAM wParam, LPARAM lParam) { /*do something*/ return true; }

Share this post


Link to post
Share on other sites
Advertisement
From the MSDN:
Quote:

WH_SYSMSGFILTER - Global only

[snip]

All global hook functions must be in libraries.
To hook WH_SYSMSGFILTER, your hook proc needs to be in a DLL, so Windows can load / unload it properly.

Also, I'm sure you've been told this already, but you really shouldn't be using VC6. It's an absolutely terrible compiler. VC2005 Express is much better, and it's free.

Share this post


Link to post
Share on other sites
Thanks for taking time to help me and I am glad to discuss the problem with you.
Thank you for your recommending VC2005 and I'll try it.

But the function HookFun(...) mentioned above is already in a DLL(MyHook.dll) shown as follows:

/* the following code is in MyHook.dll*/
extern"C" __declspec(dllexport) LRESULT CALLBACK HookFun(
int nCode,
WPARAM wParam,

LPARAM,
lParam
)
{
/*do something*/
return true;
}
/*-----------------------------------------------------------------------*/

I wonder if there is something wrong with it.

Share this post


Link to post
Share on other sites
Sorry the function in my last reply should be:

extern"C" __declspec(dllexport) LRESULT CALLBACK HookFun(
int nCode,
WPARAM wParam, LPARAM lParam )
{
/*do something*/
return true;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!