I am exploring calling .net methods from unmanaged C++ code and have found the function below in How To Inject a Managed .NET Assembly (DLL) Into Another Process
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();
}
This works with no problem when called once in a console application.
I now want to split this function for use within a dll, logically this should be in three parts
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
How and where do I declare the ICLRRuntimeHost pClrHost/HRESULT hr in order to achieve this?