corert icon indicating copy to clipboard operation
corert copied to clipboard

MarshalAs Incorrect Behavior

Open wjk opened this issue 10 years ago • 3 comments

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.

wjk avatar Jul 08 '16 04:07 wjk

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.

jkotas avatar Jul 08 '16 04:07 jkotas

@shrah Would this be expected to work now? Can we close this?

MichalStrehovsky avatar Mar 18 '17 01:03 MichalStrehovsky

Yes, this should work now. @MichalStrehovsky

shrah avatar Mar 18 '17 16:03 shrah