tags:

views:

41

answers:

2

Hello, I've made a mouse low level hook and it works fine except one problem: the procedure's param.

here's my code: http://pastebin.com/X2198UTb

My HookProc located in the middle of the code under my comment.

Is it a problem or it should be like that? if it should be like that - how can I know which window should get the right click? /// I added a condition to activate the event - right click.

A: 

This shouldn't even work at all. If you try the same thing (using LoadLibrary("User32") and a thread id of 0) in a pure native app, it will only work for a short while.

Use of a global hook requires a native (no C#) DLL. This is far more complicated than it might seem, especially if you want it to work on 64-bit Windows where you need both a 32- and 64-bit injection DLL as well as 32- and 64-bit injection processes.

Tergiver
+1  A: 

Your code is quite confusing because of the fact that you declared the WH_MOUSE constant to have the value 14. WH_MOUSE actually has the value 7, and 14 is the value of WH_MOUSE_LL (and yes I know you wrote "low level" in your question).

But then you go on by using the WH_MOUSE related types. Specifically, the hook procedure of a WH_MOUSE_LL hook receives a MSLLHOOKSTRUCT structure, not the MOUSEHOOKSTRUCT you're using.

Also, as Hans and Tergiver has hinted, you should pass in the module handle of your own code, not User32.dll. Try using Marshal.GetHINSTANCE(typeof(globalMouse).Module).

Mattias S
you right, It's should be WH_MOUSE_LL. If I pass what you said, It doesn't work at all, maybe if I'll create another dll it'll work, but User32.dll works... can I know the handle of the window that should recieve the message with the struct you pointed? (because I don't see 'hwnd' property)
Ohad
I gonna do WH_MOUSE(not LL) in CPP... I hope it'll work.
Ohad
No, low-level hooks are called before the message has been posted to the target thread's message queue. It's not yet associated with a specific hwnd.
Mattias S
Nice catch, I missed the 7/14. I wondered how that even worked.
Tergiver