Jump to content

Detecting struct elements causing illogical and inconsistent results


Recommended Posts

At least illogical to me!

The following script is based on a script jchd wrote. I have changed it so it returns a tag rather than writing the element types and values to the Console. The tag and the values set in the structure are those he used.

As is, it returns "char;". It should return "char[3]; ...'. With changes in the diagnostic code, I have seem it return "char[3];", but it should return the whole tag.

I am aware that the script will not differentiate, for example, between an int and a long: it works from the type of what DllStructGetData returns, and calls it an int.

I am concerned that there may be a memory leak due to a DLLStruct* call, because the code, with minor variations, produces different results.

I have instrumented it liberally, but @error is 0 everywhere.

Clues will be most welcome!

Local $tag = "char a[3];handle b[3];uint c[35];byte d[128];wchar e[190000]; double f[3];int64 g[3];" & _
    "char h[3];float i;double j;byte k;ubyte l;short m;ushort n;int o;uint p;char q"
Local $struct = DllStructCreate($tag)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 1, 'sos')
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 2, Ptr(123456789))
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 3, 8, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 3, 0x87654321, 2)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 3, 256, 5)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 4, Binary('sos'))
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 5, 'gno' & @CRLF & 'ji' & @TAB & 'o')
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 6, 3.1415926, 2)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 7, 17, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 7, -1, 2)
DllStructSetData($struct, 8, 'end')
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 9, 2.7182818284590452353602874713527)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 10, 2.7182818284590452353602874713527)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 11, 107)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 12, -108)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 13, 109)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 14, 110)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 15, 111)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
DllStructSetData($struct, 16, 112)
If @error Then MsgBox(0,@ScriptLineNumber,@error)

_cDebug_DetectStructElements($struct)

Func _cDebug_DetectStructElements($tStruct)
Local $retTag
    Local $len = DllStructGetSize($tStruct)
    Local $ptr = DllStructGetPtr($tStruct)
    Local $nbElem = 1, $idx, $incr, $data, $type, $oldvalue, $readvalue, $elem
    $g_CDebug_sStructElementTypes = ''
    While 1
        $data = DllStructGetData($tStruct, $nbElem)
If @error And @error<>2 Then MsgBox(0,@ScriptLineNumber,@error)
        If @error = 2 Then ExitLoop ; if element out of range or unknown
        $type = VarGetType($data)
        $idx = 1
        $incr = 0
        ; determine max index of element
        While 1
            DllStructGetData($tStruct, $nbElem, 2 * $idx)
If @error And @error<>3 Then MsgBox(0,@ScriptLineNumber,@error)

            If @error = 3 Then ExitLoop
            $incr = $idx
            $idx *= 2
        WEnd
        ; index is in [$idx, (2 * $idx) - 1]
        $idx += $incr
        Do
            DllStructGetData($tStruct, $nbElem, $idx)
If @error And @error<>3 Then MsgBox(0,@ScriptLineNumber,@error)
            If @error = 3 Then      ; if element is out of range
                ; approach is asymetric (upper bound is too big)
                $idx -= ($incr = 1) ? 1 : $incr / 2
            Else
                $idx += Int($incr / 2)
            EndIf
            $incr = Int($incr / 2)
        Until $incr = 0
        Switch $type
            Case "Int32", "Int64"
                $data = DllStructGetData($tStruct, $nbElem, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
                DllStructSetData($tStruct, $nbElem, 0x7777666655554433, 1)
                $readvalue = DllStructGetData($tStruct, $nbElem, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
                Switch $readvalue
                    Case 0x7777666655554433
                        $elem = "int64"
                        ; alias: uint64
                        ; alias: int_ptr(x64), long_ptr(x64), lresult(x64), lparam(x64)
                        ; alias: uint_ptr(x64), ulong_ptr(x64), dword_ptr(x64), wparam(x64)
                    Case 0x55554433
                        DllStructSetData($tStruct, $nbElem, 0x88887777, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
                        $readvalue = DllStructGetData($tStruct, $nbElem, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
                        $elem = ($readvalue > 0 ? "uint" : "int")
                        ; int aliases: long, bool, int_ptr(x86), long_ptr(x86), lresult(x86), lparam(x86);
                        ; uint aliases: ulong, dword, uint_ptr(x86), ulong_ptr(x86), dword_ptr(x86), wparam(x86)
                    Case 0x4433
                        DllStructSetData($tStruct, $nbElem, 0x8888, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
                        $readvalue = DllStructGetData($tStruct, $nbElem, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
                        $elem = ($readvalue > 0 ? "ushort" : "short")
                    Case 0x33
                        $elem = "byte"
                        ; alias: ubyte
                EndSwitch
                DllStructSetData($tStruct, $nbElem, $data, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
                $retTag &= $elem
            Case "String"
                $oldvalue = DllStructGetData($tStruct, $nbElem, 1)
                DllStructSetData($tStruct, $nbElem, ChrW(0x2573), 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
                $readvalue = DllStructGetData($tStruct, $nbElem, 1)
                DllStructSetData($tStruct, $nbElem, $oldvalue, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
                $retTag = ($readvalue = ChrW(0x2573) ? "wchar" : "char")
;~              If $idx > 1 Then $elem &= "[" & $idx & "]"      ; Dosn't work here either!!!

            Case "Binary"
                Local $blen = BinaryLen($data)
                $retTag = "byte"
            Case "Ptr"
                $retTag &= "ptr"
                ; alias: hwnd, handle
            Case "Double"
                $oldvalue = DllStructGetData($tStruct, $nbElem, 1)
                DllStructSetData($tStruct, $nbElem, 10^-15, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
                $readvalue = DllStructGetData($tStruct, $nbElem, 1)
                DllStructSetData($tStruct, $nbElem, $oldvalue, 1)
If @error Then MsgBox(0,@ScriptLineNumber,@error)
                $retTag &= ($readvalue = 10^-15 ? "double" : "float")
        EndSwitch
If $nbElem=1 Then MsgBox(0,@ScriptLineNumber,'idx '&$idx)
        If $idx>1 Then
If $nbElem=1 Then MsgBox(0,@ScriptLineNumber,'before index')
            $retTag &= '['&$idx&']'
        EndIf
        $retTag &= ';'
        $nbElem += 1
    WEnd
MsgBox(0,@ScriptLineNumber,'$retTag "'&$retTag&'"')
    Return $retTag
EndFunc

jchd's code is in post 7

 

 

Edited by c.haslam
Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

I tried inserting

Opt('TrayIconDebug',1)
#include <WinAPIDiag.au3>

at the top of the script and

_WinAPI_DisplayStruct($tStruct)

after Case "String".

When I ran my script, it got into an infinite loop. Hovering over the A icon in the Tray showed WinAPIInternals.au3 line 604 ReDim $aData[$aData[0] + $iIncrement]

I see that line 604 is in function __Inc

Is this a clue?

Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

Sorry if I misunderstand your point. AFAICT, my latest version of _VarDump works well and displays fields as expected.

Local $t = DllStructCreate("char a[3];handle b[3];uint c[35];byte d[128];wchar e[190000]; double f[3];int64 g[3];" & _
    "char h[3];float i;double j;byte k;ubyte l;short m;ushort n;int o;uint p;char q")
ConsoleWrite(_vardump($t) & @LF)

yields:

Struct       (380376) @:02E51D30 (structure alignment is unknown)
      char[3]         ''
      ptr[3]
                      0x00000000
                      0x00000000
                      0x00000000
      uint[35]
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      0
                      ... there are 15 more uint in this array
      byte[128]       0x00000000000000000000000000000000 ... 00000000000000000000000000000000
      wchar[190000]   ''
      double[3]
                      0.0
                      0.0
                      0.0
      int64[3]
                      0
                      0
                      0
      char[3]         ''
      float           0.0
      double          0.0
      byte            0
      byte            0
      short           0
      ushort          0
      int             0
      uint            0
      char            ''

I confess my function isn't as polished and documented as it should be but it wasn't initially meant for public consumption but personal use. Ellipsis in long lists (arrays) are normal (intended to shorten display) and can be disabled by an optional parameter. byte and ubyte types are indistinguishable when tested from AutoIt and make no difference anyway in practice.

I've made no effort to deduce struct alignment, nor nested structures. Yet I believe it would be possible to detect non-default struct align directives by painful and pedestrian examination of each field against a byte array struct union-ed to the input array. I've never felt the need for such complication.

I can [re]post the latest version of my code if required.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

jchd,

I have the version in post 7 of your thread, which works AOK to the Console.

My problem is that I am trying to incorporate your detection method into my cDebug example script.

Because the development version of cDebug already has the ability to display a struct where the user has told it what the tag is, I am trying to use your detection technique (and code) to tell my script what the tag is (the elements are) so it can call the function it has now to display the struct. The display will then be in the same format for both cases:

  • where the tag is specified, and
  • where it is detected.

I explain further below:

I now have working, in principle

$tag = 'char[3];byte ...')
$tStruct = DLLStructCreate($tag)
_GuiDebug(..,'test{tag}',$tStruct) => _cDebug_DumpStruct(($indent,$tstruct,$tag)

I am trying to add

$tStruct = DLLStructCreate($tag)
_GuiDebug(..,'test',$tStruct) => _cDebug_DumpStruct($indent,$tstruct,'')

with _cDebug_DumpStruct , when $tag is "", calling  _cDebug_DetectStructElements($tStruct) to detect, and return, $tag.

So I need to remove the Consolewrite part of your_VarDump  function, and instead have my detection function return a tag.

BTW My cDebug.au3 is already able to handle the case where an element of an array is a struct. When a tag is specified, between {} in the second argument, it displays (or dumps to Console) the array nesting and the structure. An example, with your test data (slightly modified):

#include "cDebug devel.au3"

Local $tag = "char a[3];handle b[3];uint c[35];byte d[128];wchar e[190000]; double f[3];int64 g[3];" & _
    "char h[3];float i;double j;byte k;ubyte l;short m;ushort n;int o;uint p;char q"

Local $struct = DllStructCreate($tag)
DllStructSetData($struct, 1, 'sos')
DllStructSetData($struct, 2, Ptr(123456789))
DllStructSetData($struct, 3, 8, 1)
DllStructSetData($struct, 3, 0x87654321, 2)
DllStructSetData($struct, 3, 256, 5)
DllStructSetData($struct, 4, Binary('sos'))
DllStructSetData($struct, 5, 'gno' & @CRLF & 'ji' & @TAB & 'o')
DllStructSetData($struct, 6, 3.1415926, 2)
DllStructSetData($struct, 7, 17, 1)
DllStructSetData($struct, 7, -1, 2)
DllStructSetData($struct, 8, 'end')
DllStructSetData($struct, 9, 2.7182818284590452353602874713527)
DllStructSetData($struct, 10, 2.7182818284590452353602874713527)
DllStructSetData($struct, 11, 107)
DllStructSetData($struct, 12, -108)
DllStructSetData($struct, 13, 109)
DllStructSetData($struct, 14, 110)
DllStructSetData($struct, 15, 111)
DllStructSetData($struct, 16, 112)

Local $c[2][0]
Local $e[2][2] = [[Null, Default], [_CDebug_DumpStr, MsgBox]]
Local Enum $p = 33333333333333
Opt("WinTitleMatchMode", 2)
Local $a[3][4] = [ _
    [$c, $e, ObjCreate("shell.application"), WinGetHandle("Dump.au3")], _
    ['zzz', 1/3, True, 0x123456], _
    [$struct, 93, Null, $p] _
]
_ChangeElementLimitForDebug(10)
_GuiDebug('Test example of moderate complexity','$a{'&$tag&'}',$a)

sends to the Console, or displays:

***Test example of moderate complexity-------------------------
$a <Array> Ubounds 3,4
      [0][0] => Array: Ubounds 2,0
      [0][1] =>  <Array> Ubounds 2,2
            [0][0] => <Keyword>       Null
            [0][1] => <Keyword>       Default
            [1][0] => <UserFunction>  _CDEBUG_DUMPSTR
            [1][1] => <Function>      MSGBOX
      [0][2] => <Object>     IShellDispatch6
      [0][3] => <Ptr>           0x00000000
      [1][0] => <String> (3 chars)   'zzz'
      [1][1] => <Double>        0.333333333333333
      [1][2] => <Bool>          True
      [1][3] => <Int32>         1193046
      [2][0] => <Struct> (380376 bytes) @:02797BD8
             1  char a[3]       => <String> (3 chars)   'sos'
            <alignment> 1
             2  handle b[3]    
                         => <Ptr>           0x075BCD15
                         => <Ptr>           0x00000000
                         => <Ptr>           0x00000000
             3  uint c[35]     
                         => <Int64>         8
                         => <Int64>         2271560481
                         => <Int64>         0
                         => <Int64>         0
                         => <Int64>         256
                         => <Int64>         0
                         => <Int64>         0
                         => <Int64>         0
                         => <Int64>         0
                         => <Int64>         0
            25 more elements)
             4  byte d[128]     => <Binary> (128 bytes)   0x736F7300000000000000 ... 0000000000000000000000
             5  wchar e[190000] => <String> (39 chars)   'gno<@CRLF>
                  ji<@TAB>o'
            <alignment> 4
             6  double f[3]    
                         => <Double>        0
                         => <Double>        3.1415926
                         => <Double>        0
             7  int64 g[3]     
                         => <Int64>         17
                         => <Int64>         -1
                         => <Int64>         0
             8  char h[3]       => <String> (3 chars)   'end'
            <alignment> 1
             9  float i         => <Double>        2.71828174591064
            10  double j        => <Double>        2.71828182845905
            11  byte k          => <Int32>         107
            12  ubyte l         => <Int32>         148
            13  short m         => <Int32>         109
            14  ushort n        => <Int32>         110
            <alignment> 2
            15  int o           => <Int32>         111
            16  uint p          => <Int64>         112
            17  char q          => <String> (0 char)   ''
            <alignment> 7
      [2][1] => <Int32>         93
      [2][2] => <Keyword>       Null
      [2][3] => <Int64>         33333333333333

While your _VarDump() works perfectly, my adaptation of it does not:

  • It returns only the first element
  • For the first element, it should return char[3]; but instead returns char;

I hope my explanation of the problem in this post is clearer.

I greatly appreciate having your code available to me.

Edited by c.haslam
Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

Put another way: the code near the end is:

If $idx>1 Then
If $nbElem=1 Then MsgBox(0,@ScriptLineNumber,'before index')
            $retTag &= '['&$idx&']'
        EndIf
        $retTag &= ';'
        $nbElem += 1
    WEnd
MsgBox(0,@ScriptLineNumber,'$retTag "'&$retTag&'"')
  • The MsgBox on line 2 works
  • The code on line 3 does nothing
  • Line 4 works
  • The While .. WEnd loop is not looping.
  • Commenting out line 2 does not improve the situation.
Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

awesome! if you add some ConsoleWrite logging or use log4a.au3 udf you could log all your variables and display them in a log. that way you could see that you had misnamed variables.

I love this one

 

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

OK I now see what it was all about. I read a little bit too fast and feel gealty for that o:)
Nice to see dust has settled.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

cDebug will output variables, constants and expressions to your choice of the Console, the Clipboard or a GUI (with a button to save the result to the Clipboard. V. 1.5 outputs structs as well, even if you don't tell it the tag: this last thanks to jchd. It is in Example Scripts.

Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...