Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WH_GETMESSAGE & System Hooks in general

343 views
Skip to first unread message

Samar Abbas Lotia

unread,
Jan 23, 1999, 3:00:00 AM1/23/99
to
Hi,

First of all I'm not positive where this should go so I am posting it on two
groups.

I'm experimenting with system level hooks for the first time and have run
into several confusing issues. I will try and outline them as best as I can
and would appreciate ANY pointers from people who have had some experience
with this stuff.

I'm implementing a system level WH_GETMESSAGE hook, and have implemented the
function in a DLL as the docs suggest. Now:

1) to call CallNextHookEx I need a handle to the current hook. Since my hook
function is in an external DLL I need to pass the value of the handle from
my app to the DLL. Right now I just have a variable that holds the handle
and have an exported function that sets the variable in the DLL. I also hope
that nothing will call my hook function b/w the time I call SetWindowsHookEx
and make the call to my exported function which retains the handle for use
by the hook function. more explicitly:

// DLL Code

static HHOOK myHook = 0;

__declspec(dllexport) void SetHookHandle(HHOOK hhk)
{
myHook = hhk;
}

__declspec(dllexport) LRESULT CALLBACK GetMsgProc(int code, WPARAM wParam,
LPARAM lParam)
{
if (code == HC_ACTION)
{
// Do stuff
}

return ::CallNextHookEx(myHook, code, wParam, lParam);
}

// Code in my application
hfunc_t lpfn = reinterpret_cast<hfunc_t>(::GetProcAddress(hmod,
"GetMsgProc"));
datafunc_t lpdfn = reinterpret_cast<datafunc_t>(::GetProcAddress(hmod,
"SetHookHandle"));

HHOOK hh = ::SetWindowsHookEx(WH_GETMESSAGE, lpfn, hmod, 0);
(*lpdfn)(hh);

Presumably there is a safer way to handle this.

2) How does the issue of concurrency come into play? Do I assume that my
hook function could be called simultaneously by multiple threads/processes,
and if so then I need to worry about serializing access to my DLL data?? For
instance suppose I am listening to WM_CHAR messages and am storing the typed
characters in a buffer looking for certain words. So would I need to worry
about serializing access to my buffer and if so, how much will this affect
overall performance of my system, since I will be clogging the que with my
crtical sections or mutexes.

Any general pointers on writing system level hooks (or links to web
resources) would also be much appreciated.

Thanks.

-Samar Lotia
<sal...@iname.com> -- remove the "foo" from iname.foo.com


David Lowndes

unread,
Jan 24, 1999, 3:00:00 AM1/24/99
to
>1) to call CallNextHookEx I need a handle to the current hook. Since my hook
>function is in an external DLL I need to pass the value of the handle from
>my app to the DLL.
>
>static HHOOK myHook = 0;

You need to put this variable in a shared data section (#pragma
data_seg), then it'll be the same in each process without you having
to do anything.

>2) How does the issue of concurrency come into play? Do I assume that my
>hook function could be called simultaneously by multiple threads/processes,

Yes it will be. Probably the only issue is with multi-threading in the
same process (unless you have shared data besides the hook handle), in
which case you'll need to use a critical section to guard any global
variables in your DLL.

Dave
----
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.

0 new messages