Jump to content
Sign in to follow this  
erifash

Binary trouble

Recommended Posts

erifash

Hello, I am trying to recreate Larry's binary read/write functions in two functions: _BinRead() and _BinWrite(). They use hex formatted strings like Larry's, but are not comma seperated. The binary read function works fine, the only problem is with the binary write function. For some reason it will create a 0-byte file and not write data to it :o . The code looks fine to me but it doesn't work, so can someone else please take a look at it?

Func _BinWrite( $sFile, $sHex )

    Local $GENERIC_WRITE = 0x40000000, $OPEN_ALWAYS = 4, $FILE_ATTRIBUTE_NORMAL = 0x00000080
    Local $c = -1
    Local $hDLL = DllOpen("kernel32.dll")
    Local $iBytes = StringLen($sHex) / 2
    Local $hFile = DllCall($hDLL, "hwnd", "CreateFile", "str", $sFile, "long", $GENERIC_WRITE, "long", 0, "ptr", 0, "long", $OPEN_ALWAYS, "long", $FILE_ATTRIBUTE_NORMAL, "long", 0)
    Local $AFW_ptr = DllStructCreate("byte[" & $iBytes & "]")

    For $i = 1 to $iBytes
        $c += 2
        DllStructSetData($AFW_ptr, 1, Dec(StringMid($sHex, $c, 2)), $i)
    Next

    Local $AFW_r = DllCall($hDLL, "int", "WriteFile", "hwnd", $hFile, "ptr", DllStructGetPtr($AFW_ptr), "long", $iBytes, "long_ptr", 0, "ptr", 0)

    DllStructDelete($AFW_ptr)
    DllCall($hDLL, "int", "CloseHandle", "hwnd", $hFile[0])
    DllClose($hDLL)

    SetError($AFW_r[0])
    Return $AFW_r[4]

EndFunc
;Example useage:
_BinWrite("num.txt", "3132333435")
;Writes the string "12345" to num.txt

Thanks. B)

Edited by erifash

Share this post


Link to post
Share on other sites
Valuater

is this correct

Local $sHex = "3132333435"

Local $iBytes = StringLen($sHex) / 2

MsgBox(0,"Test", $iBytes)

message box returns the number 5

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
erifash

Yes, it finds out how many hex digits to set into struct $AFW_ptr. 2 hex digits = 1 byte.

Edited by erifash

Share this post


Link to post
Share on other sites
erifash

Share this post


Link to post
Share on other sites
erifash

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.