How can I obtain the returned parameter value when calling COM in. NET?

Source: Internet
Author: User

Author: Nan crazy Article Source: blog Park

When calling the COM Component Interface, we generally use the Type. InvokeMember () method to call it. Type. InvokeMember () requires an array of Object types to pass interface parameter information when calling. For interface parameters that only contain [in] Or ByVal, you only need to construct such an array and pass it to Type. InvokeMember.

If the interface parameters of the COM component contain [out] or [in, out] Return parameters (in the COM component developed by VB, It is ByRef), in addition to the above practices, some additional work is required. You must tell the parameters in the Type. InvokeMember () interface parameters which are a [in] [in, out] Or ByRef parameter. Otherwise, you will not get any response parameter values. To obtain the value of a parameter returned from [in] [in, out] Or ByRef, you must use the Type. InvokeMember () overload method containing the ParameterModifier array.

The ParameterModifier array only needs to contain one element. The ParameterModifier object has an index attribute called parameters called Item. In the called interface, if the nth parameter is a referenced parameter, the nth Item attribute must be assigned true to indicate the Type. invokeMember () is a referenced parameter.

The following is an example of a com api c # That contains three parameters (two of which are referenced parameters). In this example, both the second and third parameters are referenced parameters:

Type ComObjType;
Object ComObj;
String ReturnValue;

// Create a COM Object Reference
ComObjType = Type. GetTypeFromProgID ("SomeComServer. SomeComObject ");
ComObj = Activator. CreateInstance (ComObjType );

// Construct the parameter array for the InvokeMethod call and initialize each Parameter Element
Object [] ParamArray = new object [3];
ParamArray [0] = "InParam ";
ParamArray [1] = 5;
ParamArray [2] = "";
// Construct the ParameterModifier array (Note that there is only one element in the ParameterModifier array mentioned above)
// There are three parameters. Therefore, when creating a ParameterModifier object, you must specify the number of parameters in its constructor.
// Use the index attribute of the parameter to specify which parameters are a returned Parameter
// You do not need to specify parameters that are in or ByRef.
ParameterModifier [] parammod = new ParameterModifier [1];
Parammod [0] = new ParameterModifier (3); // number of parameters initialized as interface parameters
Parammod [0] [1] = true; // you can specify the second parameter as the return parameter.
Parammod [0] [2] = true; // you can specify the third parameter as the return parameter.
// Call the overload function containing the ParameterModifier Array
ReturnValue = (string) ComObjType. InvokeMember ("ReturnSomeValues", // interface function name
BindingFlags. Default | BindingFlags. InvokeMethod,
Null,
ComObj, // COM component called
ParamArray, // parameter Array
Parammod, // specify the ParameterModifier array of the returned Parameter
Null,
Null );

// Display the parameter value
Console. WriteLine ("Param1 = {0}", ParamArray [0]);
Console. WriteLine ("Param2 = {0}", ParamArray [1]);
Console. WriteLine ("Param3 = {0}", ParamArray [2]);
Console. WriteLine ("Return Value = {0}", ReturnValue );

Note: To pass the correct ParameterModifier array to InvokeMethod (), you must initialize the Data Type of the parameter in the array that accepts the returned parameters. In the preceding example, the second parameter is an integer [5] and the third parameter is text [].

 

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.