I have in my C++/CLI project some native functions and a ref (managed) class that exposes them to a C# project.
One of the functions that it exposes returns a WCHAR* string (AKA LPWSTR / wchar_t*).
When I ran the C# program which prints it, all I could see is square symbols.
I've set a breakpoint at the native return statement and the debugger shows that the returned string is correct.
Then I stepped over once and landed in the managed function (which has a WCHAR* variable that's set to the returned value), and somehow it shows those square symbols.
It seems like once the string enters the managed "section" it gets messed up.
I'd show my code but the issue happens before I even convert the WCHAR* string into System::String so it doesn't really matter.
Code sample as requested:
static String^ GetWindowTitle(IntPtr windowHandle)
{
HWND hWnd = (HWND)windowHandle.ToPointer();
LPWSTR nativeTitle = NativeGetWindowTitle(hWnd).get();
String^ title = gcnew String(nativeTitle);
return title;
}
NativeGetWindowTitle? Why are you using C strings here? Surely you should be usingstd:wstring? What makes you believe thatnativeTitlewill still be valid when you pass it to theStringconstructor? It seems hard to imagine that it could be. - David HeffernanLPWSTRparameter. I don't know why that matters. it works. - MasterMasticunique_ptr<WCHAR[]>. - MasterMastic