Objective
A recent problem at work is that in order to use COM in. NET core across platforms, you cannot use the COM registration mechanism under Windows, but you can pass the IUnknown pointer directly to C #, convert to pointers, and then convert to C # 's interface (interface).
Did this research, but in the end I did not use this technology, because the distribution of idispatch::invoke is too cumbersome, and can not use the ATL and VS development environment IDL capabilities. So no further research on event subscriptions (C # is event,c++com is IConnectionPoint).
What you need to do in C + +:
The simple point, realizes the IDispatch to be possible, the comprehensive point, realizes IManagedObject or the IProvideClassInfo, the latter is a big project.
If we want to implement the interfaces defined in C #, then it is best to give (not give, the compiler will give each interface a default GUID) interface a guid,.net to your object QueryInterface to process the IID, and return the IDispatch pointer to the S_OK.
If you cross the platform, replace the __uuidof with the actual UUID.
struct Foo:public IDispatch {//through IDispatch inherits virtual ULONG AddRef (void) Override{return 0;}
Virtual ULONG release (void) Override{return 0;}
Virtual HRESULT QueryInterface (REFIID riid, void * * ppvobject) Override {if (riid = = __uuidof (IUnknown))
{*ppvobject = (iunknown*) this;
return S_OK;
The IID uid;
Iidfromstring (L "{C # declaration interface Guid/iid}", &uid);
if (riid = = uid) {*ppvobject = (idispatch*) this;//(iunknown*) this;
return S_OK;
} if (riid = = __uuidof (IDispatch)) {*ppvobject = (idispatch*) this;
return S_OK;
return E_NOTIMPL;
Virtual HRESULT gettypeinfocount (UINT * pctinfo) Override{return S_OK;}
Virtual HRESULT gettypeinfo (UINT itinfo, lcid lcid, ITypeInfo * * pptinfo) Override{return S_OK;
Virtual HRESULT GetIDsOfNames (REFIID riid, Lpolestr * rgsznames, UINT cnames, lcid lcid, DISPID * rgdispid) override { *rgdispid = 1;
return S_OK; Virtual HRESULT Invoke (DISPID dispidmember, REFIID riid, lcid lcid, WORD wflags, Dispparams * pdispparams, VARIANT *
Pvarresult, Excepinfo * pexcepinfo, UINT * puargerr) override {cout << "be called" << Endl;
return S_OK; }
};
Then you export a function of a DLL to the. NET Runtime
extern "C" __declspec (dllexport)
foo* winapi Gettestobject ()
{return
new foo;//simple rough leak:)
}
C # code:
[DllImport (@ "Foo.dll")]
static extern IntPtr gettestobject ();
[InterfaceType (Cominterfacetype.interfaceisidispatch)]
[Guid ("Your uiID")]
Interface Test
{
int func ();
}
var v = gettestobject ();
obj = (Test) marshal.getobjectforiunknown (v);
var value = Obj.func ();//Output be called
I Love COM
COM thinking is important, COM recently not only active in the Windows platform, but also spread to the Linux, Android, iOS and other platforms. Architects, programmers should make reasonable use of.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.