ICLRRuntimeHost :: UnloadAppDomain失败

时间:2018-01-14 14:43:41

标签: c++ c++-cli

我试图将托管的c ++ / cli dll加载到本机进程中,所以我创建了一个启动clr并启动clr并调用托管dll的bootstrap dll,一切正常但我无法杀死/卸载clr这是代码。 InitCLR和FinitCLR由注入器调用,对UnloadAppDomain的调用返回0x80131015,而GetAppdomainId托管函数是一个返回AppDomain::CurrentDomain->Id;的函数

auto StartCLR(std::wstring clr_version) -> ICLRRuntimeHost * {
    ICLRMetaHost *meta_host = nullptr;
    ICLRRuntimeInfo *runtime_info = nullptr;
    ICLRRuntimeHost *runtime_host = nullptr;

    if (CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, reinterpret_cast<LPVOID *>(&meta_host)) == S_OK) {
        if (meta_host->GetRuntime(clr_version.c_str(), IID_PPV_ARGS(&runtime_info)) == S_OK) {
            BOOL loadable = false;
            if (runtime_info->IsLoadable(&loadable) == S_OK && loadable) {
                if (runtime_info->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&runtime_host)) == S_OK) {
                    return runtime_host;
                }
            }
        }
    }

    if (meta_host) {
        meta_host->Release();
    }

    if (runtime_info) {
        runtime_info->Release();
    }

    if (runtime_host) {
        runtime_host->Release();
    }
    return nullptr;
}

extern "C" __declspec(dllexport) int InitCLR() {
    if (pCLR) {
        MessageBoxA(NULL, "CLR already initialized", "", NULL);
        return 1;
    }

    pCLR = StartCLR(L"v4.0.30319");
    if (!pCLR) {
        MessageBoxA(NULL, "StartCLR failed", "", NULL);
        return 1;
    } else {
        pCLR->Start();
    }

    DWORD result = 0;
    auto err = pCLR->ExecuteInDefaultAppDomain(managed_DLL_PATH.c_str(), L"Managed.Helpers", L"Say", L"Hello", &result);
    if (err != S_OK) {
        MessageBoxA(NULL, "ExecuteInDefaultAppDomain failed", "", NULL);
        return 1;

    }

    return 0;
}

extern "C" __declspec(dllexport) int FinitCLR() {
    if (!pCLR) {
        MessageBoxA(NULL, "CLR is noy initialized", "", NULL);
        return 1;
    }

    DWORD current_appdomain_id = 0;
    auto err = pCLR->ExecuteInDefaultAppDomain(managed_DLL_PATH.c_str(), L"Managed.Helpers", L"GetAppdomainId", L"", &current_appdomain_id);
    if (err != S_OK) {
        MessageBoxA(NULL, "ExecuteInDefaultAppDomain(GetAppdomainId) failed", "", NULL);
        return 1;
    }

    err = pCLR->UnloadAppDomain(current_appdomain_id, true);
    if (err != S_OK) {
        MessageBoxA(NULL, "UnloadAppDomain failed", "", NULL);
        return 1;
    }

    pCLR->Stop();
    pCLR->Release();
    pCLR = nullptr;

    return 0;
}

1 个答案:

答案 0 :(得分:-1)

不幸的是,无法卸载重要部分中的here

  

此方法不会将资源释放到主机,卸载应用程序域或破坏线程。您必须终止该过程以释放这些资源。