When calling the COM component interface, we generally use the Type.InvokeMember() method. Type.InvokeMember() needs to accept an array of type Object to pass the parameter information of the interface when it is called. 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 (ByRef in COM components developed in VB), then in addition to the above method, some additional work must be done. You must tell Type.InvokeMember() which parameters in the interface parameters are [in][in,out] or ByRef parameters, otherwise, you will not get any return parameter value. In order to get the value of the [in][in,out] or ByRef return parameter, you have to use the Type.InvokeMember() overloaded method that contains the ParameterModifier array.
The ParameterModifier array only needs to contain one element. The ParameterModifier object has an index property called Item for the calling parameter. In the calling interface, if the Nth parameter is a reference parameter, then the Nth Item property must be assigned a value of true to tell Type.InvokeMember() that this is a reference parameter.
The following is a C# example of calling a COM interface with three parameters (two of which are reference parameters). In this example, the second parameter and the third parameter are both reference parameters:
type ComObjType;
object ComObj;
string ReturnValue;
//Create a reference to the COM object
ComObjType = Type.GetTypeFromProgID("SomeComServer.SomeComObject");
ComObj = Activator.CreateInstance(ComObjType);
//Construct a parameter array for the call to InvokeMethod 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 here. Therefore, when creating a ParameterModifier object, you must indicate the number of parameters in its constructor.
//Use the index attribute of the parameter to indicate which parameter is a returned parameter
//There is no need to specify parameters that are [in] or ByRef.
ParameterModifier[] ParamMods = new ParameterModifier [1];
ParamMods[0] = new ParameterModifier (3); // Initialized to the number of interface parameters
ParamMods[0][1] = true; // Set the second parameter as the return parameter
ParamMods[0][2] = true; //Set the third parameter as the return parameter
//Call the overloaded function containing ParameterModifier array
ReturnValue = (string) ComObjType.InvokeMember("ReturnSomeValues", //Interface function name BindingFlags.Default | BindingFlags.InvokeMethod,
null,
ComObj, // The called COM component ParamArray, // Parameter array ParamMods, // ParameterModifier array specifying the return parameter null,
null);
//Display the value of the 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 parameter array that accepts the return. In the above example, the second parameter is an integer [5], and the third parameter is a text [''].