Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C# : Passing a VARIANT by ref to a COM object

727 views
Skip to first unread message

Hermann Angstl

unread,
Dec 5, 2002, 4:49:48 PM12/5/02
to
Hi!

i'm trying to call a COM object with InvokeMember(). For simple cases it
work's fine.

But i have no idea how this would work with an "[out] VARIANT* "
parameter.

How is it possible in C# to pass an Array by reference to a COM object ?

I attached part of the typelib and the C# code (the affected variable is
named "quantityTotal").

Any ideas ?

thanx,
hal


----- Type Lib -----

[
uuid(06688AF2-07BE-11D7-AABF-00D0B7B2802B),
helpstring("Dispatch Interface")
]
dispinterface MyApplication {
properties:
methods:
...

[id(0x00000010)]
short function10 (
[in] BSTR equipmentName,
[out] long* recnum,
[out] VARIANT* quantityTotal );

...
};


----- C# Code -----

using System;
using System.Reflection;
using System.Collections;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{

class Class1
{

static void Main(string[] args)
{

Type objTypeMyApp = Type.GetTypeFromProgID("OLEDemo");
object objMyAppLateBound = Activator.CreateInstance(objTypeMyApp);

object myAppObj = objTypeAirline.InvokeMember("MyApplication",
BindingFlags.Default | BindingFlags.GetProperty,
null,
objAirlineLateBound,
new object[]{});


System.Int32 recnum = new System.Int32();
... quantityTotal = ... // HOW TO DEFINE THIS ???????

ParameterModifier pm1 = new ParameterModifier(2);
pm1[0] = true;
pm1[1] = false;

ParameterModifier pm2 = new ParameterModifier(2);
pm2[0] = false;
pm2[1] = true;

ParameterModifier pm3 = new ParameterModifier(2);
pm3[0] = false;
pm3[1] = true;

object[] arrayInputEq = { "Equipment_01", recnum, quantityTotal };

object funcObj = myAppObj.GetType().InvokeMember("function10",
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
myAppObj,
arrayInputEq, new ParameterModifier[]{pm1, pm2, pm3}, null, null );

Console.ReadLine();
}
}
}


Jacob Yang

unread,
Dec 10, 2002, 12:33:06 AM12/10/02
to
Hi Hermann,

Based on my research and experience, I think that the following Knowledge
Base article is useful to you.

http://support.microsoft.com/default.aspx?scid=KB;en-us;317030&

Thanks,

Jacob Yang
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. ? 2002 Microsoft Corporation. All rights
reserved

Jacob Yang

unread,
Dec 10, 2002, 12:47:58 AM12/10/02
to
Hi Hermann,

Please also refer to the following URL.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconarrayssample.asp

António

unread,
Dec 11, 2002, 5:53:45 PM12/11/02
to
I'm having a similar problem, and i haven't worked it out.

In my case, i'm trying to get back a BSTR * (byref string
in VB) from a COM object.

Question:
How can i pass a string by reference from c# to a VB 6
object (byref string) (and get back the value), using
InvokeMember()?

Thanks.

>.
>

Jacob Yang

unread,
Dec 11, 2002, 8:01:37 PM12/11/02
to
Hi,

I hope that the following document is useful.

"When making late-bound calls using Reflection, you first obtain an
interface reference, and then you use Type.InvokeMember() to make method
calls. InvokeMember() accepts an object array containing the parameter
values. For methods that only have [in] or ByVal parameters, you create
the array, and pass it to InvokeMember().


If the COM object method has [out], or [in,out] parameters (ByRef
parameters for VB users), additional work must be done. You must tell the
Type.InvokeMember function that a paramter is a ByRef parameter. If you
don't, no data will be returned through that parameter. To get data back
through an [out] or ByRef parameter, you have to use the version of
Type.InvokeMember that allows you to pass a ParameterModifier array. The
ParameterModifier array should contain a single ParameterModifier element.
The ParameterModifier object has a indexer property called Item. The Nth
Item should be set to 'true' to indicate that the Nth parameter should be
passed by reference. Here is a C# code fragment that calls a COM method
that has 3 parameters and a return value. The second and third parameters
are out, or ByRef parameters


Type ComObjType ;
object ComObj ;
string ReturnValue ;

// create the instance of the object
ComObjType = Type.GetTypeFromProgID("SomeComServer.SomeComObject");
ComObj = Activator.CreateInstance(ComObjType) ;

// Set up the parameter array for the InvokeMethod call. Initialize each
element.
object[] ParamArray = new object[3];
ParamArray [0] = "InParam" ;
ParamArray[1] = 5 ;
ParamArray[2] = "" ;

// Set up the ParameterModifier array. There are 3 parameters. Note that
// the ParameterModifier constructor is given the number of parameters
// in the method. The indexer is used to specify which parameters are
// out parameters. This is not necessary if the method only has [in], or
// ByVal parameters
ParameterModifier[] ParamMods = new ParameterModifier [1];
ParamMods[0] = new ParameterModifier (3); // inititialize with number of
method parameters
ParamMods[0][1] = true; // set the 2nd param to be an out param
ParamMods[0][2] = true; // set the 3rd param to be an out param

// Call the version of InvokeMember that accepts a ParameterModifier array.
ReturnValue = (string) ComObjType.InvokeMember(
"ReturnSomeValues", // method name
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
ComObj, // obj being called
ParamArray, // argument list
ParamMods, // ParameterModifier array specifying out params
null,
null) ;

//Display the 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 that in addition to passing a correctly initialized ParameterModifier
array to the InvokeMethod() function, you must also be sure to intialize
the elements in the parameter array with values that are of the correct
type.

António Mendes Silva

unread,
Dec 12, 2002, 5:49:35 AM12/12/02
to
It worked!.

Thank you very much.

(i'm really gratefull. You know that feeling, when you
spend hours trying to solve a problem, and them someone
else comes along and helps you... That's how grateful i
am).

>.
>

0 new messages