Thursday, January 29, 2009

Tricky C code

We have a static library project linked into a DLL. For years, this static library does not require initialization/unitialization code. This time a new feature is added that requires the initialization and cleanup. So we have to call functions when the DLL is attached/detached to the process.

The original DLL does not have a DllMain. We got one by having VC++ create for us, and it did. We did this by creating a new DLL project and copy the function into ours:


BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
}
return TRUE;
}


It was easy to paste code. Now we invoke functions:


case DLL_PROCESS_ATTACH:
InitMyLib();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
TerminateMyLib();
break;



Unfortunately did not work as we expected. Does anyone see the problem?

No comments: