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 ~!