Hi,
>>I can't use ClrCreateManagedInstance() since that would be used to
create COM objects in a managed DLL, and that is what ><I am trying to
avoid. (This assumes I understand the signature of ClrCreateManagedInstance
()).
small modification on that one :

you use this undocumented api to create an instance of a managed class
directly in your unmanaged code.

The ClrCreateManagedinstance 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.

...example on how to create an instance of SomeClass, you need to include
the mscoree.h header file.


 CComPtr<IDispatch> pDisp;
 HRESULT hr = S_OK;
 hr = ClrCreateManagedInstance(L"Namespace.SomeClass,Namespace,
Version=1.5.0.1, Culture=neutral,
PublicKeyToken=f09ae53443459c4e",IID_IDispatch, (void**)&pDisp);

 OLECHAR FAR* szMember = L"Hallo"; //call the Hallo method, I know this is
complex stuff but bare with me here.
 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 as Shawn said, 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.

cheers, Allan

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to