0

i have extrated with success a structure object from a valid pointer using the PtrToStructure function (in vb.NET), the problem is the field "FrameRects" has invalid values comparing with the values obtained from a C++ sample:

the vb.NET code is :

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

the C++ code is :

 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;
};

1 Answers1

0

Shouldn't the field offset for FrameRate be 16, not 20? And everything after that shifted down 4 bytes? Until you get to FrameRects, which should be 32? And since FrameRects is 16 bytes, NumRects should be at 48?

But since you have everything in your structure typed and sequential, why wouldn't you use

<StructLayout(LayoutKind.Sequential)> 

Instead of

StructLayout(LayoutKind.Explicit, pack:=1, CharSet:=CharSet.Ansi)> 
Patrick
  • 196
  • 1
  • 2
  • 9
  • it does not work with the LayoutKind.Sequential, if i use it, it gives me an undefined results – abdellah_gogop Jul 30 '17 at 16:36
  • if i change the number of fieldoffset i get wrong results REMARK: i can't compare the result of fieldoffset = 48 and the out_of_boundaries values because they gives the same values (which equals to 0). – abdellah_gogop Jul 30 '17 at 16:59
  • With the offset of FrameRects at 16, it is overlapping FrameRate, is it not? – Patrick Jul 30 '17 at 17:02
  • it does not overlapping FrameRate – abdellah_gogop Jul 30 '17 at 18:40
  • please help_____________? – abdellah_gogop Jul 30 '17 at 19:50
  • Every type in your structure is 4 bytes, with the exception of the RECT structure which is 16 bytes. There's no reason you should not be be able to use sequential. But there should be no problem with explicit either. Pack should be 4, but shouldn't matter if your offsets are on 4 byte boundaries. Where you have FrameRects at 16, it starts right after FrameNum which ends at 15 and FrameRects goes to 31, which overlaps FrameRate, FrameRateDiv and ReadError. – Patrick Jul 30 '17 at 20:39
  • https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.layoutkind(v=vs.110).aspx – Patrick Jul 30 '17 at 20:45
  • solved , thanks to all. – abdellah_gogop Jul 31 '17 at 18:34
  • Please do tell us about the solution. – Patrick Jul 31 '17 at 23:54
  • the correct value for the FrameRect member is 16, it is solved by changing some parameters of the pointer. – abdellah_gogop Aug 01 '17 at 08:12