Is it possible to receive DllMain like notifications about thread attach/detach in stand-alone exe without using any extra dlls?
Edit: This is just a theoretical question that has to do with some testing I'm doing. not a real life situation.
Is it possible to receive DllMain like notifications about thread attach/detach in stand-alone exe without using any extra dlls?
Edit: This is just a theoretical question that has to do with some testing I'm doing. not a real life situation.
Interesting question. I don't know of anything built into Win32 - I think you might have to to whip up a DLL that has an API that signaled events or posted messages when it got the various attach/detach messages.
An alternative that would not require a separate DLL but would require some hack trickery is to use the debugging API (
WaitForDebugEvent()
specifically). If your application has a special 'test' mode (maybe indicated by a command line option) that does nothing but relaunch the exe usingCreateProcess()
with theDEBUG_ONLY_THIS_PROCESS
flag, the parent ('debugger') process can callWaitForDebugEvent()
to get notification of the thread start and end events as well as a bunch of other interesting events. The parent process can pass them on to the child as messages or by signaling events (if that's what you want) or perform its own logging if thats all you need.By no means a simple thing, but it would work and would not require a separate DLL or image, just a special mode for when you want to perform these tests.
Your question is to get notification while the process is getting loaded and unloaded. This is more applicable for DLLs as they are getting loaded by other processes.
For Exes, you have InitInstance and ExitInstance which you can handle equivalently!
There is no external code that runs on the thread and loads the executable, hence no thread attach/detach notifications [1]. The code in the executable usualy control the threading [2].
If you describe your scenario, people might be able to give you some ideas how to achieve it.
[1] Well, most of the time. It is possible to load an executable in another process, but people don't do it usually.
[2] There are certain exceptions where the threading model and the threads are created by the OS, instead of the executable code. These are mostly related to COM/RPC.