Nielsen's Weblog : .NET [use your Context dude]
Updated: 29-03-2004; 23:54:57.

 

Subscribe to "Nielsen's Weblog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

10. februar 2004

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    comment []

© Copyright 2004 Allan Nielsen.



Click here to visit the Radio UserLand website.

 


February 2004
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29            
Jan   Mar