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 those that only contain [in] Or byval
Interface Parameters. 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 oneIndex of the called ParameterThe property is 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:
T Ype comobjtype;
Object Comobj;
String Returnvalue;
// Create COM Object Reference
Comobjtype = Type. gettypefromprogid ( " Somecomserver. somecomobject " );
Comobj = Activator. createinstance (comobjtype );
// Construct a 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.
// The index attribute of the parameter is used to indicate 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 ; // Set the second parameter to the return parameter.
Parammod [ 0 ] [ 2 ] = True ; // Set 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, // Parametermodifier array of the returned Parameters
Null ,
Null );
// Show parameter values
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 [''].