views:

434

answers:

3

Hello,

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:

[DllImport("pjsipDlld")]
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,

+17  A: 

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

[DllImport("pjsipDlld")]
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.

Blindy
+1  A: 
    [DllImport("pjsipDlld", CharSet = CharSet.Ansi)]
    static extern int dll_registerAccount(string username, string password);
Stephen Martin
+1  A: 

StringBuilder for char*, since the length is unknown?

[DllImport("pjsipDlld")]
static extern int dll_registerAccount(StringBuilder username, StringBuilder password);
ParmesanCodice
The length might be unknown, but what comes in and what gets out are constant strings (taken separatly of course, they dont have to be the same thing), so they map perfectly to a `string` object.
Blindy
ParmesanCodice