DllMain入口点DLL_PROCESS_DETACH

时间:2016-08-23 12:24:53

标签: c# c++ dllmain

我有一个c ++ dll,从c#进程调用...

[DllImport(@"My.dll", EntryPoint = "Function1", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool MyFunction(int id1, int id2, [MarshalAsAttribute(UnmanagedType.LPWStr)] StringBuilder Name);

这是dll入口点......

switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        std::cout << "Attach" << std::endl;
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        std::cout << "Detach" << std::endl;
        break;
    }

我得到一个附件和四个分离像这样...

附加 分离 分离 分离 分离

任何人都可以解释原因吗?

该过程仅对该函数进行一次调用。

1 个答案:

答案 0 :(得分:3)

case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
    std::cout << "Detach" << std::endl;
    break;

DLL_THREAD_ATTACHDLL_THREAD_DETACHDLL_PROCESS_DETACH被触发时,您的代码将在控制台中写入“分离”。如果您只想在DLL_PROCESS_DETACH中添加breakswitch语句:

switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
    std::cout << "Attach" << std::endl;
    break;
case DLL_THREAD_ATTACH:
    break;
case DLL_THREAD_DETACH:
    break;
case DLL_PROCESS_DETACH:
    std::cout << "Detach" << std::endl;
    break;
}

或者只是省略未使用的案例。