Please help,
my c++ function:
extern "C" REGISTRATION_API void calculate(char* msg)
{
//some calculation here
msg = "some text";
}
my c# call:
[DllImport("thecpp.dll")]
static extern void calculate(StringBuilder sMsg);
private void button4_Click(object sender, EventArgs e)
{
StringBuilder msg = new StringBuilder();
calculate(msg);
MessageBox.Show(msg.ToString());
}
No matter what i put in msg, the stringbuilder is always empty? why? whats wrong? any ideas? please share
You are correct that you should use
stringforLPCTSTRbuffers andStringBuilderforLPTSTRbuffers.But you need 2 changes:
1) Set the capacity on your StringBuilder
2) You should be doing
strcpyinto that buffer, changing what memory address that variable holds won’t do anything as you have it now. You need to change what is at the memory address.So you want in your C/C++ code:
and in C#:
Note: You really should pass in the length of the buffer that you are passing in to the calculate function as well.