i'm trying to write a shell hook using the SetWindowsHookEx function.
here's part of my code:
Imports System
Imports System.Runtime.InteropServices
Private Delegate Function HookCallBack(ByVal nCode As Integer,
ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Class Hook
Declare Auto Function SetWindowsHookEx Lib "user32.dll" (ByVal
idHook As Integer, ByVal lpfn As HookCallBack, ByVal hMod As Integer,
ByVal dwThreadID As Integer) As Integer
Declare Auto Function UnhookWindowsHookEx Lib "user32.dll"
(ByVal hhk As Integer) As Integer
End Class
Private Function ShellProc(ByVal nCode As Integer, ByVal wParam As
Integer, ByVal lParam As Integer) As Integer
If nCode = HSHELL_WINDOWACTIVATED And wParam = myHnd Then
Console.WriteLine("Window activated!")
Hook.UnhookWindowsHookEx(myHook)
End If
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click
myHook = Hook.SetWindowsHookEx(WH_SHELL, AddressOf ShellProc,
0, myId)
End Sub
---- END OF CODE LISTING ----
myId is set the process ID which I get using a Process object.
OK... so, when button2 gets clicked the hook should be created and i
should get a handle to the hook returned in myHook. But it doesn't
happen... I stepped through the code and myHook doesn't get assigned
any value (it stays at 0). For some reason, the hook isn't getting
created.
I think it may have something to do with the fact that the 3rd
argument in the SetWindowsHookEx call is supposed to be a NULL if i'm
making a thread-level hook. There's no VB.net equivalent for NULL, so
I tried both 0 and Nothing, but it makes no difference. Could this be
the problem? If so, then how do I send a NULL value? I read somewhere
that this would require tweaking MSIL code. Please help!
Does this help?
"HOW TO: Set a Windows Hook in Visual Basic .NET"
http://support.microsoft.com/?id=319524
Thread ID should be used as last parameter to SetWindowsHookEx, e.g.
AppDomain.CurrentDomain.GetCurrentThreadId()).
..
Regards,
Vadim.
> "HOW TO: Set a Windows Hook in Visual Basic .NET"
> http://support.microsoft.com/?id=319524
>
>
> Thread ID should be used as last parameter to SetWindowsHookEx, e.g.
> AppDomain.CurrentDomain.GetCurrentThreadId()).
>
> ..
> Regards,
> Vadim.
Thanks for that link... Yes, you were right, I was supposed to use the
thread ID of the window, not the process itself. I was unaware that
they would be different. I got the code to work by using
p.threads(0).id (where p is a process object).
Anyway, the link also tells me that I can't implement a global hook in
the .net framework. This is somewhat unfortunate since I was trying to
write a shell using .net. I think the only workaround would be to
write the DLL for the global hook in unmanaged C++. What do you think?
Thanks,
ShellD00d.
> Anyway, the link also tells me that I can't implement a global hook in
> the .net framework. This is somewhat unfortunate since I was trying to
> write a shell using .net. I think the only workaround would be to
> write the DLL for the global hook in unmanaged C++. What do you think?
It's good idea to implement global hook in unmanaged C++, I would even make
this DLL as small as possible and without CRT and other libraries
dependencies. And probably it's good that .NET doesn't support global
hooks:). Just imagine - global hook DLL is loaded by each process in the
same desktop, and if this hook would be managed, each process loaded Common
Language Runtime consuming a lot of system resources.
..
Regards,
Vadim.
>
> It's good idea to implement global hook in unmanaged C++, I would even make
> this DLL as small as possible and without CRT and other libraries
> dependencies. And probably it's good that .NET doesn't support global
> hooks:). Just imagine - global hook DLL is loaded by each process in the
> same desktop, and if this hook would be managed, each process loaded Common
> Language Runtime consuming a lot of system resources.
>
I've tried code samples in .net that implement a global mouse and
keyboard hook. How do they work? I've been trying to implement a
global shell hook. It works on and off. I've posted a new thread with
all my code and output. Take a look and see if you find it
interesting. Thanks again for your assistance!