473,800 Members | 2,614 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SetWindowsHookE x and VB.NET 2005

I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb input.
this is the statement:
hHOOKKb = SetWindowsHookE x( _
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 11901
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***@communit y.nospam> wrote in message
news:BE******** *************** ***********@mic rosoft.com...
I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb
input.
this is the statement:
hHOOKKb = SetWindowsHookE x( _
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.InstallHook s()
but I got an error: "SetWindowsHook Ex failed."

In my trials, writing:
hHOOK = SetWindowsHookE x(WH_KEYBOARD, _
hookproc, _
IntPtr.Zero, _
AppDomain.GetCu rrentThreadId() )
I was able to hook my running program. But I was not able to hook all
threads.
Can you explain me more about :
Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().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***@communit y.nospam> wrote in message
news:BE******** *************** ***********@mic rosoft.com...
I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb
input.
this is the statement:
hHOOKKb = SetWindowsHookE x( _
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***@communit y.nospam> wrote in message
news:A3******** *************** ***********@mic rosoft.com...
Colin,
thank you for the sample. It is what I was looking for.
I just tried it:

Dim Hks As New GlobalHook
Hks.InstallHook s()
but I got an error: "SetWindowsHook Ex failed."

In my trials, writing:
hHOOK = SetWindowsHookE x(WH_KEYBOARD, _
hookproc, _
IntPtr.Zero, _
AppDomain.GetCu rrentThreadId() )
I was able to hook my running program. But I was not able to hook all
threads.
Can you explain me more about :
Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().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***@communit y.nospam> wrote in message
news:BE******** *************** ***********@mic rosoft.com...
>I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb
>input.
> this is the statement:
> hHOOKKb = SetWindowsHookE x( _
> 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***@communit y.nospam> schrieb:
I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb
input.


<URL:http://www.developer.c om/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.Reflecti on.Assembly
thisAssembly = System.Reflecti on.Assembly.Get ExecutingAssemb ly

'get all modules in this assembly (there is only one)
Dim myModules() As System.Reflecti on.Module
myModules = thisAssembly.Ge tModules

'get the only module
Dim thisModule As System.Reflecti on.Module
thisModule = myModules(0)

'get a pointer to the module
Dim thisModulePoint er As IntPtr
thisModulePoint er = Marshal.GetHINS TANCE(thisModul e)

hMouseHook = Win32.SetWindow sHookEx( _
Win32.WH.WH_KEY BOARD_LL, _
KeyboardHookPro cedure, _
thisModulePoint er, _
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*****@nowher e.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
http://www.developer.com/net/vb/article.php/2193301
"bruno" <br***@communit y.nospam> wrote in message
news:A3******** *************** ***********@mic rosoft.com...
Colin,
thank you for the sample. It is what I was looking for.
I just tried it:

Dim Hks As New GlobalHook
Hks.InstallHook s()
but I got an error: "SetWindowsHook Ex failed."

In my trials, writing:
hHOOK = SetWindowsHookE x(WH_KEYBOARD, _
hookproc, _
IntPtr.Zero, _
AppDomain.GetCu rrentThreadId() )
I was able to hook my running program. But I was not able to hook all
threads.
Can you explain me more about :
Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().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***@communit y.nospam> wrote in message
news:BE******** *************** ***********@mic rosoft.com...
>I need some help on how write SetWindowsHookE x in vb.net 2005 to hook
>kb
>input.
> this is the statement:
> hHOOKKb = SetWindowsHookE x( _
> 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***@communit y.nospam> wrote in message
news:A3******** *************** ***********@mic rosoft.com...
Colin,
thank you for the sample. It is what I was looking for.
I just tried it:

Dim Hks As New GlobalHook
Hks.InstallHook s()
but I got an error: "SetWindowsHook Ex failed."

In my trials, writing:
hHOOK = SetWindowsHookE x(WH_KEYBOARD, _
hookproc, _
IntPtr.Zero, _
AppDomain.GetCu rrentThreadId() )
I was able to hook my running program. But I was not able to hook all
threads.
Can you explain me more about :
Marshal.GetHINS TANCE(Reflectio n.Assembly.GetE xecutingAssembl y().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***@communit y.nospam> wrote in message
news:BE******** *************** ***********@mic rosoft.com...
>I need some help on how write SetWindowsHookE x in vb.net 2005 to hook kb
>input.
> this is the statement:
> hHOOKKb = SetWindowsHookE x( _
> 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

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

Similar topics

1
3099
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 that is ... Windows uses ShellExecute() to doalmost every task. I need some implementation help. I want to trigger an userdefined function when suppose Notepad is opened or in simplecase a messagebox that says Notepad has started. If anyone has a...
1
8447
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 int InterpretKey(int nCode, IntPtr wParam, IntPtr lParam) { .....
4
6065
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 service on a windows machine. If I am at that windows machine the application works just fine, logging my keystrokes. However, if I use Remote Desktop to connect to the machine and open up say Notepad it doesn't capture the keystrokes. Is this a...
2
3894
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 works all the jni hooks are fine and the program appears to run but the callback (HookKeyboardProc) does not seem to get executed or possibly never returns from execution. The code is as follows. None of the variables being set in the code...
5
4562
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 get any error. That is, the program runs without error but the function being called dont execute. Can anybody help me figure out this matter?? ***HERES WHAT SHOULD THE PROGRAM DO.... message box should always display at the center of the owner...
2
2898
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 the the keyboardcallback function declaration line it pauses for seconds before moving to the next line. This directly precedes the new variable declaration of the callback variable While running this leads to only 1 press of the "windows" keys...
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.