Modify

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#723 closed Bug (No Bug)

Structure problem

Reported by: trancexx Owned by:
Milestone: Component: AutoIt
Version: 3.2.12.1 Severity: Blocking
Keywords: DllStructureCreate(), align, DllStructGetPtr() Cc:

Description

Unexpected behaviour with function DllStructureCreate()when created with pointer and "dword":

ConsoleWrite("Binary data is 0x3378FF453D1133" & @CRLF & @CRLF)

$structure_Binary_1 = DllStructCreate("byte[7]")
DllStructSetData($structure_Binary_1, 1, "0x3378FF453D1133"); that is 0x3378 and 0xFF453D11, last byte 0x33 is just for testing 

; Testing on "short;dword"
$struct_1 =  DllStructCreate("short;dword", DllStructGetPtr($structure_Binary_1))
ConsoleWrite("Before align:" & @CRLF)
ConsoleWrite(Binary(DllStructGetData($struct_1, 1)) & " <-- Binary(short) - ok" & @CRLF)
ConsoleWrite(Binary(DllStructGetData($struct_1, 2)) & " <-- Binary(dword) - 0xFF is missing and 0x33 is there" & @CRLF)

ConsoleWrite(@CRLF)


; New structure is "short;dword" with keyword align
$structure_Binary_2 = DllStructCreate("byte[7]")
DllStructSetData($structure_Binary_2, 1, "0x3378FF453D1133")

$struct_2 =  DllStructCreate("align 1;short;dword", DllStructGetPtr($structure_Binary_2)) ; or "align 2"
ConsoleWrite("After:" & @CRLF)
ConsoleWrite(Binary(DllStructGetData($struct_2, 1)) & " <-- Binary(short)" & @CRLF)
ConsoleWrite(Binary(DllStructGetData($struct_2, 2)) & " <-- Binary(dword), this appears ok" & @CRLF)


ConsoleWrite(@CRLF)


; Testing on "dword;short"
$structure_Binary_3 = DllStructCreate("byte[7]")
DllStructSetData($structure_Binary_3, 1, "0x3378FF453D1133")

$struct_3 =  DllStructCreate("dword;short", DllStructGetPtr($structure_Binary_3)) 
ConsoleWrite("Reoganized (no align):" & @CRLF)
ConsoleWrite(Binary(DllStructGetData($struct_3, 1)) & " <-- Binary(dword) - ok" & @CRLF)
ConsoleWrite(Binary(DllStructGetData($struct_3, 2)) & " <-- Binary(short) - ok" & @CRLF)

Thread on forum:
http://www.autoitscript.com/forum/index.php?showtopic=85213

Attachments (0)

Change History (3)

comment:1 Changed 11 years ago by Valik

  • Resolution set to No Bug
  • Status changed from new to closed

The code is correct. The helpfile almost explains why with this line:

DllStructCreate("short;int") ; structure is 8 bytes, the "int" is at offset 4

The key there is, the int (dword in your example) starts at byte 4 (0-index) and not at byte 2 like you are thinking.

So, we have the data 0x3378FF453D1133. The 0x3378FF45 portion is mapped to the short but only the first 2 bytes are accessible (0x3378). The 0xFF45 portion is mapped to the 2 bytes of padding that appears between the short and the dword. That means that the dword is 0x3D113300. Visually it looks like:
3378FF453D113300 (Extra 00's added to pad length for clarity).
SSSSPPPPDDDDDDDD (Where S = short, P = padding and D = dword).

When you start changing the alignment, you remove the padding and the structures align correctly.

This is not a bug.

comment:2 Changed 11 years ago by Valik

Ooops, here's the memory layout expressed in a fixed-width font so you may see the mapping.

3378FF453D113300 (Extra 00's added to pad length for clarity).
SSSSPPPPDDDDDDDD (Where S = short, P = padding and D = dword).

comment:3 Changed 11 years ago by anonymous

Briliant and simple explanation.

...and if I set data manually (like copying from one structure to another) problem cannot occure because it's filled 8 and 8 and 8...

Thanks!

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.