Jump to content

Recommended Posts

Posted (edited)

I figured i would edit this, I was originally having problems with this script but it seems to be working fine now after some exhaustive work.  Just a few functions.  One takes a variable and gets a pointer to it and the other just takes the pointer and pulls out the underlying data.  I figured this might be useful to someone in the future looking to use c++ style pointer.  I plan to add a function to write to the pointer in the near future. I'd also like to say that the _GetPtr() function beats the pants off of the built in autoit ptr() function....

;;;pointers and dereferenceing
#include <Array.au3>
HotKeySet("{ESC}","exit1")
Global $PointerTracker,$DummyStruct



$a="mark"
$b=-15.34334534
$c=True
$d=Binary("Gerbal")
$e=32
Global $f[5]=["mark", 13.4332, True, "Gerbal", 32] ;arrays arn't compatible, the array is the pointer to the underlying data

;~ $f=DllStructCreate("struct;int;endstruct")
;~ DllStructSetData($f,1,987)
;~ MsgBox('','',_GetPtr($a))
;~ $f=_GetPtr($a)
;~ $g=_GetPtr($b)
;~ $h=_GetPtr($c)
;~ $i=_GetPtr($d)
;~ $j=_GetPtr($e)


$a=_GetPtr($a)
$b=_GetPtr($b)
$c=_GetPtr($c)
$d=_GetPtr($d)
$e=_GetPtr($e)
;~ $g=_GetPtr($f)  can get pointer but not dereference dllstruct type variable maybe implemented in the future


$a=_Dereference($a)
$b=_Dereference($b)
$c=_Dereference($c)
$d=_Dereference($d)
$e=_Dereference($e)
;~ $f=_Dereference($f)

ConsoleWrite($a & @CRLF)
ConsoleWrite($b & @CRLF)
ConsoleWrite($c & @CRLF)
ConsoleWrite(BinaryToString($d) & @CRLF)
ConsoleWrite($e & @CRLF)
;~ ConsoleWrite($g & @CRLF)
;~ ConsoleWrite(VarGetType($g) & @CRLF)

;~ $l=_Dereference($f)
;~ MsgBox('','',VarGetType($l))

;~ $m=_Dereference($g)
;~ MsgBox('','',VarGetType($m))

;~ $n=_Dereference($h)
;~ MsgBox('','',VarGetType($n))

;~ $k=_Dereference($i)
;~ MsgBox('','',VarGetType($k))

;~ $o=_Dereference($j)
;~ MsgBox('','',VarGetType($o))

;~ ConsoleWrite($l & @CRLF)
;~ ConsoleWrite($m & @CRLF)
;~ ConsoleWrite($n & @CRLF)
;~ ConsoleWrite(BinaryToString($k) & @CRLF)
;~ ConsoleWrite($o & @CRLF)



;~ while 1
;~ MsgBox("","",$l,1)
;~ MsgBox("","",$m,1)
;~ MsgBox("","",$n,1)
;~ MsgBox("","",BinaryToString($k),1)
;~ MsgBox("","",$o,1)
;~ WEnd


_PointerDelete()
$f=0


func _Dereference($var) ;_Dereference($pointer)

;_ArrayDisplay($PointerTracker)
  $DataType=VarGetType($var)                               ;[$x][0]=value  x[1]=Tag x[2]=pointer [3]datatype
if $DataType<>"ptr" Then
    MsgBox('','ERROR',"Var Must Be a Pointer!!")
    Exit
EndIf

$result=""
for $x=0 to UBound($PointerTracker)-1

    if $PointerTracker[$x][2]=$var then    ;match

            if $PointerTracker[$x][3]="dllstruct" Then
                MsgBox('',"ERROR","Cannot Dereference DllStruct type variable")
                    Exit
                    EndIf

                $result=DllStructGetData($DummyStruct[$x],1)


;~                          ConsoleWrite($result & @CRLF)
;~                               ConsoleWrite($result & @CRLF & VarGetType($result) & @CRLF)
;~                                   _ArrayDisplay($PointerTracker)
;~                                          MsgBox('','',$PointerTracker[$x][3])                ;[$x][0]=value  x[1]=Tag x[2]=pointer [3]datatype
            if $PointerTracker[$x][3]="int"  Then
                Return $result
                    Else
                        Return BinaryTostring($result)
                    EndIf
        EndIf
Next

Exit

EndFunc

func _GetPtr($var)
local $tag="",$len=""
$DataType=VarGetType($var)


if $DataType<>"int32" and $DataType<>"dllstruct" Then
$var=String($var)
$var=binary($var)
    ElseIf $DataType="int32" Then
        $DataType="int"
EndIf


                                                             ;[$x][0]=value  x[1]=Tag x[2]=pointer  [3]datatype
if Not IsArray($PointerTracker) Then
    dim $PointerTracker[1][4]
elseif IsArray($PointerTracker) Then
    ReDim $PointerTracker[UBound($PointerTracker)+1][4]
EndIf



$PointerTracker[UBound($PointerTracker)-1][3]=$DataType   ;saves the data type
$PointerTracker[UBound($PointerTracker)-1][0]=$var   ;just saves the original value


if $DataType<>"int" and $DataType<>"dllstruct" Then
    $len=BinaryLen($var)
$tag="struct;align 1;byte[" & $len & "];endstruct"
ElseIf $DataType="int" Then
    $tag="struct;int;endstruct"
ElseIf $DataType="dllstruct" Then
    $bin=Binary($var)
    $len=BinaryLen($bin)
    $tag="struct;align 1;byte[" & $len & "];endstruct"
    EndIf

$PointerTracker[UBound($PointerTracker)-1][1]=$tag       ;saves the tag ;unnecessary but whatever


if not IsArray($DummyStruct) Then ;first time through
    dim $DummyStruct[1]

        if IsArray($DummyStruct) Then
            if $DataType<>"dllstruct" Then

                do
                    $DummyStruct[0]=DllStructCreate($tag)
                        sleep(150)
                    Until @error=0

                do
                        $success=DllStructSetData($DummyStruct[0],1,$var)
                    Until $success=$var
            EndIf

            if $DataType="dllstruct" Then
                $DummyStruct[0]=$var
                EndIf
;~ MsgBox('','',BinaryToString($var))
;~ _ArrayDisplay($PointerTracker)
        $PointerTracker[0][2]=DllStructGetPtr($DummyStruct[0],1)
Return $PointerTracker[0][2]
EndIf
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Elseif UBound($DummyStruct)>=1 Then ;subsequent times through
      ReDim $DummyStruct[UBound($DummyStruct)+1]

    if IsArray($DummyStruct) Then
        if $DataType<>"dllstruct" Then
                    Do
                            $DummyStruct[UBound($DummyStruct)-1]=DllStructCreate($tag)
                        Until @error=0

                    do
                            $success=DllStructSetData($DummyStruct[UBound($DummyStruct)-1],1,$var)
                        Until $success=$var
        EndIf
            if $DataType="dllstruct" Then
                $DummyStruct[UBound($DummyStruct)-1]=$var
                EndIf


            $PointerTracker[UBound($PointerTracker)-1][2]=DllStructGetPtr($DummyStruct[UBound($DummyStruct)-1],1)
;~ _ArrayDisplay($PointerTracker)
        Return $PointerTracker[UBound($PointerTracker)-1][2]
    EndIf

                                                                                         ;[$x][0]=value  x[1]=Tag x[2]=pointer  [3]datatype

EndIf
EndFunc

Func _PointerDelete()

    _ArrayDelete($DummyStruct, "0-" & UBound($DummyStruct)-1)


EndFunc

func exit1()
    Exit
    EndFunc

 

Edited by markyrocks
Posted

Idk. I've tested this in a variety of ways and my best guess is that the script is running faster than the memory can be read an wrote to.  It works perfectly as far as I can tell.   Just weird that the results change when the array display is deactivated.... maybe I should  add a do until loop to slow it down until the expected result is exactly what its supposed to be...

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
×
×
  • Create New...