Click to See Complete Forum and Search --> : pass VARIANT parameters by reference


Jiye An
January 25th, 2005, 07:01 PM
Recently, I met a problem when using ActiveX control in VB.NET.
The control is created by MFC, it provides a method which needs a
parameter of type VARIANT *, so the parameter must be passed by
reference. But under .NET, this will cause a
System.Runtime.InteropServices.COMException.

I have searched MSDN, and found that it is a bug.
http://support.microsoft.com/default.aspx?scid=kb;en-us;312910

The prototype of the method
MyMethod(para1 As Integer, para2 As Integer, ByRef para3 As Object)

The third parameter is of type VARIANT *. In VB 6.0, I can call
this method by passing a VARIANT. But in VB.NET, if I call the method like
this

Dim para1 As Integer
Dim para2 As Integer
Dim para3 As Object

MyControl.MyMethod(para1, para2, para3)

A COMException will occur.

According to the MSDN article, I re-wrote the VB.NET code to

Dim args(2) As Object

args(0) = para1
args(1) = para2
args(2) = para3


Dim c(0) As Reflection.ParameterModifier
c(0) = New Reflection.ParameterModifier(3)
c(0).Item(0) = False
c(0).Item(1) = False
c(0).Item(2) = True ‘ByRef

MyControl.GetType().InvokeMember(
"MyMethod",
Reflection.BindingFlags.InvokeMethod,
Nothing,
MyControl,
args,
c,
Nothing,
Nothing)

There is no COMException, but a TargetInvocationException occurs.
I am not familiar with .NET-COM Interop and VB.NET,
can anyone give me a suggestion how to solve this problem?

Thank you in advance.

An