Home All Groups Group Topic Archive Search About

SetWindowsHookEx and VB.NET 2005

Author
6 Jan 2006 5:16 PM
bruno
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

Author
6 Jan 2006 5:31 PM
Mattias Sjögren
>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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
6 Jan 2006 5:49 PM
Colin Neller
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.

Show quoteHide quote
"bruno" <bruno@community.nospam> wrote in message
news:BE8FD86E-EB04-4D01-A17C-743D7EBEFBFE@microsoft.com...
>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
Author
6 Jan 2006 7:03 PM
bruno
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


Show quoteHide quote
"Colin Neller" wrote:

> 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" <bruno@community.nospam> wrote in message
> news:BE8FD86E-EB04-4D01-A17C-743D7EBEFBFE@microsoft.com...
> >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
>
>
>
Author
6 Jan 2006 7:22 PM
Rocky
http://www.developer.com/net/vb/article.php/2193301


Show quote Hide quote
"bruno" <bruno@community.nospam> wrote in message
news:A3D1DFC0-AA30-4007-9F70-705C8AFFFA77@microsoft.com...
> 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" wrote:
>
>> 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" <bruno@community.nospam> wrote in message
>> news:BE8FD86E-EB04-4D01-A17C-743D7EBEFBFE@microsoft.com...
>> >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
>>
>>
>>
Author
6 Jan 2006 8:13 PM
Colin Neller
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."

Show quoteHide quote
"Rocky" <nopl***@nowhere.com> wrote in message
news:%23wZkPavEGHA.2184@TK2MSFTNGP10.phx.gbl...
> http://www.developer.com/net/vb/article.php/2193301
>
>
> "bruno" <bruno@community.nospam> wrote in message
> news:A3D1DFC0-AA30-4007-9F70-705C8AFFFA77@microsoft.com...
>> 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" wrote:
>>
>>> 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" <bruno@community.nospam> wrote in message
>>> news:BE8FD86E-EB04-4D01-A17C-743D7EBEFBFE@microsoft.com...
>>> >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
>>>
>>>
>>>
>
>
Author
6 Jan 2006 8:43 PM
Mattias Sjögren
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 Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
6 Jan 2006 8:56 PM
Colin Neller
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.

Author
6 Jan 2006 9:16 PM
bruno
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


Show quoteHide quote
"Rocky" wrote:

> http://www.developer.com/net/vb/article.php/2193301
>
>
> "bruno" <bruno@community.nospam> wrote in message
> news:A3D1DFC0-AA30-4007-9F70-705C8AFFFA77@microsoft.com...
> > 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" wrote:
> >
> >> 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" <bruno@community.nospam> wrote in message
> >> news:BE8FD86E-EB04-4D01-A17C-743D7EBEFBFE@microsoft.com...
> >> >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
> >>
> >>
> >>
>
>
>
Author
6 Jan 2006 9:24 PM
Colin Neller
> I copy/pasted the full sample but I'm getting : "Keyboard hook failed: 0"

What version of windows are you running?

Author
6 Jan 2006 10:01 PM
bruno
WinXP prof sp2
--
bruno


Show quoteHide quote
"Colin Neller" wrote:

> > I copy/pasted the full sample but I'm getting : "Keyboard hook failed: 0"
>
> What version of windows are you running?
>
> --
> Colin Neller
> http://www.colinneller.com/blog
>
>
>
Author
6 Jan 2006 11:19 PM
bruno
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
Author
7 Jan 2006 12:59 AM
Rocky
Here's another one to look at:
http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/

Show quoteHide quote
"bruno" <bruno@community.nospam> wrote in message
news:E7E7056E-818F-4074-9A74-85CF68ABFD3D@microsoft.com...
> 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
>
Author
7 Jan 2006 3:22 AM
Rocky
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 .


Show quoteHide quote
"bruno" <bruno@community.nospam> wrote in message
news:E7E7056E-818F-4074-9A74-85CF68ABFD3D@microsoft.com...
> 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
>
Author
7 Jan 2006 8:06 PM
bruno
OK. Thank you very much
--
bruno


Show quoteHide quote
"Rocky" wrote:

> 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 .
>
>
> "bruno" <bruno@community.nospam> wrote in message
> news:E7E7056E-818F-4074-9A74-85CF68ABFD3D@microsoft.com...
> > 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
> >
>
>
>
Author
6 Jan 2006 7:53 PM
Herfried K. Wagner [MVP]
"bruno" <bruno@community.nospam> schrieb:
>I need some help on how write SetWindowsHookEx in vb.net 2005 to hook kb
>input.

<URL:http://www.developer.com/net/net/article.php/11087_2193301_1>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>