Question on Calling VB6 Dll

R

RC

Hi,

I have a dll written by vb6.
AFunction(ByRef o as Variant)

I want to use it in csharp by System.Reflection.

Assembly asm = Assembly.LoadFrom("Lib.dll");
Type t = asm.GetTypes("Lib.CLib");
object o = Activator.CreateInstance(t);
object param = new string[] {"1", "2", "3", "4"};
MethodBase mth = t.GetMethods("AFunction");
mth.Invoke(o, new object[]{param}); <---Throw ArgumentException "Object type
cannot be converted to target type."

As I know the argument type is "System.Object&" in .NET, I think it could
pass the parameter by "AFunction(ref object o)". However the dll file is
loaded in runtime dynamically, so calling "AFunction(ref object o)" directly
should not be worked.

Anyone know how to convert VB6 array into CSharp array in my case ?
And How to call the VB6 Function in CSharp?

Regards,
RC
 
D

Dmytro Lapshyn [MVP]

Hi,

As far as I know, VB6 can create only COM dlls. Therefore, they must be
consumed as any other COM objects - through creating an interop assembly
with tlbimp.exe. Your code below indicates that you are trying to load a VB6
dll like a .NET assembly, that won't work at all.
 
R

RC

oh, I forget to say. I 've already load the dll from a .NET assembly.

Dmytro Lapshyn said:
Hi,

As far as I know, VB6 can create only COM dlls. Therefore, they must be
consumed as any other COM objects - through creating an interop assembly
with tlbimp.exe. Your code below indicates that you are trying to load a
VB6 dll like a .NET assembly, that won't work at all.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


RC said:
Hi,

I have a dll written by vb6.
AFunction(ByRef o as Variant)

I want to use it in csharp by System.Reflection.

Assembly asm = Assembly.LoadFrom("Lib.dll");
Type t = asm.GetTypes("Lib.CLib");
object o = Activator.CreateInstance(t);
object param = new string[] {"1", "2", "3", "4"};
MethodBase mth = t.GetMethods("AFunction");
mth.Invoke(o, new object[]{param}); <---Throw ArgumentException "Object
type cannot be converted to target type."

As I know the argument type is "System.Object&" in .NET, I think it
could pass the parameter by "AFunction(ref object o)". However the dll
file is loaded in runtime dynamically, so calling "AFunction(ref object
o)" directly should not be worked.

Anyone know how to convert VB6 array into CSharp array in my case ?
And How to call the VB6 Function in CSharp?

Regards,
RC
 
D

Dmytro Lapshyn [MVP]

RC,

MSDN reads on MethodInfo.Invoke:

"If the method or constructor represented by this instance takes a ByRef
parameter, there is no special attribute required for that parameter in
order to invoke the method or constructor using this function."

However, what looks suspicious is that you create an array of strings as the
value of the "param" - I'd try using "new object[] {...}" here as well.

You can also consider re-posting this question in the
microsoft.public.dotnet.framework.interop newsgroup.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


RC said:
oh, I forget to say. I 've already load the dll from a .NET assembly.

Dmytro Lapshyn said:
Hi,

As far as I know, VB6 can create only COM dlls. Therefore, they must be
consumed as any other COM objects - through creating an interop assembly
with tlbimp.exe. Your code below indicates that you are trying to load a
VB6 dll like a .NET assembly, that won't work at all.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


RC said:
Hi,

I have a dll written by vb6.
AFunction(ByRef o as Variant)

I want to use it in csharp by System.Reflection.

Assembly asm = Assembly.LoadFrom("Lib.dll");
Type t = asm.GetTypes("Lib.CLib");
object o = Activator.CreateInstance(t);
object param = new string[] {"1", "2", "3", "4"};
MethodBase mth = t.GetMethods("AFunction");
mth.Invoke(o, new object[]{param}); <---Throw ArgumentException "Object
type cannot be converted to target type."

As I know the argument type is "System.Object&" in .NET, I think it
could pass the parameter by "AFunction(ref object o)". However the dll
file is loaded in runtime dynamically, so calling "AFunction(ref object
o)" directly should not be worked.

Anyone know how to convert VB6 array into CSharp array in my case ?
And How to call the VB6 Function in CSharp?

Regards,
RC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top