什么.NET UnmanagedType是Unicode(UTF-16)?

我将字节打包到结构中,其中一些对应于Unicode字符串.以下适用于ASCII字符串:

[StructLayout(LayoutKind.Sequential)]
private struct PacketBytes
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
    public string MyString;
}

我以为我能做到

[StructLayout(LayoutKind.Sequential)]
private struct PacketBytes
{
    [MarshalAs(UnmanagedType.LPWStr, SizeConst = 32)]
    public string MyString;
}

使其成为Unicode,但这不起作用(字段值为空,其他字段的值不正确,表明字节解包已搞砸). (因为这个字段是包含其他字段的结构的一部分,为了清楚起见我省略了,我不能简单地更改包含结构的CharSet.)

知道我做错了什么吗?

这是输入(64字节,小端):

31:00:31:00:32:00:33:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00

输出应为Unicode字符串“1123”.

最佳答案 我会通过声明字符串类型的嵌套结构来做到这一点. “内部”结构可以声明其CharSet.这类似于我博客上的解决方案:
http://nitoprograms.blogspot.com/2010/02/interop-multidimensional-arrays-of.html

例如.:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct StringSizeConst32AsString
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    private string Value;

    public static implicit operator string(StringSizeConst32AsString source)
    {
        return source.Value;
    }

    public static implicit operator StringSizeConst32AsString(string source)
    {
        // Note that longer strings would be silently truncated
        //  if we didn't explicitly check this.
        if (source.Length >= 32)
            throw new Exception("String too large for field: " + source);

        return new StringSizeConst32AsString { Value = source };
    }
}
点赞