Calling DLL function with char* param from C#?

calling c dll from c#
calling unmanaged c++ from c#
call managed dll from unmanaged c++
c# call c++ function
calling c from c#
c++ call native dll
c++ to c# wrapper generator

I have a C++ dll that I need to call from C#. One of the functions in the dll requires a char* for an input parameter, and another function uses a char* as an output parameter.

What is the proper way to call these from C#?

Just using strings will work fine for input parameters, though you can control details about the string with the MarshalAs attribute. E.g.

[DllImport("somedll.dll", CharSet = CharSet.Unicode)]
static extern void Func([MarshalAs(UnmanagedType.LPWStr)] string wideString);

As for returning char* parameters, that's a little more complex since object ownership is involved. If you can change the C++ DLL you can use CoTaskMemAllocate, with something like:

void OutputString(char*& output)
    char* toCopy = "hello...";
    size_t bufferSize = strlen(toCopy);
    LPVOID mem = CoTaskMemAlloc(bufferSize);
    memcpy(mem, toCopy, bufferSize);
    output = static_cast<char*>(mem);

The C# side then just uses an 'out string' parameter, and the garbage collector can pick up the ownership of the string.

Another way of doing it would be to use a StringBuilder, but then you need to know how big the string will be before you actually call the function.

Calling Native Functions from Managed Code, Calling Native Functions from Managed Code or PInvoke, that enables managed code to call C-style functions in native dynamic-linked libraries (DLLs). __declspec(dllexport) char* fstringA(char* param) { return param; }. Calling C++ dll from C# using a function with char** as param with DllImport. I'm trying to call a C++ function that has a char** as a parameter from C# code using DllImport. After googling around and finding some threads I haven't been able to solve my problem.

string should work if the parameter is read-only, if the method modifies the string you should use StringBuilder instead.

Example from reference below:

 [DllImport ("")]
 private static extern void strncpy (StringBuilder dest, 
      string src, uint n);

 private static void UseStrncpy ()
    StringBuilder sb = new StringBuilder (256);
    strncpy (sb, "this is the source string", sb.Capacity);
    Console.WriteLine (sb.ToString());

If you don't know how p/invoke marshaling works you could read

If you are only conserning with strings, read only the section:

[PDF] Calling C Library DLLs from C Sharp, For instance, take the example of a C function that takes two parameters of the The final type to consider is the string type in C#, mapping to char* type in C. If the DLL function is expecting an allocated buffer of char* (not a wide/multibyte buffer) then the following will work: [DllImport("somedll.dll", CharSet = CharSet.Ansi)] static extern void TheFunc(byte[] someBuffer, int someSize); Here a buffer allocated in c# is passed to TheFunc which fills it with a string of characters (of type char).

Not sure this works, but have you tried with


Not sure about initialising the String from char * tho. Mybe just assign to a string object, its worth a try.

Passing Arguments to DLL Functions, Since DLL functions are written in C, the arguments you pass to these functions If a DLL sets an out parameter of type char* to a value larger than 256 bytes, you If the user calls a DLL function with an output string buffer that is less then the  Marshaling is a big topic, but luckily you don't have to know much to get the job done. To call a DLL function from C#, first you must provide a declaration, something programmers using Visual Basic have been doing for years. In C#, it's DllImport: using System.Runtime.InteropServices; // DllImport.

Have you tried StringBuilder? I found this in a Google search:

public static extern bool GetUserName(StringBuilder lpBuffer, ref int nSize);

If you post the call you're making we can help you assemble it.

How do I call a C++ dll method (having const char* as input params , at first: dont use full pathes. The dll belongs in the output dir of your build (per copy) I use Stringbuilder objects for these taskes for years. In general, the calling convention of a native function, such as a Windows API function or a C-runtime DLL function, describes how the parameters are pushed onto and cleaned off the thread's stack. Most Windows API functions push the last parameter of a function onto the stack first, and then it is the called function's job to clean up the stack.

If the DLL function is expecting an allocated buffer of char* (not a wide/multibyte buffer) then the following will work:

[DllImport("somedll.dll", CharSet = CharSet.Ansi)]
static extern void TheFunc(byte[] someBuffer, int someSize);

Here a buffer allocated in c# is passed to TheFunc which fills it with a string of characters (of type char). Bytes aren't "interpreted" by C# they are treated like 8 bit integers, so are perfect for holding 8 bit characters.

An example code snipped would therefore be:

byte[] mybuffer;
int bufSize;

bufSize = 2048;
mybuffer = new byte[bufSize];

TheFunc(mybuffer, bufSize);

string value;
for(value = "", int ix  = 0; (mybuffer[ix] != 0) && (ix < bufSize); ix++)
  value += (char) mybuffer[ix];


C# in a Nutshell, Since C # not only can call out to C functions but can also be called back from To call a function in a DLL that takes a callback function pointer as a parameter the char started at an offset of 2 , then you ' d change the value of the int if you  This creates a copy of the string in unmanaged memory. This also converts the unicode string used in C# to ascii (char* is ASCII, wchar_t* is Unicode) init_set(ptrCString); // Call our function Marshal.FreeHGlobal(ptrCString); // Free the copy of the memory we made 2 lines above} } }

How to pass char in C# to C++ DLL, Then remember that C# strings aren't null terminated - so unless you pass the string length to C++, you need to manually add a null value at the  For instance, a DLL you are using utilises a struct in C which contains a char * (a string or character array). Your wrapper DLL can contain a wrapper function with a char * parameter that places the char * into a struct, and in turn calls the original DLL. In LabVIEW, you can instead call the wrapper DLL functions.

Financial Applications using Excel Add-in Development in C / C++, Where DLL functions take native C data type arguments such as ints, doubles and double *; • [signed] char * (null-terminated ASCII byte string); • unsigned char If Excel cannot convert an input value to the type specified then it will not call 

1. Extending Python with C or C++, For example, if your use case is calling C library functions or system calls, you static PyObject * spam_system(PyObject *self, PyObject *args) { const char