封送 C++ 锯齿状数组给出 "Invalid managed/unmanaged type combination" 错误

Marshaling C++ jagged array gives "Invalid managed/unmanaged type combination" error

这是我的 C++ 定义:

typedef struct _DRIVER_VERSION
{
DWORD DriverNum;
CHAR Version[16][16];
} DRIVER_INFO, *PDRIVER_INFO;

这是我的 C# 互操作代码:

[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Version16
{
    [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 16)]
    public string VerInfo;
}


/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct _DRIVER_VERSION
{
    public System.UInt32 DriverNum;
    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)]
    public Version16 Version16;

}

    public static class InstallDeviceClass
{
    [DllImport(@"driver.dll")]
    public static extern SResultEnum s4drv_GetDriverInfo(ref _DRIVER_VERSION pDrvInfo);
}

    static void Main(string[] args)
    {
        _DRIVER_VERSION version=new _DRIVER_VERSION();
        var s4Res = InstallDeviceClass.s4drv_GetDriverInfo(ref version);
    }

但是,当我运行代码时,我会得到

$exception {"Cannot marshal field 'Version16' of type '_DRIVER_VERSION': Invalid managed/unmanaged type combination (this value type must be paired with Struct).":""} System.TypeLoadException

发生这种情况的任何原因,以及如何解决这个问题?

修复是

/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct _DRIVER_VERSION
{
    public System.UInt32 DriverNum;
    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)]
    public Version16[] Version16;

}

Version16应该是数组,不是字段