SetWindowsHookEx and VB.NET 2005

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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?

You can't write global hooks in VB.NET or any other language producing
managed code. Better use plain old C for that.


Mattias
 
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


bruno said:
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.
 
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


bruno said:
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.
 
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.
 
Colin,
I had not seen the article given by Rocky... it is a good one.

Interesitng, I was just going to reply to Herfired who posted the same
link and say that I thought it was a poorly written article. It
contains incorrect statements and buggy code. The code in your blog
post was at least somewhat more correct.


Mattias
 
Mattias,
Interesitng, I was just going to reply to Herfired who posted the same
link and say that I thought it was a poorly written article. It
contains incorrect statements and buggy code.

I must confess I only skimmed it... at a high level, it seemed to cover most
of the bases. I suppose I should not endorse something that I have only
skimmed :) Would you like to elaborate on "the statements that are
incorrect"?

The code in your blog post was at least somewhat more correct.

That's good. I welcome any suggestions for improvement.
 
Thank you Rocky,
the article is very clear and explained me thinks I was looking for.
I've just created a form with two buttons: Hook and Unhook.
I copy/pasted the full sample but I'm getting : "Keyboard hook failed: 0"

I' using:
Microsoft Visual Studio 2005 Version 8.0.50727.42 (RTM.050727-4200)
Microsoft .NET Framework Version 2.0.50727
Installed Edition: Professional
Microsoft Visual Basic 2005 77626-009-0000007-41133

--
bruno


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.
 
I copy/pasted the full sample but I'm getting : "Keyboard hook failed: 0"

What version of windows are you running?
 
Collin,
Thanks for your well detailed explanation of getHINSTANCE.
After revising the SetWindowsHookEx documentation I've done this:
- put your two classes "Win32" and "GlobalHook" toghether in a VB.NET
project,
- compile and get the DLL
- create a separate EXE Appl. that references that DLL

Now SetWindowsHookEx (mouse and Kb) doesn't give me error, but
"MouseHookProc" gets never control and "KeyboardHookProc" gets control always
with KeyDownEvent and KeyUpEvent= nothing, so no "Onxxx" procedure get fired.
I will go on with more test, but at the moment have no idea.

Thanks to all you for informations.
 
Bruno,

I found the problem. You must disable the "Enable the Visual Studio hosting
process" option in the Menu--->Project-->Properties---->Debug form. Uncheck
the box .
 
OK. Thank you very much
--
bruno


Rocky said:
Bruno,

I found the problem. You must disable the "Enable the Visual Studio hosting
process" option in the Menu--->Project-->Properties---->Debug form. Uncheck
the box .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top