我试图将托管的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"", ¤t_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;
}