P/Invoke a Function Passed a StringBuilder

Posted by andrew on Stack Overflow See other posts from Stack Overflow or by andrew
Published on 2010-05-21T17:29:23Z Indexed on 2010/05/22 4:10 UTC
Read the original article Hit count: 285

Filed under:
|
|
|

in a C# file i have a

class Archiver {
    [DllImport("Archiver.dll")]
    public static extern void archive(string data, StringBuilder response);
}

string data is an input, and StringBuilder response is where the function writes something

the archive function prototype (written in C) looks like this:

void archive(char * dataChr, char * outChr);

and it receives a string in dataChr, and then does a

strcpy(outChr,"some big text");

from C# i call it something like this:

string message = "some text here";
StringBuilder response = new StringBuilder(10000);
Archiver.archive(message,response);

this works, but the problem, as you might see is that i give a value to the StringBuilder size, but the archive function might give back a (way) larger text than the size i've given to my StringBuilder. any way to fix this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about c