我正在探索从非托管 C++ 代码调用 .net 方法,并在如何将托管 .NET 程序集 (DLL) 注入另一个进程中找到了以下函数
void StartTheDotNetRuntime()
{
// Bind to the CLR runtime..
ICLRRuntimeHost *pClrHost = NULL;
HRESULT hr = CorBindToRuntimeEx(
NULL, L"wks", 0, CLSID_CLRRuntimeHost,
IID_ICLRRuntimeHost, (PVOID*)&pClrHost);
// Push the CLR start button
hr = pClrHost->Start();
// Okay, the CLR is up and running in this (previously native) process.
// Now call a method on our managed class library.
DWORD dwRet = 0;
hr = pClrHost->ExecuteInDefaultAppDomain(
L"c:\\PathToYourManagedAssembly\\MyManagedAssembly.dll",
L"MyNamespace.MyClass", L"MyMethod", L"MyParameter", &dwRet);
// Stop the CLR runtime
hr = pClrHost->Stop();
// Don't forget to clean up.
pClrHost->Release();
}
在控制台应用程序中调用一次时,这没有问题。
我现在想将此函数拆分为在 dll 中使用,逻辑上这应该分为三个部分
Method - DLLMain
DLL_PROCESS_ATTACH
Bind to the CLR runtime
Push the CLR start button
DLL_PROCESS_DETACH
Stop the CLR runtime
Do not forget to clean up.
Method - CallDotNetToDoSomething
为了实现这一点,我如何以及在哪里声明 ICLRRuntimeHost pClrHost/HRESULT hr?