Function signature:
char * errMessage(int err);
My code:
[DllImport("api.dll")] internal static extern char[] errMessage(int err); ... char[] message = errMessage(err);
This returns an error:
Cannot marshal 'return value': Invalid managed/unmanaged type combination.
What am I doing wrong? Thanks for any help.
A simple and robust way is to allocate a buffer in C# in the form if a StringBuilder
, pass it to the unmanaged code and fill it there.
Example:
C
#include <string.h>
int foo(char *buf, int n) {
strncpy(buf, "Hello World", n);
return 0;
}
C#
[DllImport("libfoo", EntryPoint = "foo")]
static extern int Foo(StringBuilder buffer, int capacity);
static void Main()
{
StringBuilder sb = new StringBuilder(100);
Foo(sb, sb.Capacity);
Console.WriteLine(sb.ToString());
}
Test:
Hello World
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With