473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

SetWindowsHookEx and VB.NET 2005

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
Jan 6 '06 #1
15 11852
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.
Jan 6 '06 #2
Bruno,

I just posted an example on my blog:
http://www.colinneller.com/blog/Perm...e5918a39c.aspx.
Take a look and see if it's what you're looking for.

--
Colin Neller
http://www.colinneller.com/blog
"bruno" <br***@community.nospam> wrote in message
news:BE**********************************@microsof t.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

Jan 6 '06 #3
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.GetExecut ingAssembly().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/Perm...e5918a39c.aspx.
Take a look and see if it's what you're looking for.

--
Colin Neller
http://www.colinneller.com/blog
"bruno" <br***@community.nospam> wrote in message
news:BE**********************************@microsof t.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


Jan 6 '06 #4
http://www.developer.com/net/vb/article.php/2193301
"bruno" <br***@community.nospam> wrote in message
news:A3**********************************@microsof t.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.GetExecut ingAssembly().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/Perm...e5918a39c.aspx.
Take a look and see if it's what you're looking for.

--
Colin Neller
http://www.colinneller.com/blog
"bruno" <br***@community.nospam> wrote in message
news:BE**********************************@microsof t.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


Jan 6 '06 #5
"bruno" <br***@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/>

Jan 6 '06 #6
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" <no*****@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
http://www.developer.com/net/vb/article.php/2193301
"bruno" <br***@community.nospam> wrote in message
news:A3**********************************@microsof t.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.GetExecut ingAssembly().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/Perm...e5918a39c.aspx.
Take a look and see if it's what you're looking for.

--
Colin Neller
http://www.colinneller.com/blog
"bruno" <br***@community.nospam> wrote in message
news:BE**********************************@microsof t.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


Jan 6 '06 #7
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.
Jan 6 '06 #8
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.

--
Colin Neller
http://www.colinneller.com/blog
Jan 6 '06 #9
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" wrote:
http://www.developer.com/net/vb/article.php/2193301
"bruno" <br***@community.nospam> wrote in message
news:A3**********************************@microsof t.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.GetExecut ingAssembly().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/Perm...e5918a39c.aspx.
Take a look and see if it's what you're looking for.

--
Colin Neller
http://www.colinneller.com/blog
"bruno" <br***@community.nospam> wrote in message
news:BE**********************************@microsof t.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


Jan 6 '06 #10
> 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
Jan 6 '06 #11
WinXP prof sp2
--
bruno
"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

Jan 6 '06 #12
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

Jan 6 '06 #13
Here's another one to look at:
http://msdn.microsoft.com/msdnmag/is...0/CuttingEdge/

"bruno" <br***@community.nospam> wrote in message
news:E7**********************************@microsof t.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

Jan 7 '06 #14
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" <br***@community.nospam> wrote in message
news:E7**********************************@microsof t.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

Jan 7 '06 #15
OK. Thank you very much
--
bruno
"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" <br***@community.nospam> wrote in message
news:E7**********************************@microsof t.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


Jan 7 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bugs bunny via .NET 247 | last post by:
Does anyone have actually used SetWindowsHookEx() to hook upShellExecute() so as to find what application was started bywindows or which folder is currently browsed by explorer. I knowone thing and...
1
by: Mike Carlisle | last post by:
Hi, Can't seem to find any sensible C# examples of this, struggling to work out the correct way to trap key combinations using SetWindowsHookEx with WH_KEYBOARD_LL, such as ALT-ESC. private...
4
by: abcd | last post by:
I am having trouble with pyHook on python 2.4.1. Basically I have a python app that uses pyHook to capture keyboard events and write them straight to a file. The application is running as a...
2
by: KWhat4 | last post by:
I seem to have an issue that I cant resolve. I have a jni dll that calls SetWindowsHookEx with a callback to HookKeyboardProc (basically a real simple global keyboard hook). Now the dll almost...
5
krungkrung
by: krungkrung | last post by:
Hi everyone! Im new to vb.net. I'm currently recoding my vb6.0 codes to vb.net(for upgrading purposes), unfortunately some of the codes dont work/execute the way it executes in vb6.0, though I never...
2
by: niksoftware | last post by:
I'm making a kiosk style desktop replacement. Im using setwindowshookex to run a callback system to trap the keys just trapping "windows" key atm. if I step through just as the program goes...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.