Bruno,
I had not seen the article given by Rocky... it is a good one.
To help you better understand the getHINSTANCE call, I have broken it up
into smaller pieces. Think of and HINSTANCE as a pointer to your DLL:
****
'get a reference to this assembly
Dim thisAssembly As System.Reflection.Assembly
thisAssembly = System.Reflection.Assembly.GetExecutingAssembly
'get all modules in this assembly (there is only one)
Dim myModules() As System.Reflection.Module
myModules = thisAssembly.GetModules
'get the only module
Dim thisModule As System.Reflection.Module
thisModule = myModules(0)
'get a pointer to the module
Dim thisModulePointer As IntPtr
thisModulePointer = Marshal.GetHINSTANCE(thisModule)
hMouseHook = Win32.SetWindowsHookEx( _
Win32.WH.WH_KEYBOARD_LL, _
KeyboardHookProcedure, _
thisModulePointer, _
0)
*****
I think the problem with your code is that you are not using the low-level
hook (WH_KEYBOARD_LL) but the regular hook (WH_KEYBOARD). Also, you are
specifying an ID for the thread parameter. You should pass in a "0" here to
indicate that you want to be notified for all threads (and processes.) Here
is the description of the dwThreadId parameter from the docs:
"If this parameter is zero, the hook procedure is associated with all
existing threads running in the same desktop as the calling thread."
--
Colin Neller
http://www.colinneller.com/blog
Rocky said:
http://www.developer.com/net/vb/article.php/2193301
bruno said:
Colin,
thank you for the sample. It is what I was looking for.
I just tried it:
Dim Hks As New GlobalHook
Hks.InstallHooks()
but I got an error: "SetWindowsHookEx failed."
In my trials, writing:
hHOOK = SetWindowsHookEx(WH_KEYBOARD, _
hookproc, _
IntPtr.Zero, _
AppDomain.GetCurrentThreadId())
I was able to hook my running program. But I was not able to hook all
threads.
Can you explain me more about :
Marshal.GetHINSTANCE(Reflection.Assembly.GetExecutingAssembly().GetModules()(0)),
0)
what does it mean?
--
bruno
Colin Neller said:
Bruno,
I just posted an example on my blog:
http://www.colinneller.com/blog/PermaLink,guid,2838f59a-f4af-4c95-a322-b9ee5918a39c.aspx.
Take a look and see if it's what you're looking for.
--
Colin Neller
http://www.colinneller.com/blog
I need some help on how write SetWindowsHookEx in vb.net 2005 to hook
kb
input.
this is the statement:
hHOOKKb = SetWindowsHookEx( _
WH_KEYBOARD, _
lpfn, _
hMod, _
dwThreadId)
And this is from MSDN Library:
lpfn [in] Pointer to the hook procedure.
"hMod [in] Handle to the DLL containing the hook procedure pointed
to
by the lpfn parameter."
"dwThreadId [in] Specifies the identifier of the thread with which
the
hook
procedure is to be associated."
To get kb events from another thread, outside my program or from any
desktop
thread should I put the hook procedure in a DLL (a separate
module.vb)?
And
if so how do I get its Handle and lpfn?
Thanks.