MarshalAs Incorrect Behavior
When working on a P/Invoke call that is declared as follows:
[DllImport("ManagedResourceLoader.dll")]
private static extern int GetManagedResource(
[MarshalAs(UnmanagedType.LPWStr)] string dllName,
[MarshalAs(UnmanagedType.LPWStr)] string resName,
out IntPtr bufferPtr, out long bufferSize);
...it marshals the two string arguments as ASCII strings, not wchar_t strings, despite my MarshalAsAttribute stating otherwise. If I declare the strings as type LPCWSTR on the receiving end (in C++), I get strings of Chinese characters. If I declare them as LPCSTR, I get the correct values.
It is because of TODOs around custom marshaling in https://github.com/dotnet/corert/blob/master/src/Common/src/TypeSystem/IL/Stubs/PInvokeILEmitter.cs.
Fixing these TODOs is about reading the MarshalAs from metadata, and use it to control how the arguments get marshalled.
As workaround, you can use CharSet = CharSet.Unicode in DllImport. It causes string arguments to be marshalled as Unicode as well, and works in CoreRT today.
@shrah Would this be expected to work now? Can we close this?
Yes, this should work now. @MichalStrehovsky