I have a CPP app that I want to invoke to my C# app but I'm having trouble passing a ref string param to the CPP method which accepting std::wstring.
My code as follows:
CPP Header:
namespace NMSP
{
extern "C"
{
__declspec(dllexport) int MyCPPMethod(std::wstring &StrCode);
}
CPP Source:
__declspec(dllexport) int NMSP::MyCPPMethod(std::wstring &StrCode)
{
StrCode = "12345";
return 1;
}
C#:
[DllImport(@"MyExternal.dll", CharSet = CharSet.Unicode)]
static extern int MyCPPMethod(ref string StrCode);
string strVer = "version";
var rcver = MyCPPMethod(ref strVer);
When accessing the CPP method, I get "Error reading string" error from the std::wstring &StrCode
value.
I've been tried using StringBuilder str = new StringBuilder("version", 1024);
but no luck.
If I'm changing the std::wstring to char*, I get 'v' (when sending "version" in the parameter) - still I'm confused.
Any help will be appreciated. Thx.