user7698505
user7698505

Reputation:

Multiple DLLMain functions

I have a C++ program that has a DLLMain function that runs some code when this program (which is actually a DLL) is loaded by another one. This is working fine.

Now, when I compile this same C++ program this time including an .obj file (made from C code) that already has a DLLMain function defined, the compiler (in this case, VS2010 for Windows) complains that the DLLMain is already defined.

Without using the linker flag /FORCE:MULTIPLE how can one have both DLLMain functions (defined in the C and the C++ programs) coexisting and running in the same final DLL?

FYI, in GCC for Linux using static void __attribute__((constructor)) _my_initializer(void) works fine (i.e. both initializers can coexist in the same final .so file and run).

Upvotes: 0

Views: 839

Answers (2)

SoronelHaetir
SoronelHaetir

Reputation: 15172

If you cannot change the C source create a second DLL project and have your C++-based DLL depend on that second library. Each DLL can have a DllMain of its own. You could even use forwarding exports so that it appears that your C++ DLL exports the symbols from the secondary library.

A forwarding export is done with the following in your library's .DEF file:

EXPORTS
EntryName=realldll.RealEntryName

Upvotes: 0

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32727

Rename one of the DLLMain functions to something else (I'll call it CDLLMain), then call that renamed function from the other DLLMain. Care must be taken when deciding where to make the call to CDLLMain (first thing in DLLMain, last before returning (if safe to do so), or somewhere in the middle), in handling the return value from it (can you continue in DLLMain or do you need to return), and potential duplicated effort in the two DLLMain functions.

Upvotes: 1

Related Questions