[Dev-C++] API hooking problems, please help.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <mr...@c2...> - 2002-02-06 10:36:06
|
Hi all,
I'm new to this list, and I've found no way to search the list
archives, so I'm posting the questions here. I've also posted this at
the Dev-C++ forum at Sourceforge, but no answers as of yet.
Goal:
I want to use the API hooking functions to monitor the messages to
another process/thread.
What I have done:
I've read most of the docs over at the MSDN site regarding this, and
I've understood that to hook other processes the hook function must
reside in a DLL.
So far so good.
I've created a DLL that contains a LRESULT CALLBACK function of type
GetMsgProc as described in the MSDN docs. The DLL also contains
functions for installing the hook, releasing the hook, and to read
data from a shared data segment within the DLL. This segment is
created with the #pragma data_seg() directive, like so:
#pragma data_seg(".SHARE")
// shared, initialized data here...
#pragma data_seg()
#pragma comment(linker,"/SECTION:.SHARE,rws")
In my (exported) Install_hook function I call SetWindowsHookEx with
the appropriate parameters, and it returns a valid hook handle, which
is stored in the shared data segment.
My hook function looks at the messages it retrieves, and sets
variables in the shared data segment accordingly. It also returns the
value from CallNextHookEx, as per MSDN spec.
In my application I load this DLL with LoadLibrary (I want to use run-
time linking for different reasons), and call GetProcAddress to get
the addresses to my exported functions.
I then call my Install_hook function to install the hook. In this
call I specify the dwThreadId parameter that is passed to
SetWindowsHookEx.
What happens next is that if I specify a dwThreadId of 0 to indicate
I want a system hook, my callback function is called, but only with
messages that is sent to my own application.
It seems my hook only hooks my own app, despite that I asked for a
system hook, and despite that SetWindowsHookEx indicated that all is
ok.
Now, I really don't want a system hook, but if I supply a specific
thread id in the call, my hook function is never called.
SetWindowsHookEx nevetheless returns a valid hook handle. If I supply
a non-existing or illegal thread id on purpose, SetWindowsHookEx
returns NULL, as it should.
Why does SetWindowsHookEx return a valid hook handle when it
obviously only hooks my own app that loaded the DLL in the first
place?
I should add that I'm not very proficient in using DLL's, and I'm
very blank when it comes to makefiles.
I use the Dev-C++ IDE for everything, including linker options etc.
When I compile my DLL I get a linker warning that I haven't provided
a .def file, and that dllwrap will create one for me, but I don't
know if this problem is related to that. The DLL seems to work ok
when viewed from my own app.
If this is the problem, could someone tell me how to proceed? If I
had a .def file, how could I tell the linker to use this file,
preferrably from within the IDE/Project options?
Best regards
-=D8yvind
---------------------------------
mr...@c2... @ work
|