PInvoke编组wchar_t参数

时间:2017-12-01 22:39:43

标签: c# .net-core pinvoke

根据https://docs.microsoft.com/en-us/dotnet/framework/interop/marshaling-data-with-platform-invoke,如果我在C ++函数中有wchar_t类型的参数,我需要“用Unicode装饰它”。但是,MarshalAs属性似乎没有任何选项。 UnmanagedType中的System.Runtime.InteropServices枚举没有Unicode元素。或者Ansi就此而言,尽管页面表明应该这样做。

那么,这是怎么做到的?

修改

假设我有一个看起来像这样的函数:

int doSomething(wchar_t charIn) { ... }

甚至

int doSomethingElse(wchar_t *charArr) {...}

获取wchar_t个值的数组。根据页面我应该用Unicode注释(装饰)这些参数。我该怎么做?

1 个答案:

答案 0 :(得分:0)

wchar_t*基本上是String,与LPWSTR相同。对于方法doSomethingElse,我会说:

[DllImport("MyLib.dll", CharSet = CharSet.Unicode)]
internal static extern Int32 doSomethingElse(StringBuilder builder);

或者这个:

[DllImport("MyLib.dll")]
internal static extern Int32 doSomethingElse([MarshalAs(UnmanagedType.LPWStr)] String str);

对于方法doSomething的问题,我有点不确定。我的意思是,它要求一个char,这很奇怪。 byte不是一回事,我想知道你的声明是否正确。