NT DLL modules and C++

Gordon McMillan gmcm at hypernet.com
Thu Nov 4 19:41:05 EST 1999

Patrick J. Moran writes:

> I should clarify a bit.  I have an initFOO() call for my FOO
> module, and it gets called just fine.  I can manually call
> "placement new" constructors on the static data members of my
> classes to initialize them, but it's awkward and a hack.  It's
> also not a complete solution because there may be other static
> objects that belong to libraries whose implementation I'm not
> familiar with, e.g., iostreams.  There's got to be a better way.
> My understanding is that there is an entry point,
> _DllMainCRTStartup, that the linker generates that calls the
> necessary C++ constructors, and then calls DllMain.  

It's a routine in the C runtime, not in your DLL.

> So, the good
> news is that I could call _DllMinCRTStartup and the appropriate
> construction code would be called, assuming I figure out the
> arguments required by _DllMainCRTStartup. 

I don't think you can call it.

> The bad news is that
> _DllMainCRTStartup calls DllMain, the entry point provided by the
> user (or the Python library, I presume).  

DllMain can be provided by the dll author. If he/she doesn't 
provide one, a dummy one will be created.

> DllMain in turn calls
> my initFOO (again, I presume) so I would be in an infinite loop. 
> Of course I could think of hacks to prevent the infinite looping.
>  Still, there's got to be a better way.

Nope. DllMain (the default one) does nothing. If you write one, 
it's generally to do things like set up TLS (which you probably 
_don't_ want to do with Python).
> The solution that I'm looking for is to get Python to call the
> constructor code when it does the dynamic linking.  I think this
> means calling _DllMainCRTStartup rather than DllMain.  I've tried
> looking through the doc in the PC directory, but it's still
> unclear to me.  That's what I was looking for help with.

It's not Python's responsibility, it's the runtime library.

I haven't tried it in a couple years, but I was never successful 
in getting static initializers to work with DLLs. In fact, I had a 
lot of problems with them in normal MSVC apps, so I got out 
of the habit of using them. I just "new" them on startup.

- Gordon

More information about the Python-list mailing list