Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 09/20/2012 in all areas

  1. LockFile allows you to lock a file to the current process. This is useful if you want to interact with a specific file but wish to avoid the accidental deletion by another process or worse still a user. Examples have been provided and any advice for improvements is much appreciated. UDF: #include-once ; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7 ; #INDEX# ======================================================================================================================= ; Title .........: Lock_File ; AutoIt Version : v3.3.10.0 or higher ; Language ......: English ; Description ...: Lock a file to the current process only. Any attempts to interact with the file by another process will fail ; Note ..........: ; Author(s) .....: guinness ; Remarks .......: The locked file handle must be closed with the Lock_Unlock() function after use ; =============================================================================================================================== ; #INCLUDES# ========================================================================================================= #include <WinAPI.au3> ; #GLOBAL VARIABLES# ================================================================================================= ; None ; #CURRENT# ===================================================================================================================== ; Lock_Erase: Erase the contents of a locked file ; Lock_File: Lock a file to the current process only ; Lock_Read: Read data from a locked file ; Lock_Reduce: Reduce the locked file by a certain percentage ; Lock_Write: Write data to a locked file ; Lock_Unlock: Unlock a file so other processes can interact with it ; =============================================================================================================================== ; #INTERNAL_USE_ONLY#============================================================================================================ ; None ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: Lock_Erase ; Description ...: Erase the contents of a locked file ; Syntax ........: Lock_Erase($hFile) ; Parameters ....: $hFile - Handle returned by Lock_File() ; Return values .: Success - True ; Failure - False, use _WinAPI_GetLastError() to get additional details ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func Lock_Erase($hFile) _WinAPI_SetFilePointer($hFile, $FILE_BEGIN) Return _WinAPI_SetEndOfFile($hFile) EndFunc ;==>Lock_Erase ; #FUNCTION# ==================================================================================================================== ; Name ..........: Lock_File ; Description ...: Lock a file to the current process only ; Syntax ........: Lock_File($sFilePath[, $bCreateNotExist = False]) ; Parameters ....: $sFilePath - Filepath of the file to lock ; $bCreateNotExist - [optional] Create the file if it doesn't exist (True) or don't create (False). Default is False ; Return values .: Success - Handle of the locked file ; Failure - Zero and sets @error to non-zero. Call _WinAPI_GetLastError() to get extended error information ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func Lock_File($sFilePath, $bCreateNotExist = False) Return _WinAPI_CreateFile($sFilePath, BitOR($CREATE_ALWAYS, (IsBool($bCreateNotExist) And $bCreateNotExist ? $CREATE_NEW : 0)), BitOR($FILE_SHARE_WRITE, $FILE_SHARE_DELETE), 0, 0, 0) ; Creation = 2, Access = 2 + 4, Sharing = 0, Attributes = 0, Security = 0 EndFunc ;==>Lock_File ; #FUNCTION# ==================================================================================================================== ; Name ..........: Lock_Read ; Description ...: Read data from a locked file ; Syntax ........: Lock_Read($hFile) ; Parameters ....: $hFile - Handle returned by Lock_File() ; $iBinaryFlag - [optional] Flag value to pass to BinaryToString(). Default is $SB_UTF8. See BinaryToString() documentation for more details ; Return values .: Success - Data read from the file ; Failure - Empty string and sets @error to non-zero ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func Lock_Read($hFile, $iBinaryFlag = $SB_UTF8) Local $iFileSize = _WinAPI_GetFileSizeEx($hFile) + 1, _ $sText = '' Local $tBuffer = DllStructCreate('byte buffer[' & $iFileSize & ']') _WinAPI_SetFilePointer($hFile, $FILE_BEGIN) _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), $iFileSize, $sText) Return SetError(@error, 0, BinaryToString(DllStructGetData($tBuffer, 'buffer'), $iBinaryFlag)) EndFunc ;==>Lock_Read ; #FUNCTION# ==================================================================================================================== ; Name ..........: Lock_Reduce ; Description ...: Reduce the locked file by a certain percentage ; Syntax ........: Lock_Reduce($hFile, $iPercentage) ; Parameters ....: $hFile - Handle returned by Lock_File() ; $iPercentage - A percentage value to reduce the file by ; Return values .: Success - True ; Failure - False. Use _WinAPI_GetLastError() to get additional details ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func Lock_Reduce($hFile, $iPercentage) If Not IsInt($iPercentage) Then $iPercentage = Int($iPercentage) If $iPercentage > 0 And $iPercentage < 100 Then Local $iFileSize = _WinAPI_GetFileSizeEx($hFile) * ($iPercentage / 100) _WinAPI_SetFilePointer($hFile, $iFileSize) Return _WinAPI_SetEndOfFile($hFile) EndIf Return Lock_Erase($hFile) EndFunc ;==>Lock_Reduce ; #FUNCTION# ==================================================================================================================== ; Name ..........: Lock_Write ; Description ...: Write data to a locked file ; Syntax ........: Lock_Write($hFile, $sText) ; Parameters ....: $hFile - Handle returned by Lock_File() ; $sText - Data to be written to the locked file ; Return values .: Success - Number of bytes written to the file ; Failure - 0 and sets @error to non-zero ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func Lock_Write($hFile, $sText) Local $iFileSize = _WinAPI_GetFileSizeEx($hFile), _ $iLength = StringLen($sText) Local $tBuffer = DllStructCreate('byte buffer[' & $iLength & ']') DllStructSetData($tBuffer, 'buffer', $sText) _WinAPI_SetFilePointer($hFile, $iFileSize) Local $iWritten = 0 _WinAPI_WriteFile($hFile, DllStructGetPtr($tBuffer), $iLength, $iWritten) Return SetError(@error, @extended, $iWritten) ; Number of bytes written EndFunc ;==>Lock_Write ; #FUNCTION# ==================================================================================================================== ; Name ..........: Lock_Unlock ; Description ...: Unlock a file so other applications can interact with it ; Syntax ........: Lock_Unlock($hFile) ; Parameters ....: $hFile - Handle returned by Lock_File() ; Return values .: Success - True ; Failure - False ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func Lock_Unlock($hFile) Return _WinAPI_CloseHandle($hFile) EndFunc ;==>Lock_Unlock Example 1: (with LockFile) #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> ; Include the LockFile.au3 UDF #include 'LockFile.au3' ; LockFile by guinness Example_1() Func Example_1() ; Path of the locked file Local Const $sFilePath = _WinAPI_GetTempFileName(@TempDir) ; Lock the file to this process and create it if not already done so Local $hLock = Lock_File($sFilePath, True) ; Erase the contents of the locked file Lock_Erase($hLock) ; Write random data to the locked file For $i = 1 To 5 Lock_Write($hLock, RandomText(10) & @CRLF) Next ; Read the locked file Local $sRead = Lock_Read($hLock) ; Display the contents of the locked file that was just read MsgBox($MB_SYSTEMMODAL, '', $sRead) ; Display the current file size of the locked file. For example 60 bytes MsgBox($MB_SYSTEMMODAL, '', ByteSuffix(_WinAPI_GetFileSizeEx($hLock))) ; Reduce the file size by 50% Lock_Reduce($hLock, 50) ; Display the reduced size. This will be 50% less than before. For example 30 bytes MsgBox($MB_SYSTEMMODAL, '', ByteSuffix(_WinAPI_GetFileSizeEx($hLock))) ; Delete the locked file. As this is locked the deletion will fail MsgBox($MB_SYSTEMMODAL, '', 'Delete the locked file: ' & _ @CRLF & _ @CRLF & _ FileDelete($sFilePath) & ' (this will return 0, as the file is currently locked).') ; Unlock the locked file Lock_Unlock($hLock) ; Delete the file as it is now unlocked MsgBox($MB_SYSTEMMODAL, '', 'Delete the locked file: ' & _ @CRLF & _ @CRLF & _ FileDelete($sFilePath) & ' (this will return 1, as the file is unlocked).') EndFunc ;==>Example_1 ; Convert a boolean datatype to an integer representation Func BooleanToInteger($bValue) Return $bValue ? 1 : 0 EndFunc ;==>BooleanToInteger ; Append the largest byte suffix to a value Func ByteSuffix($iBytes, $iRound = 2) ; By Spiff59 Local Const $aArray = [' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB'] Local $iIndex = 0 While $iBytes > 1023 And $iIndex < UBound($aArray) $iIndex += 1 $iBytes /= 1024 WEnd Return Round($iBytes, Int($iRound)) & $aArray[$iIndex] EndFunc ;==>ByteSuffix ; Generate random text Func RandomText($iLength) Local $iRandom = 0, _ $sData = '' For $i = 1 To $iLength $iRandom = Random(55, 116, 1) $sData &= Chr($iRandom + 6 * BooleanToInteger($iRandom > 90) - 7 * BooleanToInteger($iRandom < 65)) Next Return $sData EndFunc ;==>RandomText Example 2: (without LockFile) #include <MsgBoxConstants.au3> #include <StringConstants.au3> #include <WinAPIFiles.au3> ; Traditional approach to reading and writing to a file. Due to the nature of AutoIt, the file isn't locked, thus allowing other processes to interact with the file Example_2() Func Example_2() ; Path of the locked file Local $sFilePath = _WinAPI_GetTempFileName(@TempDir) ; Lock the file to this process and create it if not already done so This is writing mode Local $hLock = FileOpen($sFilePath, $FO_OVERWRITE) ; Erase the contents of the locked file FileWrite($hLock, '') ; Write random data to the locked file For $i = 1 To 5 FileWrite($hLock, RandomText(10) & @CRLF) Next ; Close the file handle and create another handle to read the file contents FileClose($hLock) $hLock = FileOpen($sFilePath, $FO_READ) ; Read the locked file Local $sRead = FileRead($hLock) ; Display the contents of the locked file that was just read MsgBox($MB_SYSTEMMODAL, '', $sRead) ; Delete the locked file. As this is not locked by AutoIt the file will be deleted MsgBox($MB_SYSTEMMODAL, '', 'Delete the locked file: ' & _ @CRLF & _ @CRLF & _ FileDelete($sFilePath) & ' (this will return 1, as the file is''t locked by AutoIt due to safety measures in place).') ; Unlock the locked file (though not really locked) FileClose($hLock) EndFunc ;==>Example_2 ; Convert a boolean datatype to an integer representation Func BooleanToInteger($bValue) Return $bValue ? 1 : 0 EndFunc ;==>BooleanToInteger ; Generate random text Func RandomText($iLength) Local $iRandom = 0, _ $sData = '' For $i = 1 To $iLength $iRandom = Random(55, 116, 1) $sData &= Chr($iRandom + 6 * BooleanToInteger($iRandom > 90) - 7 * BooleanToInteger($iRandom < 65)) Next Return $sData EndFunc ;==>RandomText All of the above has been included in a ZIP file. Previous download: 866+ LockFile.zip
    2 points
  2. Ward

    LZMA Compression UDF

    How to compress data in memory? The simplest way is to use the Native API Compression by trancexx. Here provides another way, using LZMA algorithm. LZMA is the default method of 7z format, provides a high compression ratio and very fast decompression. In this UDF, embedded and external DLL are both supported. If "LZMA.DLL.AU3" (embedded binary) is not included in scripts, a external LZMA.DLL must exists. You can use external DLL if MemoryDll is not work for you. Some tips: Both LzmaEnc and LzmaDec only accept binary arguments. To compress string, StringToBinary and BinaryToString can help.LzmaEnc can accept an extra argument, the comression level, from 1 to 9. For example: LzmaEnc($Binary, 9)More settings are support by LzmaEncSetting, please see LZMA SDK.This UDF is not for file compression. To compress files, 7-ZIP UDF is the better choiceExample: #include "LZMA.AU3" #include "LZMA.DLL.AU3" Local $Source = Binary("LZMALZMALZMALZMALZMALZMALZMALZMALZMALZMA") Local $Compressed = LzmaEnc($Source) Local $Decompressed = LzmaDec($Compressed) LZMA.zip
    1 point
  3. Also, if you've installed the full install of Scite4AutoIt3 there's a macro recorder in the Tools menu, if you have a file with the .au3 extension open.
    1 point
  4. welcome yes, absolutely AutoIt can do that. Au3Record.exe is included with AutoIt in the 'Extras' directory. if that's not to your liking, you can always write your own
    1 point
  5. Better? Or not? #include <EditConstants.au3> #include <GUIConstantsEx.au3> Example() Func Example() ; Create a GUI with an edit control. Local $hGUI = GUICreate("Example") Local $iEdit = GUICtrlCreateEdit("Line 0" & @CRLF, 0, 0, 400, 350) Local $iOK = GUICtrlCreateButton("OK", 310, 370, 85, 25) ; Set data of the edit control. For $i = 1 To 25 GUICtrlSetData($iEdit, "Line " & $i & @CRLF, 1) Next ; Set focus to the edit control. GUICtrlSetState($iEdit, $GUI_FOCUS) ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) ; Initialize the variable $aCtrlRecvMsg for storing the value returned by GUICtrlRecvMsg. Local $aCtrlRecvMsg = 0 While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $iOK ; Send the message EM_GETSEL, to retrieve the current selection of the edit control. $aCtrlRecvMsg = GUICtrlRecvMsg($iEdit, $EM_GETSEL) ; Set focus to the edit control. GUICtrlSetState($iEdit, $GUI_FOCUS) ; If GUICtrlRecvMsg returned the value of 0, then an error occurred otherwise display the contents of the array. If $aCtrlRecvMsg = 0 Then MsgBox(4096, "", "An error occurred. The value returned was - " & $aCtrlRecvMsg) Else MsgBox(4096, "", "Start: " & $aCtrlRecvMsg[0] & " End: " & $aCtrlRecvMsg[1]) EndIf EndSwitch WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>Example
    1 point
  6. Melba23

    Drag a control.

    PlayHD, As AutoIt cannot read your mind, you will have to tell it when you want to move the button and when you want to click it - like this: #include <GuiConstantsEx.au3> #include <WindowsConstants.au3> #include <MenuConstants.au3> $hGUI = GUICreate("Test", 500, 500) $cCheck = GUICtrlCreateCheckbox(" Move", 10, 10, 200, 20) GUICtrlCreateLabel("Move me", 100, 50, 60, 20) GUICtrlSetBkColor(-1, 0x00FF00) $cButton = GUICtrlCreateButton("Me too", 40, 100, 80, 23) GUISetState() While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE Exit Case $GUI_EVENT_PRIMARYDOWN ; Are we moving? If GUICtrlRead($cCheck) = 1 Then $cInfo = GUIGetCursorInfo($hGUI) $iControl = $cInfo[4] If $iControl Then $aPos = ControlGetPos($hGUI, "", $iControl) $iSubtractX = $cInfo[0] - $aPos[0] $iSubtractY = $cInfo[1] - $aPos[1] Do $cInfo = GUIGetCursorInfo($hGUI) ControlMove($hGUI, "", $iControl, $cInfo[0] - $iSubtractX, $cInfo[1] - $iSubtractY) Until Not $cInfo[2] EndIf ; Empty event queue to prevent the button firing Do Until GUIGetMsg() = 0 EndIf Case $cButton MsgBox(0, "Hi", "Button pressed") EndSwitch WEnd Any use? M23
    1 point
  7. guinness

    Drag a control.

    Something like this >> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Example() Func Example() Local $hGUI = GUICreate('_ControlMove()', 300, 200) Local $iLabel = GUICtrlCreateLabel('Move It', 100, 50, 60, 20) GUICtrlSetBkColor(-1, 0x00FF00) GUISetState(@SW_SHOW, $hGUI) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $GUI_EVENT_PRIMARYDOWN _ControlMove($iLabel) EndSwitch WEnd EndFunc ;==>Example Func _ControlMove($iControlID) ; By Melba23 Local Const $SC_MOVE = 0xF010 Local $aReturn = GUIGetCursorInfo() If @error Then Return 0 EndIf If $aReturn[4] = $iControlID Then GUICtrlSendMsg($iControlID, $WM_SYSCOMMAND, BitOR($SC_MOVE, $HTCAPTION), 0) EndIf EndFunc ;==>_ControlMove
    1 point
  8. bogQ

    Need help - ControlSend

    if win do allow you to send data when he dont have focus that its possible, some wins will not work others will alow you to do that so use WinGetHandle instead WinActivate and use ID of the controll your sending the command, in your case thats Edit1 so dont remove it unless you need to send data to some other controll
    1 point
  9. If I have a fixed array I've recently started to use this approach with using Enum, thus eliminating the need for using 'magic numbers.' Local Enum $sLabelName, $hGUI, $iLabelID, $iGUIMax ; Use Enum to create variables from 0 to total number of variables. Local $aGUI[$iGUIMax] ; $iGUIMax is equal to the value of 3. $aGUI[$sLabelName] = 'This is a label.' $aGUI[$hGUI] = GUICreate(... $aGUI[$iLabelID] = GUICtrlCreateLabel(...
    1 point
  10. bogQ

    Need help - ControlSend

    Guess i gotta work some more on my english, sometimes i wanna write something and it turn out like something else that even i don't understand when i re read it tomorrow $handle = "0x00020616" is text so if your win title isnt 0x00020616 you do need to use something that will return handle like WinGetHandle('[CLASS:Notepad]') or some other command from help file so this shud work $handle = WinActivate('[Class:Notepad]') If $handle Then for $i =1 to 100 ControlSend($handle, "", "", "dummy text") Next EndIf When you store some handle to some file i do think that you will need to get all win handless compare thenm to handle that you need and use real handle instead text on that win. from help file It is important to note that a window handle is [b]not[/b] classed as a number or string - it is its own special type. Edit: will reddit upper post cos it sound plain stupid
    1 point
×
×
  • Create New...