Re: Using Visual Basic to Write System-Wide Hooks
- From: erewhon@xxxxxxxxxx (J French)
- Date: Thu, 2 Jun 2005 10:51:43 +0000 (UTC)
On Wed, 1 Jun 2005 13:33:00 -0600, "Jonathan Wood"
<jwood@xxxxxxxxxxxxxxxx> wrote:
>Okay, hopefully, this will be my last word on this.
>
>I've spent about an hour looking into writing a system-wide hook using only
>Visual Basic. In addition, I have researched this and spent many hours in
>the past. For those who are interested, here's what I've concluded.
>First, a system-wide callback function must be located within a DLL. The
>reason for this is that the DLL must be mapped into the address space of any
>application that causes an event being hooked. SetWindowsHookEx will
>automatically (but maybe not immediately) map the DLL into the address space
>of running applications.
That is what I have observed - the mapping is 'on demand'
>Second, I'm not sure Visual Basic is the best language to use for writing a
>system-wide hook DLL. The reason is that Visual Basic has some large runtime
>requirements, which must all be mapped into the address space of other
>processes. But no one has given any reason why this prevents a VB DLL from
>working for this purpose.
>Third, I've heard a lot of times that VB DLLs cannot be used from
>system-wide hooks for the reason that VB DLLs are ActiveX DLLs, which expose
>a COM interface instead of just exporting flat functions. The fact is that
>these two items are not related. A very simple workaround is to install the
>hook from the same DLL that contains the hook using AddressOf. Problem
>solved! A DLL function does not need to be exported in order to be used as a
>callback.
>Finally, there is a complex problem that occurs with system-wide hooks. When
>an event occurs in another process, the callback (located in the DLL) is
>called. But it is running in a different address space. This means that any
>global variables (for example, the hook handle needed in order to pass the
>hook on to CallNextHookEx) are not available from the DLL.
Interestingly I observed that in Delphi CallNextHookEx is just calling
a null pointer in the 'cloned' DLLs
It is only the first hook that really does a CallNextHookEx
>In C, it is possible to declare a shared data segment that is available to
>all instances of a DLL. I don't understand all the mechanics and
>ramifications of doing this but it allows a DLL written in C to communicate
>back to the parent application. It is this reason, and not anything to do
>with how VB DLLs export functions, that I was unable to implement a
>system-wide hook using VB (so far).
The big problem that I found in Delphi was getting the Window Handle
of the thing that one sets up to listen in the host EXE
Actually I would use SendMessage HWND_BROADCAST and
RegisterWindowMessage to /ask/ the host EXE where it wants the
messages sent.
>I suspect there is a way around this. In the end, I doubt it would be very
>efficient. (I mean, one approach would be to read the shared variables from
>a disk file which would be a horrible drain on performance). But I'm not
>convinced there isn't a way to keep communication open between a DLL mapped
>into another process and its parent application. If anyone has any ideas,
>I'd love to hear them.
As you'll see in another thread, I've also been playing with this.
At present I am not sure whether the crashes were down to the mapping,
or down to the processing in the hook.
Windows definitely /tries/ to map in the DLL
Right now I'm fed up with running ScanDisk
- also when I go back to this it will be on a more robust machine
.
- Follow-Ups:
- Re: Using Visual Basic to Write System-Wide Hooks
- From: Karl E. Peterson
- Re: Using Visual Basic to Write System-Wide Hooks
- From: Jonathan Wood
- Re: Using Visual Basic to Write System-Wide Hooks
- References:
- Using Visual Basic to Write System-Wide Hooks
- From: Jonathan Wood
- Using Visual Basic to Write System-Wide Hooks
- Prev by Date: Re: System-wide hooking, VB+ASM
- Next by Date: SHFileOperation destination
- Previous by thread: Re: Using Visual Basic to Write System-Wide Hooks
- Next by thread: Re: Using Visual Basic to Write System-Wide Hooks
- Index(es):
Relevant Pages
|