1

Possible Duplicate:
std::string in C#?
How can I call a function of a C++ DLL that accepts a parameter of type stringstream from C#?

Is there a way to convert from a c++ std:string to C# System.String? I'm calling a function from a C++ dll that takes a std:string as input. Is there a simple way to do this?

C#:

[DllImport(@"MyDLL.dll")]
[return:MarshalAs(UnmanagedType.I1)]
public static extern bool myFunction([In, Out]string input);

C++:

extern "C" __declspec(dllexport) BOOL __stdcall myFunction(const std::string& input)
{
  //Code is here
}
3
  • 5
    Your DLL should not be using std::string in its interface. See this question Commented Oct 24, 2011 at 20:47
  • It is never a good idea to share builtin complex datatypes between different programming languages (no matter how similar), since you don't know how the datatype is built internally. A better way is to export something known to you, in this context a null-terminated char* should do the job. Commented Oct 24, 2011 at 20:53
  • 1
    Note that BOOL in C/C++ should be marshaled with the default marshaler, not as UnmanagedType.I1 -- it is C++'s bool that must be marshaled as I1 or U1. Commented Oct 24, 2011 at 22:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.