How C # invokes native C + + COM objects in detail _c language

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.