C # Interop (i) writing a C + + COM component

Source: Internet
Author: User

How C # interacts with COM components written by C + +, first writing a C + + ATL COM component, opening vs, creating a new ATL project, naming the project COMServer, and choosing dynamic Link in Application setting Library (dynamic link libraries). After creating the project, right-click on the project, add-class, select ATL Tag, select ATL Simple Object (ATL Easy objects), in the dialog box simply fill in the Comdemo, interface fill Iwelcome,progid fill in Comserver.comdemo

Then, under Class View, select Interface Iwelcome and add a method using the following parameters greeting

HRESULT greeting ([in] BSTR name, [out, retval] bstr* message);

IDL file Comserver.idl defines the interface for COM, and the code generated by the wizard in the Comserver.idl file should resemble the following, with a unique identifier (UUID) that differs. The Iwelcome interface defines the greeting method, and the brackets before the keyword interface define some of the features of the interface. The UUID defines the amount of interface id,dual marked with the interface type.

[    Object,    uuid (4704a15d-cde1-4bf1-a28e-e1477e7c74b8),    dual,    nonextensible,    pointer_default (unique)]interface  iwelcome:idispatch{    [ID (1)] HRESULT greeting ([in] BSTR name, [out, retval] bstr* message);};

The IDL file also defines the content of the type library, which is a COM object that implements the Iwelcome interface (coclass)

[    uuid (fbb80a75-06e1-452b-88ba-f7b00ed151a9),    version (1.0),]library comserverlib{    [        uuid (85567ACE-7031-4246-9e81-ca5aa470f212),     ]    CoClass Comdemo    {        [defaultinterface  iwelcome;     };};

On the head of the Iwelcome interface, use the above identifier and name Learning.Interop.Server.IWelcome to add the custom attribute. Use the corresponding name in the coclass Comdemo the same custom attribute as the day boundary:

[    Object, uuid (4704a15d-cde1-4bf1-a28e-e1477e7c74b8), dual, nonextensible, pointer_default (unique), custom (67412b27-B0FB-425A-A7D2-DAB66BD6768B,"Learning.Interop.Server.IWelcome")]Interfaceiwelcome:idispatch{[ID (1)] HRESULT Greeting ([inch] BSTR name, [ out, retval] bstr*message);}; [UUID (Fbb80a75-06e1-452b-88ba-f7b00ed151a9), version (1.0),]library comserverlib{[uuid (85567ACE-7031-4246-9e81-ca5aa470f212), helpstring ("Comdemo Class"), Custom (67412b27-B0FB-425A-A7D2-DAB66BD6768B,"Learning.Interop.Server.COMDemo")] coclass Comdemo {[default]InterfaceIwelcome; };};

You can now add an interface to the Comserver.idl file to copy the Iwelcome interface header directly to the head of the newly created IMath interface, but remember to modify the unique identifier of the UUID keyword definition.

The IMath interface provides two methods of Add,sub:

//IMath[    Object, uuid (2158751B-b96e-461d-9012-ef1680be0628), dual, nonextensible, pointer_default (unique), custom (67412b27-B0FB-425A-A7D2-DAB66BD6768B,"Learning.Interop.Server.IMath")]Interfaceimath:idispatch{[ID (1)] HRESULT ADD ([inch] LONG Val1, [inch] LONG Val2, [ out, retval] long*result); [ID (2)] HRESULT Sub ([inch] LONG Val1, [inch] LONG Val2, [ out, retval] long*result);};

You must also modify the coclass Comdemo so that it implements both interface Iwelcome and IMath. The Iwelcome interface is set as the default interface:

 [uuid (85567ACE -7031 -4246  -9e81-ca5aa470f212), helpstring (  " comdemo Class  "  ), custom (67412b27 -b0fb-425a-a7d2-dab66bd6768b,  " learning.interop.server.comdemo    default ] interface      Iwelcome;  interface   IMath;};  

You can now transfer your attention from the IDL file to the C + + code. The COMDemo.h file contains the definition of a COM object. Class Comdemo uses multiple inheritance derived from the template class Ccomobjectrootex,ccomcoclass and Idispatchimpl.ccomobjectrootex classes to implement the functionality of the Iunknow interface. Examples include the AddRef and release methods. The CComCoClass class creates a factory instantiation template parameter object, which is Ccomdemo. Idispatchimple implements the method of IDISPATHC interface.

Macros included in Begin_com_map and End_com_map create a mapping that defines all COM interfaces implemented by the COM class. The implementation of the Querinterface method uses this mapping

classatl_no_vtable Ccomdemo: PublicCcomobjectrootex<ccomsinglethreadmodel>,     PublicCcomcoclass<ccomdemo, &CLSID_COMDemo>,     PublicIdispatchimpl<iwelcome, &iid_iwelcome, &libid_comserverlib,/*wmajor =*/ 1,/*Wminor =*/ 0>,{ Public: Ccomdemo () {}declare_registry_resourceid (idr_comdemo1) begin_com_map (Ccomdemo) com_interface_entry (IWel Come) Com_interface_entry2 (idispatch,iwelcome) End_com_map () declare_protect_final_construct () HRESULT FinalConstr UCT () {returnS_OK; }    voidfinalrelease () {} Public: Stdmethod (greeting) (BSTR name, BSTR*message);};o Bject_entry_auto (__uuidof (Comdemo), Ccomdemo)

In this class definition, you also need to add a second interface, IMath, and the methods defined by the IMath interface:

classatl_no_vtable Ccomdemo: PublicCcomobjectrootex<ccomsinglethreadmodel>,     PublicCcomcoclass<ccomdemo, &CLSID_COMDemo>,Public   idispatchimpl<iwelcome, &iid_iwelcome, &libid_comserverlib,/ *wmajor =  */ 1,/ *wminor =* / 0> ,   public Idispatchimpl<imath, &iid_imath, &libid_comserverlib, 1, C14>0>,{ Public: Ccomdemo () {}declare_registry_resourceid (idr_comdemo1) begin_com_map (Ccomdemo) com_interface_entry (IWELC ome) com_interface_entry (IMath) com_interface_entry2 (idispatch,iwelcome) end_com_map () Declare_protect_fi Nal_construct () HRESULT finalconstruct () {returnS_OK; }    voidfinalrelease () {} Public: Stdmethod (greeting) (BSTR name, BSTR*message); stdmethod (ADD) (Long val1, long val2, long * result); STDMETHOD (Sub) (Long val1, long val2, long* result);};o Bject_entry_auto (__uuidof (Comdemo), Ccomdemo)

Now you can focus on COMDemo.cpp, implement the method declared in the header file in the CPP file:

STDMETHODIMP ccomdemo::greeting (BSTR name, bstr*message) {    //TODO: Add implementation code hereCComBSTR TMP ("Welcome,"); Tmp.    Append (name); *message =tmp; returnS_OK;} STDMETHODIMP Ccomdemo::add (Long val1, long val2, long*result) {    *result = Val1 +Val1;    Fire_completed (); returnS_OK;} STDMETHODIMP Ccomdemo::sub (Long val1, long val2, long*result) {    *result = Val1-Val2;    Fire_completed (); returnS_OK;}

Now it's time to build the component. The build process configures the components in the registry and requires administrator status, so be sure to run vs as an administrator.

C # Interop (i) writing a C + + COM component

Related Article

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.