VC中的__attribute __((析构函数))等效?

时间:2013-12-21 23:30:31

标签: c++ c visual-c++

我查看了__attribute__((constructor)) equivalent in VC?CRT Initialization,这对于特定于gcc的__attribute__((constructor))都有帮助。但是__attribute__((destructor))呢?是否有VC等价物?

1 个答案:

答案 0 :(得分:2)

如果您正在创建动态链接库,则可以让DllMain entry point处理此问题:

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;
}