Article View: borland.public.cppbuilder.nativeapi
Article #76Re: Setting (and losing) a keyboard hook
From: salamander1965
Date: Thu, 19 Jun 2008 21:04
Date: Thu, 19 Jun 2008 21:04
62 lines
4459 bytes
4459 bytes
Remy Lebeau (TeamB) wrote: > Your first instance is likely deactivating the hook during its shutdown, > thus it is no longer available for any instances that are still running. > The behavior you desacribe would suggest that the hook is being associated > with the first instance (per the hMod parameter of SetWindowsHookEx()). I played around with the message boxes a little more, having them also report the hMod parameter that came in to the DllEntryPoint() function and which was subsequently used in the call to SetWindowsHookEx(). The value of hMod is the same for each of the app instances that are started, so I'm not sure how the hook could be deactivated by one particular instance (the first one) terminating. By putting a MessageBox() call in the hook procedure itself, though, I found that it's not the termination of the first app instance that deactivates the hook, but instead the detaching of Explorer.exe from the DLL that immediately follows the detaching of the first app instance, even though it was in the context of the first app process that the hook was set. I'm still not clear on why Explorer.exe associates itself with the DLL only when the first app instance loads the library (but not subsequent loads), or why it even wants to be involved in this at all, but it seems to be causing the problem. > To do what you are attempting, I suggest moving the hook code into a > separate .exe of its own. The first instance of your app can launch the > hook .exe, which then loads the DLL hook. The last instance of your app can > terminate the hook .exe, unloading the DLL. That way, the hook is > associated with its own process, not any particular instance of your app. If you have any idea what's going on as things stand now, I'd like to hear it. In the meantime, though, I'm going to try the approach you suggested (for which, thanks). Just working it out on paper at this point, I think I see how to go about it -- something like this: 1. hook.dll will contain the hook procedure itself (as it has to). 2. hook.exe will load the DLL and set the hook. 3. the first app instance will start hook.exe with CreateProcess(), but subsequent app instances will not; I think a named mutex created by the first app will work for this. 4. all of the app instances should be able to take advantage of the registered message coming from the hook procedure in the DLL. 5. the instances of the app somehow need to 'know' if they are the last to terminate so they can terminate hook.exe (which will unload the DLL) before closing. I'm thinking shared memory through a FileMapping object would be the simplest way to maintain a reference count of the number of instances running. 6. when the last instance is closing, it signals hook.exe which calls ExitProcess() on its own process, allowing it clean up and unload the DLL. Since there's really no reason for hook.exe to have a message loop, I'm thinking an event would be a good signal, with hook.exe using WaitForSingleObject() after it loads the DLL. At least on paper, this seems reasonable, and I think it's at least roughly what was suggested. Anyone feel free to tell me how stupid it really is, though. Thanks -- Steve
Technical Details
Message-ID:
<485ac9db$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder2.cambrium.nl!feed.tweaknews.nl!193.201.147.85.MISMATCH!feed.xsnews.nl!border-2.ams.xsnews.nl!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4859926a@newsgroups.borland.com> <485a909f$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,21bdbb7b8038a591 X-Google-Attributes: gid10cc67,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Date: Thu, 19 Jun 2008 17:04:43 -0400 From: salamander1965 <salamander1@verizon.net> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Setting (and losing) a keyboard hook References: <4859926a@newsgroups.borland.com> <485a909f$1@newsgroups.borland.com> In-Reply-To: <485a909f$1@newsgroups.borland.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 205.149.71.160 Message-ID: <485ac9db$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1213909467 205.149.71.160 (19 Jun 2008 14:04:27 -0700) Lines: 62 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder2.cambrium.nl!feed.tweaknews.nl!193.201.147.85.MISMATCH!feed.xsnews.nl!border-2.ams.xsnews.nl!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail