I was given C code which I have compiled to a DLL. I then created a wrapper in VC++ that calls functions in this code. Everything up to this point works fine. I'm having trouble passing a C# stringbuilder (char*) to the C++ code from my C# code that uses this wrapper.
So far I have a wrapper class written in VC++ that has this function call:
void Wrapper::ReadStream(char* buffer, int* size)
{
int req_sz;
read_stream(opts, req_sz, req, buffer); //This calls the C DLL, this part works.
}
I'm having a problem in my C# code while trying to call the C++ wrapper function ReadStream. After looking around for similar problems I found that in order to pass a String from C# to C++ (which will be looking for a char*) a stringbuilder should be passed, so I have tried:
C# Code:
...
Wrapper wrap = new Wrapper()
...
int bufferSize = 48;
StringBuilder buffer = new StringBuilder(bufferSize);
wrap.ReadStream(buffer, ref bufferSize);
But the code will not compile because C# sees the function definition as wanting an sbyte*. I've read that this is because C# chars are unicode (2 bytes), whereas C++ chars are single byte characters, but the suggested solution is to pass a StringBuilder.
I've seen a lot of solutions where the C# code passes a stringbuilder directly to the DLL, but cannot find one that will pass a stringbuilder to a C++ wrapper class.