4

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,

1

5 Answers 5

27

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.

1
  • What if the C declaration is : __declspec(dllexport) MyErrCode GetTrustTypes(const char* bundleName, char ***trustTypes); What should be the corresponding declaration in C# ?
    – The King
    Commented Jun 1, 2023 at 8:07
2

Be aware of calling conventions, this tripped me up. In my case I need to call a C++ DLL but with C-style exports, which uses the cdecl calling convention. If you have the luxury of having the source Visual Studio Solution, go to Properties -> C/C++ -> Advanced and find it under "Calling Convention". This fix it for me:

[DllImport(DllName, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
// bool MyFunction(char* fileName) <-- From the DLL
static extern bool MyFunction(string fileName);
2
[DllImport("DLL loction"), EntryPoint = "dll_registerAccount",
    CallingConvention = CallingConvention.Cdecl)]
[return : MarshalAs(UnmanagedType.I4)]
static extern int dll_registerAccount(
    [MarshalAs(UnmanagedType.LPStr)]string username,
    [MarshalAs(UnmanagedType.LPStr)]string password);
  • cdecl is the default calling convention for C and C++ programs,
  • refer these doc for more information.
1
    [DllImport("pjsipDlld", CharSet = CharSet.Ansi)]
    static extern int dll_registerAccount(string username, string password);
1

StringBuilder for char*, since the length is unknown?

[DllImport("pjsipDlld")]
static extern int dll_registerAccount(StringBuilder username, StringBuilder password);
3
  • 1
    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
    Commented Sep 23, 2009 at 12:09
  • Yes, I readup on those attributes (LPStr & LPWStr) after seeing your answer. Makes perfect sense now. Commented Sep 23, 2009 at 12:28
  • This ended up working perfectly for me when calling Fortran subroutine from C#, while the accepted answer did not. I had a known length too and this is the version that worked. YMMV. Commented Mar 26, 2014 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.