First of all, I know global variables are evil 🙂 However, there is legit case why I need to use one.
I know that there are very strict limitation on what can be executed in DllMain (no LoadLibraries, no COM initialization and so on).
And I know that global variables are initialized just prior DllMain DLL_PROCESS_ATTACH.
Do I have the same limitation while global variable initialization?
I found Microsoft article: http://msdn.microsoft.com/en-us/library/988ye33t
However, it doesn’t have any details related to the limitations.
I saw also another Stackoverflow questions: What happens to global variables declared in a DLL?
Here is snippet from there “There are things that are forbidden to do in the DllMain. Those things are probably forbidden, too, in the constructors.”
However, it looks like commentor isn’t sure whether such limitations are really exist.
I would appreciate any information on this subject.
As explained in this answer of mine, the real dll entrypoint (i.e. the real
DllMain) is taken by the CRT, that, onDLL_PROCESS_ATTACH, initializes its stuff, calls the constructors of the globals and then call the “fake”DllMain(i.e. what you as a programmer see asDllMain).The MSDN documentation calls
DllMainthe real dll entrypoint, so all the restrictions apply also to the constructors of global objects, since they are called by it.This is yet another reason to avoid globals: the code in their constructors is called from
DllMain, but this is not evident, so in case of problems coming from doing “forbidden” things inDllMainit may take a lot of time before acknowledging that the offending code is in such constructors.