-1

I have extracted with success a structure object from a valid pointer using the PtrToStructure function (in VB.NET), but the result of some object members are not correct (comparing with the C++ example):

the 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(20)> _
        Dim Buffer As IntPtr ' variable but correct
        <FieldOffset(24)> _
        Dim BufferPitch As Integer ' 1408 correct
        <FieldOffset(16)> _
        Dim SurfaceType As Integer ' -2147483645 correct
        <FieldOffset(unknow)> _
        Dim WindowWidth As UInt32 ' must be 356, tested from 0 to 230
        <FieldOffset(unknow)> _
        Dim WindowHeight As UInt32 ' must be 495, tested from 0 to 100
        <FieldOffset(15)> _
        Dim ScreenHeight As UInt32 ' 768 correct
        <FieldOffset(36)> _
        Dim ScreenWidth As UInt32 ' 1366 correct
        <FieldOffset(44)> _
        Dim ScreenDepth As UInt32 ' 32 correct
End Structure

i can't find the exact values for the FieldOffset parameter of WindowWidth and WindowHeight. the question is why and how to fix them?

struct myStruct 
{ 
    U32 Width; // U32 = 32-bit unsigned integer 
    U32 Height; 
    VOID_PTR Buffer;
    S32 BufferPitch; // 32-bit signed integer 
    COPY_FLAGS SurfaceType; // integer 
    U32 WindowWidth; 
    U32 WindowHeight; 
    U32 ScreenWidth; 
    U32 ScreenHeight; 
    U32 ScreenDepth; 
}
  • The offset of 15 on ScreenHeight is almost certainly wrong; offsets should be in multiples of 4 or 8. The offsets of 36 and 44 on ScreenWidth and ScreenDepth are suspicious, because the gaps from 24 to 36 and again from 36 to 44 are much larger than the data before them. There should probably be something at an offset of 8 and something at an offset of 12. – Craig Jul 28 '17 at 19:46
  • the offset of 15 gives the correct result (i tested and retested with another pointers, so with other structures). the same thing with the other members , the problem lies with the WindowWidth and WindowHeight members. – abdellah_gogop Jul 28 '17 at 19:57
  • Is the C++ structure shown *exactly* as declared? Are you sure that there is nothing else on the C++ side that would influence the structure layout? Compiler options, pragmas? The only way I can think of to get an offset of 15 is to have something that is single-byte-sized AND to have something telling C++ not to align the structure. – Craig Jul 28 '17 at 20:00
  • The only variable length you have in there is the IntPtr; it will be either 4 or 8 bytes long (x86 or x64). Based on what you have shown, everything should be on a 4 byte boundary. – TnTinMn Jul 28 '17 at 20:04
  • ok, the documentation shows the strcuture above, but when i write the "->" sign in the C++ sample, it shows me a big list of members, so becarefull. – abdellah_gogop Jul 28 '17 at 20:20
  • this problem is solved. by the offset of 8 and 12 and modifing the pointer parameter with another value. – abdellah_gogop Jul 30 '17 at 10:05

1 Answers1

-1

this problem is solved. by changing the offset located in 8 and 12 and modifing the pointer parameter by another value.