Fog Creek Software
Discussion Board

VB.NET and Strings

I Googled and still could not find enough help to resolve this problem.

I have a C++ DLL (I wrote it) and am using VB.NET to call some functions within it.  Some functions return strings within its paramter list, and VB then complains about it.  Getting a single character back from the function in the DLL works.  But trying to get a string (filled array) back fails.  And I don't know why?

Passing by reference didn't help.  And everything looks like it should work, but doesn't.

Any tips?

Thursday, June 3, 2004

You might have to use marshalling... I know I had to, in C#, when retrieving/sending strings from/to C++ dlls.

A Dingo Ate My Baby
Thursday, June 3, 2004

In your VB file where you declare the external C++ function, you must add a MarshalAs attribute (namespace System.Runtime.InteropServices).

I don't speak VB but here's a C# example:

public static extern void BlahFunction(
    string foo);

Chris Nahr
Thursday, June 3, 2004

Oh yeah, and specify the character set in the DllImport attribute, such as:

[DllImport("mapi32.dll", CharSet = CharSet.Ansi)]

Chris Nahr
Thursday, June 3, 2004

The syntax for VB.Net marshalling is a bit different than C#.  Here's a page from MSDN that spells it out:

Robert Jacobson
Thursday, June 3, 2004

*  Recent Topics

*  Fog Creek Home