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
}
Community
  • 1
  • 1
NexAddo
  • 752
  • 1
  • 18
  • 37
  • 5
    Your DLL should not be using `std::string` in its interface. See [this question](http://stackoverflow.com/questions/4538562/how-can-i-call-a-function-of-a-c-dll-that-accepts-a-parameter-of-type-stringst/4551798#4551798) – tenfour Oct 24 '11 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. – Philipp Wendt Oct 24 '11 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`. – ildjarn Oct 24 '11 at 22:00

0 Answers0