By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,637 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

Question on Calling VB6 Dll

P: n/a
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


Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <rc@hotmail.com> wrote in message
news:uA**************@TK2MSFTNGP12.phx.gbl...
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



Nov 17 '05 #2

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

"Dmytro Lapshyn [MVP]" <x-****@no-spam-please.hotpop.com> wrote in message
news:OP****************@TK2MSFTNGP14.phx.gbl...
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" <rc@hotmail.com> wrote in message
news:uA**************@TK2MSFTNGP12.phx.gbl...
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


Nov 17 '05 #3

P: n/a
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" <rc@hotmail.com> wrote in message
news:uh*************@TK2MSFTNGP10.phx.gbl...
oh, I forget to say. I 've already load the dll from a .NET assembly.

"Dmytro Lapshyn [MVP]" <x-****@no-spam-please.hotpop.com> wrote in message
news:OP****************@TK2MSFTNGP14.phx.gbl...
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" <rc@hotmail.com> wrote in message
news:uA**************@TK2MSFTNGP12.phx.gbl...
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




Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.