Structure of COM Development

Source: Internet
Author: User

In this example, the struct is transmitted as a parameter between COM and C #. The struct has two parameters: comid and comname,

Initialize the struct in C # and pass it to com. After COM is processed, it is returned to C #.

First open the firstcom Project (http://www.cnblogs.com/xiaokang088/archive/2011/05/16/2047376.html) in the previous example, follow these steps.

1. Open the firstcom. IDL file and add a struct as follows:

Struct firststruct

{

Int comid;

[String] wchar *
Comname;

};

2. In the above example, there is an interface definition, as shown below, add a method (Red is new)

[

Object,

UUID (645ee65a-27f6-406b-b2e8-7da-660fe0e8 ),

Dual,

Nonextensible,

Pointer_default (unique)

]

Interface ifirstclass: idispatch {

[ID (1)] hresult add ([in]
Long para1, [in] Long para2, [out, retval] long *
Result );

[ID (2)]
Hresult teststruct (struct firststruct * result );

};

3. Open the header file firstclass. H, find the public modifier, and add a method (red indicates the addition), as shown below:

Public:

Stdmethod (ADD) (long
Para1, long para2, long * result );

Stdmethod (teststruct) (struct
Firststruct * result );

};

4. Open the source file firstclass. cpp and see the method added in the previous example. below it, add the struct processing method (red indicates the addition)

// Firstclass. cpp: Implementation of cmycomserver

 

# Include "stdafx. H"

# Include "firstclass. H"

# Include <string>

 

// Firstclass

 

Stdmethodimp
Cfirstclass: add (long para1, long para2,
Long * result)

{

// Todo: add the implementation code here

* Result =
Para1 + para2;

Return s_ OK;

}

 

 

Stdmethodimp
Cfirstclass: teststruct (struct firststruct * result)

{

//
Todo: add the implementation code here

Result-> comid + = 10;

Result-> comname =
_ T ("docom ");

Return
S_ OK;

}

 

5. compile the project, open the WPF project, and add the following code to test it:

// Initialize the struct firstcomlib. firststruct = new firstcomlib. firststruct () {comname = "managemethod", comid = 100}; // test the firstclass struct. teststruct (ref firststruct); debug. writeline ("comid:" + firststruct. comid); debug. writeline ("comname:" + firststruct. comname );

The output is as follows:

Comid: 110

Comname: docom

 

Success ~!

 

 

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.