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?

sedwo
Thursday, June 03, 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 03, 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:

[DllImport("blah.dll")]
public static extern void BlahFunction(
    [MarshalAs(UnmanagedType.LPStr)]
    string foo);

Chris Nahr
Thursday, June 03, 2004

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

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

Chris Nahr
Thursday, June 03, 2004

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

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcharsetobjectfield.asp

Robert Jacobson
Thursday, June 03, 2004

*  Recent Topics

*  Fog Creek Home