Jump to content

Binary trouble


erifash
 Share

Recommended Posts

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
Link to comment
Share on other sites

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

Edited by erifash
Link to comment
Share on other sites

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...