tags:

views:

33

answers:

2

I have an application loading a library containing a callback function for a global GetMessage hook. I get the procedure and pass it to SetWindowsHookEx to be set for all running threads.

The problem is that I want the DLL function to, under a certain circumstance like a keypress, tell the original application to exit, not all applications. The only way I can think of is to create a window in the application and have the DLL call SendMessage(HWND_BROADCAST) to send a custom message that the application recognizes to exit. But I want to verify if this is the best method to do this?

Another problem arises as well. Let's say I want the DLL to perform some one-time initialization such as opening a file. If I do this in DllMain then it seems to do this multiple times because it's being loaded multiple times. Is this a fix for this? Thanks.

+1  A: 

Try to use WinAPI CreateEvent function

macropas
+1  A: 

If your DLL run inside of an application there are a lot of ways to identify in which EXE I am currently running. For example one can use GetModuleFileName(NULL, ...) to get the path of the executable file of the current process. Another way: one can verify that some resource like the version resource exist in the current process with some special values of for example FileDescription (see http://msdn.microsoft.com/en-us/library/ms646981.aspx and http://msdn.microsoft.com/en-us/library/ms647464.aspx).

If your DLL don't have a per thread initializations (like TlsAlloc etc) it's a good idea to call DisableThreadLibraryCalls (see http://msdn.microsoft.com/en-us/library/ms682579.aspx and http://msdn.microsoft.com/en-us/library/ms682596.aspx) inside of DllMain is the second parameter is DLL_PROCESS_ATTACH.

Oleg