Sign in to follow this  
irreversible

Is there any way to pass a user-defined variable to a windows hook proc?

Recommended Posts

I'm hooking a menu window and I need to identify the menu object (or even just the original proc will do) in the eventual subclassed proc. However, since I don't know the menu's HWND handle before I reach the actual subclassed proc and I can't pass a user-defined parameter to CreateWindowsHookEx, I can't match the window handle I'm getting to even a globally stored proc or object/menu handle stack, since there's simply no way to correlate between the two (as far as I can see).


Note that this is relevant, because I'm working with more than one menu at a time.

Any ideas or clues?

Share this post


Link to post
Share on other sites
I'm not sure of I followed 100%, but might it be possible to use SetWindowLongPtr http://msdn.microsoft.com/en-us/library/windows/desktop/ms644898(v=vs.85).aspx with GWLP_USERDATA to attach custom identifier data to the window?

Share this post


Link to post
Share on other sites
That's the big problem. Allow me to elaborate:

Step 1)
HMENU m = CreatePopupMenu();
SetWindowsHookExW(WH_CALLWNDPROC, (HOOKPROC)HookProc, 0, GetCurrentThreadId());

Step 2)
In the hook proc, intercept HC_ACTION and call SetWindowLong() to subclass the window when the desired window class is created (by intercepting WM_CREATE)

Problem is, in the hook proc there is no longer a way to know what (eg which instance of the calling class) is initiating the call, because sending data between the two is impossible. In effect there's no way to set GWL_USERDATA, because in step 1 you only have a HMENU and no corresponding HWND and in step two you have a HWND and no corresponding HMENU. Moreover, there's no way to pass user data to SetWindowsHookEx(). Effectively, I don't see a way to link a HMENU to its hooked HWND, which is what I need.

I hope that's more clear :)

Share this post


Link to post
Share on other sites
If you don't mind me asking, I'm curious what you want to do that makes the menu's window's Proc unsuitable? I'm mainly asking because this is my first encounter of SetWindowsHookEx.

Share this post


Link to post
Share on other sites
Perhaps you can use SetMenuInfo to associate custom identifying data with the HMENU you created: http://msdn.microsoft.com/en-us/library/windows/desktop/ms647997(v=VS.85).aspx

Then in the handler, query the HMENU associated with the HWND that you receive using GetMenu: http://msdn.microsoft.com/en-us/library/windows/desktop/ms647640(v=VS.85).aspx

It might be possible to also query the HWND associated with a HMENU by using FindWindow: http://stackoverflow.com/questions/4347488/how-to-get-the-hwnd-from-hmenu , but I'm not completely certain that it will work.




Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this