As so many others I need to host the CLR in an unmanaged application written in VC++ 6.0 (sigh). Anyway using ICorRuntimeHost and CorBindToRuntimeEx is one way to host the CLR. There is another hosting API and it's called ClrCreateManagedInstance. You call this undocumented function if you want to create an instance of a managed class directly in your unmanaged app.
The ClrCreateManaged instance takes a fully qualified name of your assembly....remember that, otherwise you get HRESULT 0x80070002 which is "The system cannot find the file specified". Do your self a favor an place your assembly in the GAC.
so here is a <grin>complex</grin> example on how to create an instance of SomeClass, you need to include the mscoree.h header file and link with mscoree.lib.
CComPtr<IDispatch> pDisp; HRESULT hr = S_OK; hr = ClrCreateManagedInstance(L"namespace.aClass,Assembly, Version=1.5.0.1, Culture=neutral, PublicKeyToken=f09ae53443459c4e",IID_IDispatch, (void**)&pDisp);
OLECHAR FAR* szMember = L"Hallo"; //call the Hallo method DISPID dispid; DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
//Retrieve the DISPID pDisp->GetIDsOfNames (IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
//Invoke the method on the Dispatch Interface pDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispparamsNoArgs, NULL, NULL, NULL);
it's undocumented, so putting this in production code seems like a good idea :)
Note: if the namespace.class you are trying to create an instance of in your unmanaged code also has dependencies on other assemblies or uses com-interop, remember to place them in the GAC also, otherwise you will get the 0x80070002.
"Play fair. Don't hit people. Say you're sorry when you hurt somebody." --Robert Fulghum'
12:49:58 PM
|