Managed Dll aus nativem (C++) Prozess entladen
-
Hi Freunde,
ich habe mittels einer Bootstrap Dll eine managed Dll in einen Prozess injiziert und möchte diese wieder entladen (unload).
Unter C++ hat das ganze funktioniert, in dem ich viaFreeLibraryAndExitThread(g_hModule, 0);
den Thread verlassen habe.
Geht das gleiche genauso einfach unter C#? Ich muss doch sicherlich noch die AppDomain verlassen und aufräumen oder?mfg codekid
-
Einzelne Assemblies kannst Du nicht aus einer AppDomain entladen. Mit AppDomain.Unload werden aber alle Assemblies einer Domain entladen
AppDomain newAD = AppDomain.CreateDomain("myDomain"); newAD.Load("mydll"); ... AppDomain.Unload(newAD);
-
Das Problem ist, dass ich kein Handle zu der AppDomain in der C# Dll habe.
Die C# Dll wird über eine native C++ Dll durch diese Funktion geladen:
Die Parameter sind vorerst uninteressant.DLL_EXPORT HRESULT ImplantDotNetAssembly(wchar_t* lpCommand) { HRESULT hr; ICLRMetaHost *pMetaHost = NULL; ICLRRuntimeInfo *pRuntimeInfo = NULL; ICLRRuntimeHost *pClrRuntimeHost = NULL; // build runtime hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&pMetaHost)); hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&pRuntimeInfo)); hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pClrRuntimeHost)); // start runtime hr = pClrRuntimeHost->Start(); // parse the arguments ClrArgs args(lpCommand); // execute managed assembly DWORD pReturnValue; hr = pClrRuntimeHost->ExecuteInDefaultAppDomain( args.pwzAssemblyPath.c_str(), args.pwzTypeName.c_str(), args.pwzMethodName.c_str(), args.pwzArgument.c_str(), &pReturnValue); // (optional) unload the .net runtime; note it cannot be restarted if stopped without restarting the process hr = pClrRuntimeHost->Stop(); // free resources pMetaHost->Release(); pRuntimeInfo->Release(); pClrRuntimeHost->Release(); return hr; }
Nachdem die Funktion "pwzMethodName" der C# Dll durchlaufen wurde, ist die Dll leider noch immer im Zielprozess.
-
µ schrieb:
Mit AppDomain.Unload werden aber alle Assemblies einer Domain entladen
Was ich auch noch nie hinbekommen habe.
-
Die Default AppDomain kann man nicht entladen. Und daher können auch niemals Assemblies entladen werden die in die Default AppDomain geladen wurden.
=> Managed Bootstrapper Assembly machen die eine neue AppDomain erstellt, und dann die "eigentliche" Assembly in dieser AppDomain ausführen lässt (ohne die "eigentliche" Assembly selbst zu laden natürlich). Diese neue AppDomain kann dann später wieder entladen werden.