Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to PInvoke a function written in VB6 from C#. The VB6 code that calls the DLL is declared like this:
C#
Declare Function ApplyNNet Lib " location of the DLL file" (MyNNetType As String, MyAddress As String, MyInput() As Double) As Variant


My C# version looks like this:*
C#
[DllImport("NNetApply.dll")] 
public static extern object ApplyNNet(string type, string add, double[] data);

I call the function like this:
C#
object P_ = ApplyNNet("Back Prop", address,data);

With this declaration I receive a PInvoke error:
C#
PInvoke restriction: cannot return variants.
Posted
Updated 28-Jan-13 8:00am
v2
Comments
Sergey Alexandrovich Kryukov 28-Jan-13 14:08pm    
Why? why?! If you are already use VB.NET, why all this trouble?
Why variants, one of the worst "inventions", ever?
—SA

1 solution

First, if you've got the VB6 code calling this .DLL, why are you calling the VB6 code from C#?? Just bypass the VB6 code and go straight to the .DLL!

Second, You can't call a VB6 function that returns Variant. There is no equivilent type in .NET. You have to declare the actual type being returned.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jan-13 16:47pm    
Of course, a 5. Pointless question and artificial problem, "I cannot use VB6 but I want"...
—SA
Fiona.fj 29-Jan-13 11:56am    
I dont know how to program with vb6, so i prefer to write my project in c#.net. and now this problem occurs and i dont know what should i do!
Dave Kreskowiak 29-Jan-13 12:57pm    
You said your VB6 code is calling a .DLL. I take it you're trying to call this very same .DLL with your C# code?? It's not very clear what you're doing.

Is this other .DLL also a VB6 .DLL?? Does the function you're trying to call return a Variant?? If so, you're screwed.
Fiona.fj 29-Jan-13 13:03pm    
No, I said my code is in C#, and I wrote a declaration of the function in vb6 that was in my dll's tutorial. this function returns Variant, yeah. so you mean I cannot do any thing?
Dave Kreskowiak 29-Jan-13 13:25pm    
If the function you're calling returns a Variant, you're screwed. You cannot call that function from managed code (C#, VB.NET, any other managed language).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900