Jump to content

Inserting string into the beginning


Recommended Posts

I've used code submitted earlier on this forum:

#Include <EditConstants.au3>
#Include <WinAPI.au3>
#Include <WindowsConstants.au3>
Global $sFile, $hFile, $iSize, $sData, $tData, $iRead
$sFile = @WindowsDir & '\regedit.exe'
$iSize = FileGetSize($sFile)
$tData = DllStructCreate('byte[' & $iSize & ']')
$hFile = _WinAPI_CreateFile($sFile, 2, 2, 2)
_WinAPI_ReadFile($hFile, DllStructGetPtr($tData), $iSize, $iRead)
_WinAPI_CloseHandle($hFile)
MsgBox(0, '', Hex(DllStructGetData($tData, 1, 1), 2) & Hex(DllStructGetData($tData, 1, 2), 2) & ' (' & Chr(DllStructGetData($tData, 1, 1)) & Chr(DllStructGetData($tData, 1, 2)) & ') - signature for EXE fies.')
$sData = ''
For $i = 1 To $iSize
$sData &= Hex(DllStructGetData($tData, 1, $i), 2)
If Mod($i, 16) = 0 Then
     $sData &= @CRLF
Else
     $sData &= ' '
EndIf
Next
$sData = StringTrimRight($sData, 2)
Global $Edit
GUICreate('MyGUI', 422, 526)
$Edit = GUICtrlCreateEdit('', 10, 10, 402, 506, BitOR($ES_READONLY, $WS_VSCROLL, $WS_HSCROLL))
GUICtrlSetFont(-1, 8.5, 400, 0, 'Courier')
GUICtrlSetData($Edit, $sData)
GUISetState()
Do
Until GUIGetMsg() = -3

and it's working great! But I need to get the data in reversed order therefore I used

$sData = Hex(DllStructGetData($tData, 1, $i), 2) & $sData

It is working, but obviously it's painfully slow.

Does anyone have any ideas how to make the process quicker. I'd really gratefull.

Edited by RoderickRM
Link to comment
Share on other sites

Maybe not exactly what you're looking for, but a hell of a lot faster :)... function originally posted by trancexx

Edit:

Looking at the function CryptBinaryToString the output can tweaked to look more like your original one by using different dwFlags (only that there are two spaces right in the middle)... *test*test*, example #4 replaces those manually to mimic your example exactly... and :lol:, for me it's even faster than example #3 because parsing that data to the edit controls takes less time.

#include <EditConstants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
Global $sFile, $hFile, $iSize, $sData, $tData, $iRead
$sFile = @WindowsDir & '\regedit.exe'
$iSize = FileGetSize($sFile)
$tData = DllStructCreate('byte[' & $iSize & ']')
$hFile = _WinAPI_CreateFile($sFile, 2, 2, 2)
_WinAPI_ReadFile($hFile, DllStructGetPtr($tData), $iSize, $iRead)
_WinAPI_CloseHandle($hFile)
MsgBox(0, '', Hex(DllStructGetData($tData, 1, 1), 2) & Hex(DllStructGetData($tData, 1, 2), 2) & ' (' & Chr(DllStructGetData($tData, 1, 1)) & Chr(DllStructGetData($tData, 1, 2)) & ') - signature for EXE fies.')

$sData = ''
$timer = TimerInit()
For $i = 1 To $iSize
    $sData &= Hex(DllStructGetData($tData, 1, $i), 2)
    If Mod($i, 16) = 0 Then
        $sData &= @CRLF
    Else
        $sData &= ' '
    EndIf
Next
$sData = StringTrimRight($sData, 2)
GUICreate('1) DllStructGetData()', 422, 526)
$Edit = GUICtrlCreateEdit('', 10, 10, 402, 506, BitOR($ES_READONLY, $WS_VSCROLL, $WS_HSCROLL))
GUICtrlSetFont(-1, 8.5, 400, 0, 'Courier')
GUICtrlSetData($Edit, $sData)
GUISetState()
ConsoleWrite("1) " & TimerDiff($timer) & @CRLF)

$timer = TimerInit()
$sData = _HexEncode(DllStructGetData($tData, 1))
GUICreate('2) CryptBinaryToString - 0x0000000b', 422, 526)
$Edit2 = GUICtrlCreateEdit('', 10, 10, 402, 506, BitOR($ES_READONLY, $WS_VSCROLL, $WS_HSCROLL))
GUICtrlSetFont(-1, 8.5, 400, 0, 'Courier')
GUICtrlSetData($Edit2, $sData)
GUISetState()
ConsoleWrite("2) " & TimerDiff($timer) & @CRLF)

$timer = TimerInit()
$sData = _HexEncode(DllStructGetData($tData, 1), 0x00000004)
GUICreate('3) CryptBinaryToString - 0x00000004', 422, 526)
$Edit3 = GUICtrlCreateEdit('', 10, 10, 402, 506, BitOR($ES_READONLY, $WS_VSCROLL, $WS_HSCROLL))
GUICtrlSetFont(-1, 8.5, 400, 0, 'Courier')
GUICtrlSetData($Edit3, $sData)
GUISetState()
ConsoleWrite("3) " & TimerDiff($timer) & @CRLF)

$timer = TimerInit()
$sData = _HexEncode(DllStructGetData($tData, 1), 0x00000004)
GUICreate('4) CryptBinaryToString - 0x00000004 + StringReplace()', 422, 526)
$Edit4 = GUICtrlCreateEdit('', 10, 10, 402, 506, BitOR($ES_READONLY, $WS_VSCROLL, $WS_HSCROLL))
GUICtrlSetFont(-1, 8.5, 400, 0, 'Courier')
GUICtrlSetData($Edit4, StringReplace($sData, "  ", " ", 0, 2))
GUISetState()
ConsoleWrite("4) " & TimerDiff($timer) & @CRLF)

Do
Until GUIGetMsg() = -3


Func _HexEncode($bInput, $iFlags = 0x0000000b)
    ; CryptBinaryToString function (Windows)
    ; http://msdn.microsoft.com/en-us/library/windows/desktop/aa379887%28v=vs.85%29.aspx

    Local $tInput = DllStructCreate("byte[" & BinaryLen($bInput) & "]")
    DllStructSetData($tInput, 1, $bInput)
    Local $a_iCall = DllCall("crypt32.dll", "int", "CryptBinaryToString", _
            "ptr", DllStructGetPtr($tInput), _
            "dword", DllStructGetSize($tInput), _
            "dword", $iFlags, _
            "ptr", 0, _
            "dword*", 0)

    If @error Or Not $a_iCall[0] Then
        Return SetError(1, 0, "")
    EndIf

    Local $iSize = $a_iCall[5]
    Local $tOut = DllStructCreate("char[" & $iSize & "]")

    $a_iCall = DllCall("crypt32.dll", "int", "CryptBinaryToString", _
            "ptr", DllStructGetPtr($tInput), _
            "dword", DllStructGetSize($tInput), _
            "dword", $iFlags, _
            "ptr", DllStructGetPtr($tOut), _
            "dword*", $iSize)

    If @error Or Not $a_iCall[0] Then
        Return SetError(2, 0, "")
    EndIf

    Return SetError(0, 0, DllStructGetData($tOut, 1))

EndFunc   ;==>_HexEncode
Edited by KaFu
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...