A std::wstring is a C++ object, allocated by the C++ runtime and having an implementation-dependent internal format. You might be able to figure out how to create one of those in a C# program and pass it to the unmanaged C++ code, but doing so would be somewhat difficult and fraught with peril. Because the internal structure of a std::wstring is implementation dependent, any change to the C++ compiler or runtime libraries break your solution.
What you're trying to do is typically done by writing an interface layer in C++ that takes an LPTStr parameter, converts it to a std::wstring, and then calls the C++ function that you wanted to call. That is, if the function you want to call is declared as:
A
std::wstring
is a C++ object, allocated by the C++ runtime and having an implementation-dependent internal format. You might be able to figure out how to create one of those in a C# program and pass it to the unmanaged C++ code, but doing so would be somewhat difficult and fraught with peril. Because the internal structure of astd::wstring
is implementation dependent, any change to the C++ compiler or runtime libraries break your solution.What you're trying to do is typically done by writing an interface layer in C++ that takes an
LPTStr
parameter, converts it to astd::wstring
, and then calls the C++ function that you wanted to call. That is, if the function you want to call is declared as:You would write an interface function:
And you call
FooCaller
from your C# program.In short, C# can't create and pass a
std::wstring
, so you use a translation layer.