tags:

views:

429

answers:

3
+3  Q: 

DllMain in an exe?

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.

+1  A: 

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.

Franci Penov
A: 

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!

Prakash
InitInstance and ExitInstance are MFC constructs, not part of the win32 API.
Ferruccio
It's uncommon, but not unseen, to have an executable be treated as a DLL. After all, executables can still export symbols and both executables, DLLs, control panel applets, etc. are all just PE files with different extensions.
Chris Charabaruk
mind you I was asking about THREAD attach and detach
shoosh
+1  A: 

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 using CreateProcess() with the DEBUG_ONLY_THIS_PROCESS flag, the parent ('debugger') process can call WaitForDebugEvent() 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.

Michael Burr