ant2009 ant2009 - 11 months ago 66
C# Question

equivalent char* in C#

I have a dll that is written in c++. And I am p/invoking to call the functions.

I have this c++ declaration.

int dll_registerAccount(char* username, char* password);

I have done this dllimport declaration:

static extern int dll_registerAccount(IntPtr username, IntPtr password);

Would my DllImport be the equivalent to the c++ using IntPtr?

Many thanks for any advice,

Answer Source

The C# way of doing this is by letting the marshaler handle the char* stuff while you work with a string:

static extern int dll_registerAccount(
    [MarshalAs(UnmanagedType.LPStr)]string username,
    [MarshalAs(UnmanagedType.LPStr)]string password);

Replace LPStr with LPWStr if you're working with wide chars.