Hi,
I am having some trouble returning a string from a c++ dll. I tend to get
junk data back and I am not sure of the method, my code so far:
Function is declared in c++ dll 'Test' and exported like this:
extern "C" __declspec( dllexport ) BSTR getbagstr();
extern "C" __declspec( dllexport ) BSTR getbagstr() {
g.getbag()->setdisplaybag("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
const char * buff = g.getbag()->getdisplaybag().c_str();
return SysAllocStringByteLen (buff , lstrlen(buff));
}
I want use the function via interop services - p/invoke - to display the
string in a simple c# program:
[DllImport("Test")]
private static extern IntPtr getbagstr();
public static string getBagString()
{
return Marshal.PtrToStringAuto(getbagstr());
}
The data I am returning is not correct. All display bag does is store and
return the string as below. I am returning int data correctly, but strings
are proving difficult. Anybody able to help?
void Bag::setdisplaybag(const string & str) {
displaybag = str;
}
string Bag::getdisplaybag() { return displaybag; }