C# – Converting dllimport from vb6 to c# 3.5

c++dllimportinteropvb6

I have some code in VB6 which imports a function from a dll, it uses the byVal and byRef keywords, I want to convert that code into C# 3.5.

  1. Is there going to be a problem with unicode encoding of the strings?

  2. Do I declare the varibles that are "byRef" in vb6 into "ref" varibles in the C# code?

  3. It seams that return value is entered into a string send by the VB6 code as a "byVal" parameter, how does that work, aren't you supposed to send stuff "byRef", if you want to allow the function to edit the string? Is this concept still going to work with my C# code?

I tryed coping the function declaration from VB6, the parameter types are just int's, long's and string. Where there was a "byVal" keyword I just left it empty and replaced "byRef" keywords with the "ref" keyword in C# and the code dosn't work.

The VB6 Code:

Private Declare Function Foo Lib "Foo_Functions.dll" (ByVal a as String, ByVal b
as Long, ByVal c as String, ByVal d as String, ByVal e as String, ByVal f
as String, ByVal g as Long, ByVal h as String, ByVal i as String, ByRef j
as Long, ByRef k as Long) As
Integer

My C# 3.5 Translation:

[Dllimkport("foo_functions.dll")] public static extern int foo(String a, long b,
string c, string d, string e, string f, long g, string h, stringbuilder i,
ref long j, ref long k);

Please help, I'v already spent a whole day on this :p….

In the end I converted the function call to a VB.NET lib, using the automatic project converter (from VB6 to VB.NET 2008), and called it using C# reference.

Thanks.

Best Solution

have a look at www.pinvoke.net It shows C# and VB.net examples.

Related Question