Dear Willy,
The problem bother me badly,Now it seems that only
you can save me.
My COM DLL was developed in VB6 and I need to use it
in .NET . so here I test the simple COM DLL Wrote by VB6
rather than VC.Following your guidance,the error still
occurs,the exception
is "System.Reflection.TargetInvocationException in
mscorlib.dll".Now I list my code.
----------------------------------------
----------------------------------------
the tested COM DLL in VB6,
ProgID is: MySimpleTest.TestClass
method code:
-----------
Public Function mymethod(ByVal a As Integer, ByVal b As
Integer, ByRef c As Integer) As Integer
mymethod = a + b
c = a - b
End Function
----------------------------------------
----------------------------------------
the calling code in c# is:
-------------------------
{
int i1 = 15;
int i2 = 6;
int i3 = 3;
object[] args ={i1,i2,i3};
MessageBox.Show(args[2].ToString(), "value of args[2]",
MessageBoxButtons.OK, MessageBoxIcon.None);
Type T=Type.GetTypeFromProgID("MySimpleTest.TestClass");
object obj = Activator.CreateInstance(T);
string methodName = "mymethod";
ParameterModifier pm = new ParameterModifier(3);
pm[0] = false;
pm[1] = false;
pm[2] = true;
ParameterModifier [] mods = { pm };
object retVal=obj.GetType().InvokeMember
(methodName,BindingFlags.InvokeMethod, null, obj , args,
mods, null, null); // error occurs!
MessageBox.Show(retVal.ToString(), "result",
MessageBoxButtons.OK, MessageBoxIcon.None);
MessageBox.Show(args[2].ToString(), "value of args[0]",
MessageBoxButtons.OK, MessageBoxIcon.None);
}
----------------------------------------------------
----------------------------------------------------
Excuting the calling,error still occurs.
Would you please test a VB6 DLL ?
I ask for your help.
I'm sorry to bother you again.
Mike.
-----Original Message-----
Mike,
That's what I did
Here's my code:
<C# snippet>
int i1 = 15;
int i2 = 6;
int i3 = 3;
Type type = Type.GetTypeFromProgID
("simple.Tester"); // ProgID
object obj = Activator.CreateInstance(type);
string methodName = "MyMethod";
object[] args = new object[3];
args[0] = i1;
args[1] = i2;
args[2] = i3;
// Modify third arg. and pass it byref
ParameterModifier pm = new ParameterModifier(3);
pm[0] = false; // LONG
pm[1] = false; // LONG
pm[2] = true; // byref (type is LONG*)
ParameterModifier [] mods = { pm };
object retVal = obj.GetType().InvokeMember(methodName,
BindingFlags.InvokeMethod, null, obj , args, mods, null, null);
Console.WriteLine("Values : retval={0} i3= {1}",retVal, args[2]);
.....
</end of C#>
Here's the IDL (COM object written in C++)
....
[
object,
uuid(EA461F01-8209-41CB-869C-111220E83CB3),
dual,
helpstring("ITester Interface"),
pointer_default(unique)
]
interface ITester : IDispatch {
[id(1),helpstring("method MyMethod")] HRESULT MyMethod ([in]LONG i1,
[in]LONG i2, [in,out] LONG *i3, [out,retval] LONG *SomeReturn);
};
Here's the output:
Values : retval=21 i3=9
What exception is thrown on you?
Willy.
hello,Willy,
I followed your guidance,but exception happened.
My calling code in c# is as folloow:
object [] args={12, 3, 5};
myTest.myclass obj=new heTest.myclass();
Type T=Type.GetTypeFromProgID("heTest.myclass");
ParameterModifier pm = new ParameterModifier(3);
pm[0] = false;
pm[1] = false;
pm[2] = true;
ParameterModifier [] pmods = { pm };
object d = T.InvokeMember("method",
BindingFlags.InvokeMethod, null, obj, args, pmods, null,
null);
Would you please select a COM dll and have a try by
yourself?
Thanks a lot.
-----Original Message-----
You have to pass the third arg by ref using a
ParameterModifier array.
Here is how you should do it in C#
ParameterModifier pm = new ParameterModifier(3);
pm[0] = false;
pm[1] = false;
pm[2] = true;
ParameterModifier [] pmods = { pm };
object retVal = obj.GetType().InvokeMember (methodName,
BindingFlags.InvokeMethod, null, obj , args, pmods,
null, null);
Willy.
.