Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 07/21/2016 in all areas

  1. If guiGetMsg() fits your needs then it's nice But - depending of the use, of course - GUIRegisterMsg() could sometimes be useful because it always work. Here is a simple test #include <GuiButton.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Global $g_idChk, $g_idMemo Example() Func Example() Local $hGUI $hGUI = GUICreate("Buttons", 400, 400) $g_idMemo = GUICtrlCreateEdit("", 119, 10, 276, 374, $WS_VSCROLL) GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New") $g_idChk = GUICtrlCreateCheckbox("Check1", 10, 120, 90, 50) GUIRegisterMsg($WM_COMMAND, "WM_COMMAND") GUISetState(@SW_SHOW) For $i = 1 to 50 MemoWrite($i ) Sleep(100) Next While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Exit EndFunc ;==>Example ; Write a line to the memo control Func MemoWrite($sMessage) GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1) EndFunc ;==>MemoWrite ; React on a button click Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg Local $nNotifyCode = BitShift($wParam, 16) Local $nID = BitAND($wParam, 0x0000FFFF) Local $hCtrl = $lParam Local $sText = "" Switch $nID Case $g_idChk Switch $nNotifyCode Case $BN_CLICKED $sText = "$BN_CLICKED" & @CRLF EndSwitch MemoWrite($sText & _ "-----------------------------" & @CRLF & _ "WM_COMMAND - Infos:" & @CRLF & _ "-----------------------------" & @CRLF & _ "Code" & @TAB & ":" & $nNotifyCode & @CRLF & _ "CtrlID" & @TAB & ":" & $nID & @CRLF & _ "CtrlHWnd:" & $hCtrl & @CRLF & _ (_GUICtrlButton_GetState($hCtrl) = 521 ? "checked" : "unchecked") & @CRLF) Return 0 ; Only workout clicking on the button EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_COMMAND
    1 point
  2. Why complicate things? WM_COMMAND is useful but not for such task since we have GuiGetMsg if is just check if a checkbox is checked or not! #include <GUIConstantsEx.au3> $Form = GUICreate("Form1", 251, 147, -1, -1) $Checkbox = GUICtrlCreateCheckbox("Checkbox", 72, 56) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Checkbox If GUICtrlRead($Checkbox) = $GUI_CHECKED Then ConsoleWrite("CHECKED" & @CRLF) ElseIf GUICtrlRead($Checkbox) = $GUI_UNCHECKED Then ConsoleWrite("UNCHECKED" & @CRLF) EndIf EndSwitch WEnd
    1 point
  3. Use WM_COMMAND for that Example slightly adapted from the help file : #include <GuiButton.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Global $g_idChk, $g_idMemo Example() Func Example() Local $hGUI $hGUI = GUICreate("Buttons", 400, 400) $g_idMemo = GUICtrlCreateEdit("", 119, 10, 276, 374, $WS_VSCROLL) GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New") $g_idChk = GUICtrlCreateCheckbox("Check1", 10, 120, 90, 50) GUIRegisterMsg($WM_COMMAND, "WM_COMMAND") GUISetState(@SW_SHOW) MemoWrite("$g_idChk : " & $g_idChk & @CRLF) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Exit EndFunc ;==>Example ; Write a line to the memo control Func MemoWrite($sMessage) GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1) EndFunc ;==>MemoWrite ; React on a button click Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg Local $nNotifyCode = BitShift($wParam, 16) Local $nID = BitAND($wParam, 0x0000FFFF) Local $hCtrl = $lParam Local $sText = "" Switch $nID Case $g_idChk Switch $nNotifyCode Case $BN_CLICKED $sText = "$BN_CLICKED" & @CRLF EndSwitch MemoWrite($sText & _ "-----------------------------" & @CRLF & _ "WM_COMMAND - Infos:" & @CRLF & _ "-----------------------------" & @CRLF & _ "Code" & @TAB & ":" & $nNotifyCode & @CRLF & _ "CtrlID" & @TAB & ":" & $nID & @CRLF & _ "CtrlHWnd:" & $hCtrl & @CRLF & _ (_GUICtrlButton_GetState($hCtrl) = 521 ? "checked" : "unchecked") & @CRLF) Return 0 ; Only workout clicking on the button EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_COMMAND
    1 point
  4. To turn it off for whatever reason, you do something like this: Global $pOriginalFunc = AddHookApi("user32.dll", "MessageBoxW", "Intercept_MessageBoxW", "int", "hwnd;wstr;wstr;uint") ;... The rest of the code here... ;... ; Back to original function: AddHookApi("user32.dll", "MessageBoxW", $pOriginalFunc) ... After that it's like it was never hooked.
    1 point
  5. Here, let me then: #include <WinApi.au3> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; AddHookApi("user32.dll", "MessageBoxW", "Intercept_MessageBoxW", "int", "hwnd;wstr;wstr;uint") Func Intercept_MessageBoxW($hWnd, $sText, $sTitle, $iType) Local $aCall = DllCall("user32.dll", "int", "MessageBoxW", _ "hwnd", $hWnd, _ "wstr", $sText, _ "wstr", StringReplace($sTitle, "AutoIt", @ScriptName), _ "uint", $iType) If @error Or Not $aCall[0] Then Return 0 Return $aCall[0] EndFunc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Let's try it ; Usual message box MsgBox(0, 'Test', 'Some text') ; Cause error that would say some AutoIt shit happened, but now it wouldn't say "AutoIt" DllStructCreate("byte[123456789097]") ; The End ; The magic is down below Func AddHookApi($sModuleName, $vFunctionName, $vNewFunction, $sRet = "", $sParams = "") Local Static $pImportDirectory, $hInstance Local Const $IMAGE_DIRECTORY_ENTRY_IMPORT = 1 If Not $pImportDirectory Then $hInstance = _WinAPI_GetModuleHandle(0) $pImportDirectory = ImageDirectoryEntryToData($hInstance, $IMAGE_DIRECTORY_ENTRY_IMPORT) If @error Then Return SetError(1, 0, 0) EndIf Local $iIsInt = IsInt($vFunctionName) Local $iRestore = Not IsString($vNewFunction) Local $tIMAGE_IMPORT_MODULE_DIRECTORY Local $pDirectoryOffset = $pImportDirectory Local $tModuleName Local $iInitialOffset, $iInitialOffset2 Local $iOffset2 Local $tBufferOffset2, $iBufferOffset2 Local $tBuffer, $tFunctionOffset, $pOld, $fMatch, $pModuleName, $pFuncName Local Const $PAGE_READWRITE = 0x04 While 1 $tIMAGE_IMPORT_MODULE_DIRECTORY = DllStructCreate("dword RVAOriginalFirstThunk;" & _ "dword TimeDateStamp;" & _ "dword ForwarderChain;" & _ "dword RVAModuleName;" & _ "dword RVAFirstThunk", _ $pDirectoryOffset) If Not DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAFirstThunk") Then ExitLoop $pModuleName = $hInstance + DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAModuleName") $tModuleName = DllStructCreate("char Name[" & _WinAPI_StringLenA($pModuleName) & "]", $pModuleName) If DllStructGetData($tModuleName, "Name") = $sModuleName Then ; function from this module $iInitialOffset = $hInstance + DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAFirstThunk") $iInitialOffset2 = $hInstance + DllStructGetData($tIMAGE_IMPORT_MODULE_DIRECTORY, "RVAOriginalFirstThunk") If $iInitialOffset2 = $hInstance Then $iInitialOffset2 = $iInitialOffset $iOffset2 = 0 While 1 $tBufferOffset2 = DllStructCreate("dword_ptr", $iInitialOffset2 + $iOffset2) $iBufferOffset2 = DllStructGetData($tBufferOffset2, 1) If Not $iBufferOffset2 Then ExitLoop If $iIsInt Then If BitAND($iBufferOffset2, 0xFFFFFF) = $vFunctionName Then $fMatch = True; wanted function Else $pFuncName = $hInstance + $iBufferOffset2 + 2 ; 2 is size od "word", see line below... $tBuffer = DllStructCreate("word Ordinal; char Name[" & _WinAPI_StringLenA($pFuncName) & "]", $hInstance + $iBufferOffset2) If DllStructGetData($tBuffer, "Name") == $vFunctionName Then $fMatch = True; wanted function EndIf If $fMatch Then $tFunctionOffset = DllStructCreate("ptr", $iInitialOffset + $iOffset2) VirtualProtect(DllStructGetPtr($tFunctionOffset), DllStructGetSize($tFunctionOffset), $PAGE_READWRITE) If @error Then Return SetError(3, 0, 0) $pOld = DllStructGetData($tFunctionOffset, 1) If $iRestore Then DllStructSetData($tFunctionOffset, 1, $vNewFunction) Else DllStructSetData($tFunctionOffset, 1, DllCallbackGetPtr(DllCallbackRegister($vNewFunction, $sRet, $sParams))) EndIf Return $pOld EndIf $iOffset2 += DllStructGetSize($tBufferOffset2) WEnd ExitLoop EndIf $pDirectoryOffset += 20 ; size of $tIMAGE_IMPORT_MODULE_DIRECTORY WEnd Return SetError(4, 0, 0) EndFunc Func VirtualProtect($pAddress, $iSize, $iProtection) Local $aCall = DllCall("kernel32.dll", "bool", "VirtualProtect", "ptr", $pAddress, "dword_ptr", $iSize, "dword", $iProtection, "dword*", 0) If @error Or Not $aCall[0] Then Return SetError(1, 0, 0) Return 1 EndFunc Func ImageDirectoryEntryToData($hInstance, $iDirectoryEntry) ; Get pointer to data Local $pPointer = $hInstance ; Start processing passed binary data. 'Reading' PE format follows. Local $tIMAGE_DOS_HEADER = DllStructCreate("char Magic[2];" & _ "word BytesOnLastPage;" & _ "word Pages;" & _ "word Relocations;" & _ "word SizeofHeader;" & _ "word MinimumExtra;" & _ "word MaximumExtra;" & _ "word SS;" & _ "word SP;" & _ "word Checksum;" & _ "word IP;" & _ "word CS;" & _ "word Relocation;" & _ "word Overlay;" & _ "char Reserved[8];" & _ "word OEMIdentifier;" & _ "word OEMInformation;" & _ "char Reserved2[20];" & _ "dword AddressOfNewExeHeader", _ $pPointer) Local $sMagic = DllStructGetData($tIMAGE_DOS_HEADER, "Magic") ; Check if it's valid format If Not ($sMagic == "MZ") Then Return SetError(1, 0, 0) ; MS-DOS header missing. Btw 'MZ' are the initials of Mark Zbikowski in case you didn't know. ; Move pointer $pPointer += DllStructGetData($tIMAGE_DOS_HEADER, "AddressOfNewExeHeader") ; move to PE file header ; In place of IMAGE_NT_SIGNATURE structure Local $tIMAGE_NT_SIGNATURE = DllStructCreate("dword Signature", $pPointer) ; Check signature If DllStructGetData($tIMAGE_NT_SIGNATURE, "Signature") <> 17744 Then ; IMAGE_NT_SIGNATURE Return SetError(2, 0, 0) ; wrong signature. For PE image should be "PE\0\0" or 17744 dword. EndIf ; Move pointer $pPointer += 4 ; size of $tIMAGE_NT_SIGNATURE structure ; In place of IMAGE_FILE_HEADER structure ; Move pointer $pPointer += 20 ; size of $tIMAGE_FILE_HEADER structure ; Determine the type Local $tMagic = DllStructCreate("word Magic;", $pPointer) Local $iMagic = DllStructGetData($tMagic, 1) Local $tIMAGE_OPTIONAL_HEADER If $iMagic = 267 Then ; x86 version ; Move pointer $pPointer += 96 ; size of $tIMAGE_OPTIONAL_HEADER ElseIf $iMagic = 523 Then ; x64 version ; Move pointer $pPointer += 112 ; size of $tIMAGE_OPTIONAL_HEADER Else Return SetError(3, 0, 0) ; unsupported module type EndIf ; Validate input by checking available number of structures that are in the module Local Const $IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16 ; predefined value that PE modules always use (AutoIt certainly) If $iDirectoryEntry > $IMAGE_NUMBEROF_DIRECTORY_ENTRIES - 1 Then Return SetError(4, 0, 0) ; invalid input ; Calculate the offset to wanted entry (every entry is 8 bytes) $pPointer += 8 * $iDirectoryEntry ; At place of correst directory entry Local $tIMAGE_DIRECTORY_ENTRY = DllStructCreate("dword VirtualAddress; dword Size", $pPointer) ; Collect data Local $pAddress = DllStructGetData($tIMAGE_DIRECTORY_ENTRY, "VirtualAddress") If $pAddress = 0 Then Return SetError(5, 0, 0) ; invalid input ; $pAddress is RVA, add it to base address Return $hInstance + $pAddress EndFuncedit: because this is better than before.
    1 point
  6. Hello group, I've been trying almost everything possible to convert strings between ANSI (I personaly use Latin-1 codepage) and UTF-8 or UTF-16, but I've had no real success up to now. I need this because I have to deal with a pure ANSI database using an ODBC layer, a complex GUI interface and two separate SQLite3 (v3.6.13) databases (one utf-8 and one utf-16). Given that this will be routinely used with a significant dataflow volume, I'd like to know which would be the most practical (and if possible efficient) way. I may be misunderstanding obvious things, but it seems to me that many standard UDF or functions are still working in ANSI mode. Are simple GUI controls (say InputBoxes) delivering ANSI or UTF-8 strings? What is the SQLite3 interface expecting when it comes to data format? In the same direction, why are UTF-16 (improperly called Unicode) strings passed to/from in dll calls thru obscure structures instead of the wstr type? Aren't wstr's first class citizens? Another question haunting me: how can I hex-dump a string without going into any conversion? StringToBinary is not an option since it forces you to declare which format the string is using, which is precisely what I need to know! I really can't understand how all this is supposed to be used in simple or more complex developments! I apologize for asking that much, but it's a consequence of me wasting ___way___ too much time trying to solve these issues. Warm thanks in advance for any help.
    1 point
×
×
  • Create New...