I've looked at __attribute__((constructor)) equivalent in VC? and CRT Initialization, which were both helpful regarding the gcc-specific __attribute__((constructor))
. But what about __attribute__((destructor))
? Is there a VC equivalent?
Asked
Active
Viewed 1,666 times
4

Community
- 1
- 1

Nafis Zaman
- 1,505
- 13
- 16
-
the answer you linked gives an equivalent functionality, with the use of atexit See http://msdn.microsoft.com/en-us/library/tze57ck3.aspx – manuell Dec 21 '13 at 23:45
-
1In case of libraries - there is very little you can do safely in `DllMain` (read: call functions of Kernel32.dll as long as they are not loading libraries - and practically nothing else). Unfortunately other methods (`atexit` etc.) are not much better as they tend to be implemented via `DllMain`. In such cases the rewrite of code tend to be the easy option. – Maciej Piechotka Dec 22 '13 at 00:18
1 Answers
2
If you're making a dynamic link library, you can make your DllMain entry point handle this:
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
// equivalent of __attribute__((constructor))...
// return TRUE if succeeded, FALSE if you failed to initialize properly
return TRUE; // I'm assuming you succeeded.
}
else if (fdwReason == DLL_PROCESS_DETACH)
{
// equivalent of __attribute__((destructor))...
}
// Return value is ignored when fdwReason isn't DLL_PROCESS_ATTACH, so we'll
// just return TRUE.
return TRUE;
}

Cornstalks
- 37,137
- 18
- 79
- 144
-
Interesting and actually seems more clear than gcc's `__attribute__`. I'll heed the warnings about `DllMain` and see what I can do. Thanks! – Nafis Zaman Dec 23 '13 at 06:05