. NET how to get the return parameter value when COM is invoked

Source: Internet
Author: User
Tags array arrays reference
When invoking the COM component interface, we typically use the Type.InvokeMember () method to invoke it. Type.InvokeMember () needs to accept an array of type object to pass the parameter information of an interface at the time of invocation. For those interface parameters that contain only [in] or ByVal, you just build the array and pass it to Type.InvokeMember ().

If the COM component's interface parameters contain [out] or [in,out] return parameters (ByRef in the COM component developed by VB), there is a need for extra work in addition to the above practice. You have to tell Type.InvokeMember () which parameters in the interface parameter are a [in][in,out] or ByRef parameter, otherwise you will not get any return parameter values. In order to get the value of [in][in,out] or ByRef return parameters, you need to use a type.invokemember () overload method that contains a parametermodifier array.

The ParameterModifier array only needs to contain one element, and the ParameterModifier object has an indexed property called the item. In the calling interface, if the nth argument is a referenced parameter, then the nth Item property must be assigned to True to tell Type.InvokeMember () that this is a referenced parameter.

The following is a C # example of an interface containing three parameters (of which two parameters are reference parameters) a COM, in which the second and third parameters are reference parameters:

Type Comobjtype;
Object Comobj;
String returnvalue;

Create a reference to a COM object
Comobjtype = Type.gettypefromprogid ("Somecomserver.somecomobject");
Comobj = Activator.CreateInstance (Comobjtype);

Constructs a parameter array for the InvokeMethod call and initializes each parameter element
object[] ParamArray = new Object[3];
ParamArray [0] = "Inparam";
PARAMARRAY[1] = 5;
PARAMARRAY[2] = "";
Building ParameterModifier Arrays (note that there is only one element in the ParameterModifier array above)
Here are three parameters. So when you create a ParameterModifier object, you have to indicate the number of parameters in its constructor
Use the Index property of the parameter to indicate which argument is a returned parameter
Parameters that are [in] or byref can be used without specifying
parametermodifier[] Parammods = new ParameterModifier [1];
Parammods[0] = new ParameterModifier (3); Number of initial interface parameters initialized
Parammods[0][1] = true; Set the second argument to return parameter
PARAMMODS[0][2] = true; Set third parameter to return parameter
Calling overloaded functions that contain parametermodifier arrays
returnvalue = (string) comobjtype.invokemember ("Returnsomevalues",//Interface function name
Bindingflags.default | BindingFlags.InvokeMethod,
Null
Comobj,//Call COM component
ParamArray,//parameter array
Parammods,//Specify ParameterModifier array of return parameters
Null
NULL);

Display the value of a parameter
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 that in order to pass the correct parametermodifier array to InvokeMethod (), you must initialize the data type of the parameter in the returned parameter array. In the example above, the second parameter is shaping [5], and the third argument is the text ['].

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.