473,788 Members | 2,725 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 11900
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
3098
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
6063
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
4560
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
9656
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
9498
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,...
0
10373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
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...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5403
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...
1
4074
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
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.