Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 03/28/2012 in all areas

  1. ohh...sry, didnt know, my bad, i will read the forum rules, thnaks, and i ask an moderator de delete my post and accept my apologise. Thanks again guinness.
    2 points
  2. 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
    1 point
  3. This is from the Help File on Controls (and could be a possible reason you are having an issue with extracting text from the control): Note: AutoIt only works with standard Microsoft controls - some applications write their own custom controls which may look like a standard MS control but may resist automation. Experiment!
    1 point
  4. I ran this script and used Task Manager as a replacement for your program, which obviously I don't have, and it ran just fine for me. Here's the output from Task Manager when I hit the button.
    1 point
  5. AndiDomiK, This thread shows how to fill an array in a loop without declaring its size beforehand. This thread shows how to edit the different items within a listview - you will need to get rid of quite a lot of stuff you do not need, but the basic code you do require is in there. M23
    1 point
  6. WinGetState() allows you to find out if the window in question is maximized or minimized.
    1 point
×
×
  • Create New...