0

我正在尝试在 C# 中构建一个结构以传递给非托管 C++,我想知道unichar在我的结构中用于数组的正确变量类型是什么以及它应该被编组为什么。

我已经想通了unsigned char array

C/C++

typedef struct _foo {
    void *fileId;
    unsigned char   fileName[15];
} foo;

C#

[StructLayout(LayoutKind.Sequential)]
public struct foo
{
   public IntPtr fileId;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 15)]
   public string fileName;
}

所以如果我在 C++ 中有以下内容

typedef struct _foo {
    void *fileId;
    unichar fileName[15];   //  UTF-16LE
} foo;

在 C# 中使用的正确结构是什么?

4

2 回答 2

0

我猜同样struct会这样做,但是您需要将DllImportAttribute.CharSet属性设置为,Auto否则它将默认为Ansi. Unicode也可以,但除非您使用的是 Windows 98 或 Me(无评论),Auto否则会将字符串编组为Unicode

于 2011-06-27T09:42:31.407 回答
0

将结构指定为 unicode 结构:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct foo
{
   public IntPtr fileId;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 15)]
   public string fileName;
}
于 2011-06-27T09:45:16.540 回答