如何编组wchar []

时间:2017-04-05 06:39:49

标签: c# pinvoke

我发现了很多关于如何在互联网上封送wchar_twchar*的信息,但是现在我正在尝试整理WINBIO_STRING类型,这是定义的为wchar[256]。将其编组为char[]数组,但数组中的每个其他元素都是\0,我想避免这种情况。是否有更合适的方法来编组这些数据(它是结构的成员,特别是WINBIO_UNIT_SCHEMA结构)。这是我的代码:

[StructLayout(LayoutKind.Sequential)]
public struct BiometricUnitSchema
{
    public int UnitId;
    public BiometricPoolType PoolType;
    public BiometricType BiometricFactor;
    public BiometricSubtype SensorSubType;
    public BiometricCapabilities Capabilities;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
    public char[] DeviceInstanceId;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
    public char[] Description;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
    public char[] Manufacturer;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
    public char[] Model;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
    public char[] SerialNumber;
    public BiometricVersion FirmwareVersion;
}

我也遇到了FirmwareVersion字段总是包含MajorVersion和MinorVersion值0的问题,但我不确定这个结果是不正确还是只是误导。

1 个答案:

答案 0 :(得分:0)

我通过将WINBIO_STRING编组为C#中的字符串而不是char []来解决此问题。我还必须在我的结构中添加Unicode字符集。

我的新结构:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct BiometricUnitSchema
{
    public int UnitId;
    public BiometricPoolType PoolType;
    public BiometricType BiometricFactor;
    public BiometricSubtype SensorSubType;
    public BiometricCapabilities Capabilities;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string DeviceInstanceId;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string Description;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string Manufacturer;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string Model;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string SerialNumber;
    public BiometricVersion FirmwareVersion;
}