Jump to content

ill a byte array from DllStructGetData($lpBuffer, 1)


Go to solution Solved by funkey,

Recommended Posts

I'm trying to fill a byte array from DllStructGetData($lpBuffer, 1)

I can't figure out how to do it, the code I have is currently:
$data=DllStructGetData($lpBuffer, 1)
which seems to create a string starting with 0x then the rest is in hex.

While I have made my code work by using string manipulation to get the data I want, I could really do with this as normal binary bytes.

Can anyone point me in the right direction?

Cheers,

Dave

Link to comment
Share on other sites

This is what I get:

0x0000000000000000000000000000000000000000001FF100011800002E801F00F36400000104001C000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000403040300000000FFFFFFFF001600008000000000003000000000000000426F1DD701007988AF6C0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020

I used FileWrite($hFileOpen, $data) to grab the $data

The result is that $data is a string 514 chars long, what I would like is an array[] 256 bytes long containing the $data

At the moment I'm doing a lot of

Dec(StringMid($data,($ByteNum*2)+3,2))

to interrogate the $data. An array[] of bytes would be a lot simpler, but I can't see a simple way to do it.

Link to comment
Share on other sites

  • Solution

#include <Array.au3>

$lpBuffer = DllStructCreate('BYTE[256]')
;data is fed into the struct via a serial port at this point in my code
$data=DllStructGetData($lpBuffer, 1)

;~ msgbox (0,"",$data)

$MyArray = _BinaryToArray($data)
_ArrayDisplay($MyArray)

Func _BinaryToArray($bData)
    Local $iCount = BinaryLen($bData)
    Local $aRet[$iCount]
    For $i = 1 To $iCount
        $aRet[$i - 1] = BinaryMid($bData, $i, 1)
    Next
    Return $aRet
EndFunc

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

I did try that, but I got a 0 byte file

#include <Array.au3>
#include <File.au3>
$lpBuffer = DllStructCreate('BYTE[256]')
$data=DllStructGetData($lpBuffer, 1)
$MyFile = "C:\AutoIt\a.bin"
_FileCreate($MyFile)
$hFileOpen = FileOpen($MyFile, BitOr($FO_READ, $FO_OVERWRITE, $FO_CREATEPATH, $FO_BINARY ))
$MyArray = _BinaryToArray($data)
FileWrite($hFileOpen, $MyArray)
FileClose($hFileOpen)

;_ArrayDisplay($MyArray)

Func _BinaryToArray($bData)
    Local $iCount = BinaryLen($bData)
    Local $aRet[$iCount]
    For $i = 1 To $iCount
        $aRet[$i - 1] = BinaryMid($bData, $i, 1)
    Next
    Return $aRet
EndFunc
Link to comment
Share on other sites

Might be something like...

#include <Array.au3>

$lpBuffer = DllStructCreate('BYTE[256]')
;data is fed into the struct via a serial port at this point in my code
$data=DllStructGetData($lpBuffer, 1)

;~ msgbox (0,"",$data)

$MyArray = _BinaryToArray($data)
_ArrayDisplay($MyArray)
_BinaryArrayToFile($MyArray)

Func _BinaryArrayToFile(ByRef $array)
    Local $hFile = FileOpen("file.bin", 16 + 2) ;Binary, Overwite
    _FileWriteFromArray($hFile, $array)
    FileClose($hFile)
EndFunc

Func _BinaryToArray($bData)
    Local $iCount = BinaryLen($bData)
    Local $aRet[$iCount]
    For $i = 1 To $iCount
        $aRet[$i - 1] = BinaryMid($bData, $i, 1)
    Next
    Return $aRet
EndFunc

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Something like this? I think the empty array in autoit is a problem, it has no datatype..

#include <Array.au3>
#include <File.au3>
$lpBuffer = DllStructCreate('BYTE[256]')
$data=DllStructGetData($lpBuffer, 1)
$MyFile = "C:\a.bin"
_FileCreate($MyFile)
$hFileOpen = FileOpen($MyFile, BitOr($FO_OVERWRITE, $FO_CREATEPATH, $FO_BINARY ))
$MyArray = _BinaryToArray($data)
FileWrite($hFileOpen, DllStructGetData($lpBuffer, 1))
FileClose($hFileOpen)

;_ArrayDisplay($MyArray)

Func _BinaryToArray($bData)
    Local $iCount = BinaryLen($bData)
    ConsoleWrite("Count := " &$iCount&@CRLF)
    Local $aRet[$iCount]
    For $i = 1 To $iCount
        $aRet[$i - 1] = BinaryMid($bData, $i, 1)
    Next
    Return $aRet
EndFunc
Link to comment
Share on other sites

Why going through an array is beyond me:

#include <File.au3>
$lpBuffer = DllStructCreate('BYTE[256]')
;data is fed into the struct via a serial port at this point in my code
; sample data
For $i = 1 To 256
    DllStructSetData($lpBuffer, 1, $i - 1, $i)
Next
Local $hOut = FileOpen("data.bin", BitOr($FO_OVERWRITE, $FO_CREATEPATH, $FO_BINARY))
$data=DllStructGetData($lpBuffer, 1)
FileWrite($hOut, $data)
FileClose($hOut)

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

That does the trick. I was originally saving $data, but I had a timestamp in there as well which was causing $data to get converted to a string :(

jchd, the array is there to solve a different problem (easier interrogation of the data than the method I had)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...