I'm calling a C library function from a C# method; something like this:
[DllImport("libfoo.dll")]
public static extern int GetData(StringBuilder buffer);
It returns the number of characters that it put into the buffer, and there's a known maximum limit on the number of characters; so it could be called like:
StringBuilder sb = new StringBuilder(5000);
int received = GetData(sb);
So the problem/question: the C function is using a character array as a byte array; we're not necessarily looking at normal strings, and the character array may well include null characters.
It seems that this default, naive usage of StringBuilder results in a C# string that is cut off at the first null character / null byte.
What else could be done? Is there a way to force StringBuilder to accept strings with null characters? Or is there a better approach for accessing the C library function?
Edit: C function is something like:
int GetData (char *buffer);