0

我已经使用 PtrToStructure 函数(在 vb.NET 中)从有效指针中成功提取了结构对象,问题是“FrameRects”字段与从 C++ 示例中获得的值相比具有无效值:

vb.NET 代码是:

StructLayout(LayoutKind.Explicit, pack:=1, CharSet:=CharSet.Ansi)> _"

Public Structure myStruct
    <FieldOffset(0)> _
    Dim Width As UInt32 ' 350 correct
    <FieldOffset(4)> _
    Dim Height As UInt32 ' 466 correct
    <FieldOffset(8)> _
    Dim Frames As UInt32 ' 115 correct
    <FieldOffset(12)> _
    Dim FrameNum As UInt32 ' 1 correct
    <FieldOffset(20)> _
    Dim FrameRate As UInt32 ' 15 correct
    <FieldOffset(24)> _
    Dim FrameRateDiv As UInt32 ' 1 correct
    <FieldOffset(28)> _
    Dim ReadError As UInt32 ' 0 correct
    <FieldOffset(32)> _
    Dim OpenFlags As Integer ' 0 correct

    <FieldOffset(16)> _
    <MarshalAs(UnmanagedType.Struct)> _
    Dim FrameRects As RECT ' the problem is located here

    <FieldOffset(36)> _
    Dim NumRects As UInt32 ' 0 correct  

    <FieldOffset(44)> _
    Dim FrameChangePercent As UInt32 ' 0 correct
End Structure

"StructLayout(LayoutKind.Explicit, pack:=1, CharSet:=CharSet.Ansi)> _

Public Structure RECT
    <MarshalAs(UnmanagedType.I4)> _
    <FieldOffset(0)> _
    Dim Left As Integer ' gives me -1 but it must be 0 (in C++ sample)
    <MarshalAs(UnmanagedType.I4)> _
    <FieldOffset(4)> _
    Dim Top As Integer ' gives me 15 but it must be 0 (in C++ sample)
    <MarshalAs(UnmanagedType.I4)> _
    <FieldOffset(8)> _
    Dim Width As Integer ' gives me 1 but it must be 0 (in C++ sample)
    <MarshalAs(UnmanagedType.I4)> _
    <FieldOffset(12)> _
    Dim Height As Integer ' gives me 0 and it is 0 (in C++ sample)
End Structure

C++ 代码是:

 struct myStruct
{
  U32 Width; // U32 is unsigned integer
  U32 Height;
  U32 Frames;
  U32 FrameNum;
  U32 FrameRate;
  U32 FrameRateDiv;
  U32 ReadError;
  OPEN_FLAGS OpenFlags; // integer
  RECT_ARRAY FrameRects;
  U32 NumRects;
  U32 FrameChangePercent;
};

 struct RECT
{
  S32 Left; // S32 is singed integer
  S32 Top;
  S32 Width;
  S32 Height;
};
4

1 回答 1

0

FrameRate 的字段偏移量不应该是 16,而不是 20?之后的所有内容都向下移动了 4 个字节?直到你到达 FrameRects,它应该是 32?而且由于 FrameRects 是 16 字节,NumRects 应该是 48?

但是既然你的结构中的所有东西都是类型化和顺序的,你为什么不使用

<StructLayout(LayoutKind.Sequential)> 

代替

StructLayout(LayoutKind.Explicit, pack:=1, CharSet:=CharSet.Ansi)> 
于 2017-07-30T14:15:45.610 回答