-
Posts
50 -
Joined
-
Last visited
Profile Information
-
Interests
Computer Science and General Programming
Recent Profile Visitors
410 profile views
Solarlight27's Achievements
Wayfarer (2/7)
0
Reputation
-
Hey guys, I'm working on a code which creates a drop down menu as the user is typing, the drop down updates and calls the lines included below. $array = _GUICtrlRichEdit_GetSel($hRichEdit) $iStart = _GUICtrlRichEdit_GetCharPosOfPreviousWord($hRichEdit, $array[0]) $iEnd = _GUICtrlRichEdit_GetCharPosOfNextWord($hRichEdit, $array[0]) $sText = _GUICtrlRichEdit_GetTextInRange($hRichEdit, $iStart, $iEnd) What I want to do is instead of using the char positions of Spaces, I'd like to use Commas instead. For example if I had a shopping list of (1% milk, cheese, red grapes) I want to be able to return each list item (by using the comma locations), instead of returning each individual word on that list (i.e. "red grapes" instead of "red" and "grapes"). Thank You!
-
Sweet, found it, I still have to figure out his program and everything...
-
Yes I do know that an erring program isn't working fine, I'm just hoping that a Band-aid solution can fix my problem here... My script had been working fine for a long while, but then lately with my new flash drive it has been throwing errors, it still does what its programmed to do just it think at some there should still be a flash drive plugged in after its ejected. And the error, is a Windows error window that says "No Disk"
-
Is there a way to catch errors in AutoIt? SImilar to java's "Try-Catch" methods? My program keeps on throwing errors and I need them to just go away, my program works fine, other than these errors. Here's my code: #NoTrayIcon #include #include #include #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker #Obfuscator_Parameters=/cs /cf #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ;Opt("TrayMenuMode",1) ;$menu = TrayCreateItem("DriveLocker") ;$menu2 = TrayCreateItem("") ;$menuLock = TrayCreateItem("Lock Drive") ;TraySetState() HotKeySet("{F9}", "_LockDrive") HotKeySet("{PAUSE}", "_Switch") $running = True While 1 While ($running) $letter = DriveGetDrive("REMOVABLE") If IsArray($letter) Then For $i = 1 to $letter[0] _Check($letter[$i]) Next EndIf $time = TimerInit() While TimerDiff($time) < 10000 If IsArray($letter) Then For $i = 1 to $letter[0] _Check($letter[$i]) Next EndIf Sleep(100) WEnd WEnd Sleep(10*1000) WEnd Func _Switch() If ($running) Then $running = False Else $running = True EndIf EndFunc Func _Check($strDriveLetter) If ($running) Then If FileExists($strDriveLetter & "\AutoIt3\Locker\IAmLocked") Then _Unlock($strDriveLetter) ElseIf FileExists($strDriveLetter & "\lock.txt") Then _Unlock($strDriveLetter) EndIf EndIf EndFunc Func _Unlock($strDriveLetter) If ($running) Then $strPassword = InputBox("Security Check", "Enter your password.", "", "*", "", "", @DesktopWidth/2, @DesktopHeight/2,8, "passwordBox") ToolTip("Enter Password NOW.") _MouseTrap(@DesktopWidth, @DesktopHeight-100) If ($strPassword = "PASSWORD") Then _MouseTrap() ToolTip("") FileDelete($strDriveLetter & "\AutoIt3\Locker\IAmLocked") FileDelete($strDriveLetter & "\lock.txt") _ReAnimate($strDriveLetter) ElseIf ($strPassword = "PASSWORDFAILSAFE") Then _MouseTrap() ToolTip("") FileDelete($strDriveLetter & "\AutoIt3\Locker\IAmLocked") FileDelete($strDriveLetter & "\lock.txt") _ReAnimate($strDriveLetter) Else EjectVolume($strDriveLetter) ToolTip("") _MouseTrap() EndIf EndIf EndFunc Func _ReAnimate($strDriveLetter) ;Run(StringUpper($letter[$i]) & "\") ;Run(StringUpper($letter[$i]) & "\new.txt.lnk") ;Run(StringUpper($letter[$i]) & "\you.lnk") ;WinClose("AutoPlay") ;ProcessClose("mcagent.exe") EndFunc Func _LockDrive() $letter = DriveGetDrive("REMOVABLE") If IsArray($letter) Then For $i = 1 To $letter[0] If MsgBox(36,"Lock This Drive?","Do you wish to lock the " & StringUpper($letter[$i]) & " drive?") = 6 Then FileWrite($letter[$i] & "\AutoIt3\Locker\IAmLocked","Locked") _FileCreate($letter[$i] & "\lock.txt") ;EjectVolume($letter[$i]) EndIf Next EndIf EndFunc
-
Alright I'm trying to figure out a way to pause my script from executing which uses a timer using the (_Timer_Init() and _Timer_Diff()) methods. Now I know that when I click on the autoit icon for my scipt in the tray that it pauses my script but it also has an opition to exit my script which I would like to disable while still having the pausing option available to the user. Any suggestions?
-
Solarlight27 reacted to a post in a topic:
Google Guitar Bot
-
Solarlight27 reacted to a post in a topic:
Asking for source
-
Ever wanted to use Autoit to lock up your flash drive at home? Well here's the code for it. Code from user LvlUp and some minor modification from me. All props to LvlUp and his coding abilities and assistance with this code. Here's the main code: #NoTrayIcon #include <Misc.au3> #include <eject.au3> Opt("TrayMenuMode",1) $menu = TrayCreateItem("DriveLocker") $menu2 = TrayCreateItem("") $menuLock = TrayCreateItem("Lock Drive") $menuExit = TrayCreateItem("Exit") TraySetState() While 1 $letter = DriveGetDrive("REMOVABLE") If IsArray($letter) Then For $i = 1 to $letter[0] _Check($letter[$i]) Next EndIf $time = TimerInit() While TimerDiff($time) < 10000 $msg = TrayGetMsg() TrayItemSetState($msg,4) Select Case $msg = $menuLock _LockDrive() Case $msg = $menuExit Exit EndSelect Sleep(100) WEnd WEnd Func _Check($strDriveLetter) If FileExists($strDriveLetter & "\IAmLocked") Then _Unlock($strDriveLetter) EndFunc Func _Unlock($strDriveLetter) $strPassword = InputBox("Security Check", "Enter your password.", "", "*", "", "", @DesktopWidth/2, @DesktopHeight/2,15, "passwordBox") ToolTip("Enter Password NOW.") _MouseTrap(@DesktopWidth, @DesktopHeight-100) If ($strPassword = "password") Then _MouseTrap() FileDelete($strDriveLetter & "\IAmLocked") _ReAnimate($strDriveLetter) Else EjectVolume($strDriveLetter) _MouseTrap() EndIf EndFunc Func _ReAnimate($strDriveLetter) ;Closes the autoplay window if open WinClose("AutoPlay") EndFunc Func _LockDrive() $letter = DriveGetDrive("REMOVABLE") If IsArray($letter) Then For $i = 1 To $letter[0] If MsgBox(36,"Lock This Drive?","Do you wish to lock the " & StringUpper($letter[$i]) & " drive?") = 6 Then FileWrite($letter[$i] & "\IAmLocked","Locked") EjectVolume($letter[$i]) EndIf Next EndIf EndFunc And the includes file for eject.au3 #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <WinApi.au3> ;Prototypes ;BOOL EjectVolume(TCHAR cDriveLetter); ;HANDLE OpenVolume(TCHAR cDriveLetter); ;BOOL LockVolume(HANDLE hVolume); ;BOOL DismountVolume(HANDLE hVolume); ;BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPrevent); ;BOOL AutoEjectVolume(HANDLE hVolume); ;BOOL CloseVolume(HANDLE hVolume); ;StringFormat Output $szVolumeFormat = "\\\\.\\%s" $szRootFormat = "%s\\" $szErrorFormat = "Error %d: %s\n" ;------------------------------------------ ;Arbitrary variables ;;Global Const $INVALID_HANDLE_VALUE = 0 ;------------------------------------------ ;DeviceIoControl Contants Global Const $FSCTL_LOCK_VOLUME = int(0x090018) Global Const $FSCTL_DISMOUNT_VOLUME = int(0x00090020) Global Const $IOCTL_STORAGE_EJECT_MEDIA = int(0x002D4808) Global Const $IOCTL_STORAGE_MEDIA_REMOVAL = int(0x002D4804) ;------------------------------------------ ;Retry Constants Global Const $LOCK_TIMEOUT = 10000 ; 10 Seconds Global Const $LOCK_RETRIES = 20 ;$OpenVolume = InputBox("Ejecting...", "Enter the drive to eject", "G:") ;ConsoleWrite("Trying to Eject the drive " & EjectVolume($OpenVolume) & @crlf) Func ReportError($szMsg) ConsoleWrite(StringFormat($szErrorFormat, _WinAPI_GetLastErrorMessage (), $szMsg) & @CRLF) Exit EndFunc ;==>ReportError Func OpenVolume($cDriveLetter) ;HANDLE hVolume ;UINT uDriveType ;TCHAR szVolumeName[8] ;TCHAR szRootName[5] ;DWORD dwAccessFlags $szRootName = StringFormat($szRootFormat, $cDriveLetter) $uDriveType = DriveGetType($szRootName); ConsoleWrite($szRootName & @tab & $uDriveType & @crlf) Switch $uDriveType Case "Removable" $dwAccessFlags = 6 Case "CDROM" $dwAccessFlags = 2 Case Else ConsoleWrite("Cannot eject. Drive type is incorrect." & @CRLF) Return $INVALID_HANDLE_VALUE EndSwitch $szVolumeName = StringFormat($szVolumeFormat, $cDriveLetter) ;$szVolumeName = $szVolumeFormat & $cDriveLetter ConsoleWrite($szVolumeName & @crlf ) $hVolume = _WinAPI_CreateFile ($szVolumeName, 2,$dwAccessFlags, 6) #cs hVolume = CreateFile( szVolumeName, dwAccessFlags, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); #ce If ($hVolume == $INVALID_HANDLE_VALUE) Then ReportError("CreateFile"); Return $hVolume; EndFunc ;==>OpenVolume Func CloseVolume($hVolume) Return _WinAPI_CloseHandle ($hVolume); EndFunc ;==>CloseVolume Func LockVolume($hVolume) Local $dwBytesReturned Local $dwSleepAmount Local $nTryCount local $iRead $dwSleepAmount = $LOCK_TIMEOUT / $LOCK_RETRIES; ; Do this in a loop until a timeout period has expired For $nTryCount = 0 To $nTryCount < $LOCK_RETRIES If _Device_Control($hVolume, $FSCTL_LOCK_VOLUME, $iRead) Then Return True Else Sleep($dwSleepAmount); EndIf Next Return False; EndFunc ;==>LockVolume Func DismountVolume($hVolume) ConsoleWrite("Dismount " & $hVolume & @crlf) Local $dwBytesReturned, $iRead local $aResult = _Device_Control($hVolume, $FSCTL_DISMOUNT_VOLUME, $iRead) ;msgbox(0,"",$aResult) Return $aResult ;Return $dwBytesReturned EndFunc ;==>DismountVolume Func PreventRemovalOfVolume($hVolume, $fPreventRemoval) Local $dwBytesReturned Local $aResult Local $lpInbuffer,$nInBufferSize,$lpOutBuffer,$nOutBufferSize,$lpOverlapped $PMRBUFFER = DllStructCreate("bool PreventMediaRemoval") DllStructSetData($PMRBUFFER,"PreventMediaRemoval",$fPreventRemoval) $lpBytesReturned = DllStructCreate("int Read") $pRead = DllStructGetPtr($lpBytesReturned, "Read") $aResult = Dllcall("kernel32.dll","int","DeviceIoControl","hwnd",$hVolume,"uint",$IOCTL_STORAGE_MEDIA_REMOVAL,"ptr",DllStructGetPtr($PMRBUFFER),"uint",DllStructGetSize($PMRBUFFER), _ "ptr",$lpOutBuffer,"uint",$nOutBufferSize,"ptr",$pRead,"ptr",$lpOverlapped) if $aResult = 0 then msgbox(0,"",_WinAPI_GetLastErrorMessage()) Return $aResult <> 0 ;& PMRBuffer, sizeof (PREVENT_MEDIA_REMOVAL), ;NULL, 0, ; & dwBytesReturned, ;NULL); EndFunc ;==>PreventRemovalOfVolume Func AutoEjectVolume($hVolume) Local $aResult, $iRead; $aResult = _Device_Control($hVolume, $IOCTL_STORAGE_EJECT_MEDIA, $iRead) Return $aResult EndFunc ;==>AutoEjectVolume Func EjectVolume($cDriveLetter) Local $hVolume; Local $fRemoveSafely = False; Local $fAutoEject = False; ; Open the volume. $hVolume = OpenVolume($cDriveLetter); If $hVolume == $INVALID_HANDLE_VALUE Then Return False ; Lock and dismount the volume. If LockVolume($hVolume) And DismountVolume($hVolume) Then $fRemoveSafely = True; ConsoleWrite("Volume Locked and Dismounted, trying to Eject " & @crlf) ; Set prevent removal to false and Eject the volume. If PreventRemovalOfVolume($hVolume, False) And AutoEjectVolume($hVolume) Then $fAutoEject = True; EndIf Else ConsoleWrite("Volume can't be locked or dismounted, please close possible opened files" & @crlf) EndIf ; Close the volume so other processes can use the drive. If CloseVolume($hVolume) = False Then Return False; EndIf If $fAutoEject Then ConsoleWrite(StringFormat("Media in drive %s has been ejected safely.\n", $cDriveLetter)) Else If $fRemoveSafely Then ConsoleWrite(StringFormat("Media in drive %s can be safely removed.\n", $cDriveLetter)) EndIf EndIf Return True; EndFunc ;==>EjectVolume Func _Device_Control($hDevice, $dwIoControlAutoit, ByRef $iRead) Local $aResult Local $lpInbuffer,$nInBufferSize,$lpOutBuffer,$nOutBufferSize,$lpOverlapped $tRead = DllStructCreate("int Data") $aResult = Dllcall("kernel32.dll","int","DeviceIoControl","hwnd",$hDevice,"uint",$dwIoControlAutoit,"ptr",$lpInBuffer,"uint",0, _ "ptr",$lpOutBuffer,"uint",0,"ptr",DllStructGetPtr($tRead),"ptr",$lpOverlapped) $iRead = DllStructGetData($tRead, "Data") ConsoleWrite("Device Control " & $iRead & @CRLF) Return $aResult<>0 EndFunc ;==>_Device_Control Enjoy.
- 1 reply
-
- Flash Drive
- Lock
-
(and 2 more)
Tagged with:
-
Password Protected Flash Drive
Solarlight27 replied to Solarlight27's topic in AutoIt General Help and Support
I've actually looked into TrueCrypt and it needs admin privileges which I lack on the computer i often use. Ohhh inputbox has a time out, awesome! Thanks for all of your help, I've been wanting to finish this project for over a month now, not really knowing where to go.... So Thanks Again!! -
Solarlight27 reacted to a post in a topic:
Password Protected Flash Drive
-
Password Protected Flash Drive
Solarlight27 replied to Solarlight27's topic in AutoIt General Help and Support
LvlUp, you sir are a saint, I'll check this out tonight and let you know how it turns out! EDIT-- This works great! There's only one glaring loop hole that I can spot which would be someone could just minimize or move the password prompt input box, so maybe a timer to put in an input could be inserted? or maybe a winactive function could be used? or maybe disabling mouse movement so windows couldn't be switched, I think I can figure out how to disable the keys for Alt-tab and the like.. -
Solarlight27 reacted to a post in a topic:
Password Protected Flash Drive
-
Solarlight27 reacted to a post in a topic:
Remote Monitoring - to print the screen every second
-
Solarlight27 reacted to a post in a topic:
Remote Monitoring - to print the screen every second
-
Solarlight27 reacted to a post in a topic:
Remote Monitoring - to print the screen every second
-
Password Protected Flash Drive
Solarlight27 replied to Solarlight27's topic in AutoIt General Help and Support
Does it? Because if I run the checker program and then plug in my flash drive the program still works fine? It's just when I then eject the drive and then re-put in the drive that it doesn't start the code back up again which is because it exited like you said I'm assuming, so how do I "restart" the code from the beginning? I tried recursion as seen above, running another program so that they would run the other just before exiting (the programs would of course be mirrors of each other) but that didn't seem to work either? -
Password Protected Flash Drive
Solarlight27 replied to Solarlight27's topic in AutoIt General Help and Support
Yes, yes I know the lot of the people here are volunteers, just as many forums are, I just thought it would be nice to get some help. I know that there is someone out there that knows how to help me. So sorry for spamming the forums trying to get some help, but help would be immensely appreciated. -
Password Protected Flash Drive
Solarlight27 replied to Solarlight27's topic in AutoIt General Help and Support
Alright fine, but how am I supposed to get help with my program, I've created this project and I'm really quite proud of it and now I'm just a hair short of finishing it and I have no idea how, I've been waiting for somewhere around 3 weeks for some help and I haven't gotten anything, so how else am I supposed to draw some attention to my cause, if I can just get the help I need to make this code work that would be absolutely amazing, I understand it may be a little labor intensive to help me but a solution would be very much appreciated. -
FYI Here's the code to my collection of programs which encrypt my flash drive IF the checker program in running when the USB is plugged in: My problem here is that the project runs fine the first time around, however when i replug in my flash drive the program doesn't work, it doesn't ask for a password again like it should in theory. I've tried loops, for statements, recursion (as seen below) and the project either creates infinite windows which comes just short of killing my CP with lag or it just runs the one time and that's it. Below is the code to the lot of my programs that are run with one another: (If you want to use it feel free) Thank You All! Checker Checks to see if my drive is plugged in via seeing if a file exits on a removable drive #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker #Obfuscator_Parameters=/cs #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** Local $letter = DriveGetDrive("REMOVABLE") Func _check() For $i = 1 To $letter[0] If FileExists(StringUpper($letter[$i]) & "\AutoIt3\Programs\Re-AnimationSequence(LOCKED).exe") Then Run (StringUpper($letter[$i]) & "\AutoIt3\Programs\Re-AnimationSequence(LOCKED).exe") ;Run ("C:\Users\kids\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\Checker.exe") Else Sleep(180000) _check() EndIf Next EndFuncFor $i = 1 To $letter[0] Local $file = FileExists(StringUpper($letter[$i]) & "\AutoIt3\Programs\Re-AnimationSequence(LOCKED).exe") If $file <> 0 Then _check() EndIf Next Re-AnimationSequence(LOCKED) It's what "encrypts" my drive Local $Password = InputBox("Security Check", "Enter your password.", "", "*") Local $letter = DriveGetDrive("REMOVABLE") For $i = 1 To $letter[0] If ($Password = "password") Then BlockInput(1) Run(StringUpper($letter[$i]) & "\Re-Animation Sequence.exe") Sleep(2000) BlockInput(0) Exit Else Run(StringUpper($letter[$i]) & "\AutoIt3\Programs\runeject.exe") Sleep(2000) Exit EndIf Next While 1 MouseGetPos() Sleep(5) WEnd runeject It runs the ejection program and inserts the correct text and commands to make it run Local $letter = DriveGetDrive("REMOVABLE") For $i = 1 To $letter[0] Run(StringUpper($letter[$i]) & "\AutoIt3\Programs\eject.exe") If WinActive("Ejecting...") == 0 Then Do If WinActive("Ejecting...") == 0 Then Run("F:\AutoIt3\Programs\eject.exe") ElseIf WinActive("Ejecting...") == 0 Then Run("G:\AutoIt3\Programs\eject.exe") ElseIf WinActive("Ejecting...") == 0 Then Run("H:\AutoIt3\Programs\eject.exe") ElseIf WinActive("Ejecting...") == 0 Then Run("I:\AutoIt3\Programs\eject.exe") ElseIf WinActive("Ejecting...") == 0 Then Run("J:\AutoIt3\Programs\eject.exe") Else MsgBox(4096, "DriveGetDrive", "It appears an error occurred.") EndIf Until (WinActive("Ejecting...") <> 0) EndIf BlockInput(1) Send ("{BACKSPACE}{BACKSPACE}{BACKSPACE}" & StringUpper($letter[$i]) & "{ENTER}") Sleep (1000) Send ("{ENTER}") BlockInput(0) Next Re-Animation Sequence It runs my 2 bats that run my frequently used files Local $letter = DriveGetDrive("REMOVABLE") For $i = 1 To $letter[0] If @error Then Exit ElseIf FileExists(StringUpper($letter[$i]) & "\Re-AnimationSequence2.bat") Then Run (StringUpper($letter[$i]) & "Re-AnimationSequence2.bat") ElseIf FileExists(StringUpper($letter[$i]) & "\Re-AnimationSequence.bat") Then Run (StringUpper($letter[$i]) & "Re-AnimationSequence.bat") EndIf If FileExists(StringUpper($letter[$i]) & "\AutoIt3\Programs\hotkeyKEY.exe") Then Run (StringUpper($letter[$i]) & "\AutoIt3\Programs\hotkeyKEY.exe") EndIf Next eject (trust me it works and isn't this issue..) Got if from Autoit forums #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <WinApi.au3> ;Prototypes ;BOOL EjectVolume(TCHAR cDriveLetter); ;HANDLE OpenVolume(TCHAR cDriveLetter); ;BOOL LockVolume(HANDLE hVolume); ;BOOL DismountVolume(HANDLE hVolume); ;BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPrevent); ;BOOL AutoEjectVolume(HANDLE hVolume); ;BOOL CloseVolume(HANDLE hVolume); ;StringFormat Output $szVolumeFormat = "\\\\.\\%s" $szRootFormat = "%s\\" $szErrorFormat = "Error %d: %s\n" ;------------------------------------------ ;Arbitrary variables ;;Global Const $INVALID_HANDLE_VALUE = 0 ;------------------------------------------ ;DeviceIoControl Contants Global Const $FSCTL_LOCK_VOLUME = int(0x090018) Global Const $FSCTL_DISMOUNT_VOLUME = int(0x00090020) Global Const $IOCTL_STORAGE_EJECT_MEDIA = int(0x002D4808) Global Const $IOCTL_STORAGE_MEDIA_REMOVAL = int(0x002D4804) ;------------------------------------------ ;Retry Constants Global Const $LOCK_TIMEOUT = 10000 ; 10 Seconds Global Const $LOCK_RETRIES = 20 $OpenVolume = InputBox("Ejecting...", "Enter the drive to eject", "G:") ConsoleWrite("Trying to Eject the drive " & EjectVolume($OpenVolume) & @crlf) Func ReportError($szMsg) ConsoleWrite(StringFormat($szErrorFormat, _WinAPI_GetLastErrorMessage (), $szMsg) & @CRLF) Exit EndFunc ;==>ReportError Func OpenVolume($cDriveLetter) ;HANDLE hVolume ;UINT uDriveType ;TCHAR szVolumeName[8] ;TCHAR szRootName[5] ;DWORD dwAccessFlags $szRootName = StringFormat($szRootFormat, $cDriveLetter) $uDriveType = DriveGetType($szRootName); ConsoleWrite($szRootName & @tab & $uDriveType & @crlf) Switch $uDriveType Case "Removable" $dwAccessFlags = 6 Case "CDROM" $dwAccessFlags = 2 Case Else ConsoleWrite("Cannot eject. Drive type is incorrect." & @CRLF) Return $INVALID_HANDLE_VALUE EndSwitch $szVolumeName = StringFormat($szVolumeFormat, $cDriveLetter) ;$szVolumeName = $szVolumeFormat & $cDriveLetter ConsoleWrite($szVolumeName & @crlf ) $hVolume = _WinAPI_CreateFile ($szVolumeName, 2,$dwAccessFlags, 6) #cs hVolume = CreateFile( szVolumeName, dwAccessFlags, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); #ce If ($hVolume == $INVALID_HANDLE_VALUE) Then ReportError("CreateFile"); Return $hVolume; EndFunc ;==>OpenVolume Func CloseVolume($hVolume) Return _WinAPI_CloseHandle ($hVolume); EndFunc ;==>CloseVolume Func LockVolume($hVolume) Local $dwBytesReturned Local $dwSleepAmount Local $nTryCount local $iRead $dwSleepAmount = $LOCK_TIMEOUT / $LOCK_RETRIES; ; Do this in a loop until a timeout period has expired For $nTryCount = 0 To $nTryCount < $LOCK_RETRIES If _Device_Control($hVolume, $FSCTL_LOCK_VOLUME, $iRead) Then Return True Else Sleep($dwSleepAmount); EndIf Next Return False; EndFunc ;==>LockVolume Func DismountVolume($hVolume) ConsoleWrite("Dismount " & $hVolume & @crlf) Local $dwBytesReturned, $iRead local $aResult = _Device_Control($hVolume, $FSCTL_DISMOUNT_VOLUME, $iRead) msgbox(0,"",$aResult) Return $aResult ;Return $dwBytesReturned EndFunc ;==>DismountVolume Func PreventRemovalOfVolume($hVolume, $fPreventRemoval) Local $dwBytesReturned Local $aResult Local $lpInbuffer,$nInBufferSize,$lpOutBuffer,$nOutBufferSize,$lpOverlapped $PMRBUFFER = DllStructCreate("bool PreventMediaRemoval") DllStructSetData($PMRBUFFER,"PreventMediaRemoval",$fPreventRemoval) $lpBytesReturned = DllStructCreate("int Read") $pRead = DllStructGetPtr($lpBytesReturned, "Read") $aResult = Dllcall("kernel32.dll","int","DeviceIoControl","hwnd",$hVolume,"uint",$IOCTL_STORAGE_MEDIA_REMOVAL,"ptr",DllStructGetPtr($PMRBUFFER),"uint",DllStructGetSize($PMRBUFFER), _ "ptr",$lpOutBuffer,"uint",$nOutBufferSize,"ptr",$pRead,"ptr",$lpOverlapped) if $aResult = 0 then msgbox(0,"",_WinAPI_GetLastErrorMessage()) Return $aResult <> 0 ;& PMRBuffer, sizeof (PREVENT_MEDIA_REMOVAL), ;NULL, 0, ; & dwBytesReturned, ;NULL); EndFunc ;==>PreventRemovalOfVolume Func AutoEjectVolume($hVolume) Local $aResult, $iRead; $aResult = _Device_Control($hVolume, $IOCTL_STORAGE_EJECT_MEDIA, $iRead) Return $aResult EndFunc ;==>AutoEjectVolume Func EjectVolume($cDriveLetter) Local $hVolume; Local $fRemoveSafely = False; Local $fAutoEject = False; ; Open the volume. $hVolume = OpenVolume($cDriveLetter); If $hVolume == $INVALID_HANDLE_VALUE Then Return False ; Lock and dismount the volume. If LockVolume($hVolume) And DismountVolume($hVolume) Then $fRemoveSafely = True; ConsoleWrite("Volume Locked and Dismounted, trying to Eject " & @crlf) ; Set prevent removal to false and Eject the volume. If PreventRemovalOfVolume($hVolume, False) And AutoEjectVolume($hVolume) Then $fAutoEject = True; EndIf Else ConsoleWrite("Volume can't be locked or dismounted, please close possible opened files" & @crlf) EndIf ; Close the volume so other processes can use the drive. If CloseVolume($hVolume) = False Then Return False; EndIf If $fAutoEject Then ConsoleWrite(StringFormat("Media in drive %s has been ejected safely.\n", $cDriveLetter)) Else If $fRemoveSafely Then ConsoleWrite(StringFormat("Media in drive %s can be safely removed.\n", $cDriveLetter)) EndIf EndIf Return True; EndFunc ;==>EjectVolume Func _Device_Control($hDevice, $dwIoControlAutoit, ByRef $iRead) Local $aResult Local $lpInbuffer,$nInBufferSize,$lpOutBuffer,$nOutBufferSize,$lpOverlapped $tRead = DllStructCreate("int Data") $aResult = Dllcall("kernel32.dll","int","DeviceIoControl","hwnd",$hDevice,"uint",$dwIoControlAutoit,"ptr",$lpInBuffer,"uint",0, _ "ptr",$lpOutBuffer,"uint",0,"ptr",DllStructGetPtr($tRead),"ptr",$lpOverlapped) $iRead = DllStructGetData($tRead, "Data") ConsoleWrite("Device Control " & $iRead & @CRLF) Return $aResult<>0 EndFunc ;==>_Device_Control If anyone has any additional ideas to make this code better or anything else to improve it or know how to correctly make these scripts to always work with a USB no matter how many times its re-plugged into a computer that would be much appreciated!
-
Password Protected Auto Ejector
Solarlight27 replied to Solarlight27's topic in AutoIt Example Scripts
So any ideas Autoit Community? -
FYI Here's the code to my collection of programs which encrypt my flash drive IF the checker program in running when the USB is plugged in: My problem here is that the project runs fine the first time around, however when i replug in my flash drive the program doesn't work, it doesn't ask for a password again like it should in theory. I've tried loops, for statements, recursion (as seen below) and the project either creates infinite windows which comes just short of killing my CP with lag or it just runs the one time and that's it. Below is the code to the lot of my programs that are run with one another: (If you want to use it feel free) Thank You All! Checker Checks to see if my drive is plugged in via seeing if a file exits on a removable drive #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker #Obfuscator_Parameters=/cs #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** Local $letter = DriveGetDrive("REMOVABLE") Func _check() For $i = 1 To $letter[0] If FileExists(StringUpper($letter[$i]) & "\AutoIt3\Programs\Re-AnimationSequence(LOCKED).exe") Then Run (StringUpper($letter[$i]) & "\AutoIt3\Programs\Re-AnimationSequence(LOCKED).exe") ;Run ("C:\Users\kids\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\Checker.exe") Else Sleep(180000) _check() EndIf Next EndFuncFor $i = 1 To $letter[0] Local $file = FileExists(StringUpper($letter[$i]) & "\AutoIt3\Programs\Re-AnimationSequence(LOCKED).exe") If $file <> 0 Then _check() EndIf Next Re-AnimationSequence(LOCKED) It's what "encrypts" my drive Local $Password = InputBox("Security Check", "Enter your password.", "", "*") Local $letter = DriveGetDrive("REMOVABLE") For $i = 1 To $letter[0] If ($Password = "password") Then BlockInput(1) Run(StringUpper($letter[$i]) & "\Re-Animation Sequence.exe") Sleep(2000) BlockInput(0) Exit Else Run(StringUpper($letter[$i]) & "\AutoIt3\Programs\runeject.exe") Sleep(2000) Exit EndIf Next While 1 MouseGetPos() Sleep(5) WEnd runeject It runs the ejection program and inserts the correct text and commands to make it run Local $letter = DriveGetDrive("REMOVABLE") For $i = 1 To $letter[0] Run(StringUpper($letter[$i]) & "\AutoIt3\Programs\eject.exe") If WinActive("Ejecting...") == 0 Then Do If WinActive("Ejecting...") == 0 Then Run("F:\AutoIt3\Programs\eject.exe") ElseIf WinActive("Ejecting...") == 0 Then Run("G:\AutoIt3\Programs\eject.exe") ElseIf WinActive("Ejecting...") == 0 Then Run("H:\AutoIt3\Programs\eject.exe") ElseIf WinActive("Ejecting...") == 0 Then Run("I:\AutoIt3\Programs\eject.exe") ElseIf WinActive("Ejecting...") == 0 Then Run("J:\AutoIt3\Programs\eject.exe") Else MsgBox(4096, "DriveGetDrive", "It appears an error occurred.") EndIf Until (WinActive("Ejecting...") <> 0) EndIf BlockInput(1) Send ("{BACKSPACE}{BACKSPACE}{BACKSPACE}" & StringUpper($letter[$i]) & "{ENTER}") Sleep (1000) Send ("{ENTER}") BlockInput(0) Next Re-Animation Sequence It runs my 2 bats that run my frequently used files Local $letter = DriveGetDrive("REMOVABLE") For $i = 1 To $letter[0] If @error Then Exit ElseIf FileExists(StringUpper($letter[$i]) & "\Re-AnimationSequence2.bat") Then Run (StringUpper($letter[$i]) & "Re-AnimationSequence2.bat") ElseIf FileExists(StringUpper($letter[$i]) & "\Re-AnimationSequence.bat") Then Run (StringUpper($letter[$i]) & "Re-AnimationSequence.bat") EndIf If FileExists(StringUpper($letter[$i]) & "\AutoIt3\Programs\hotkeyKEY.exe") Then Run (StringUpper($letter[$i]) & "\AutoIt3\Programs\hotkeyKEY.exe") EndIf Next eject (trust me it works and isn't this issue..) Got if from Autoit forums #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <WinApi.au3> ;Prototypes ;BOOL EjectVolume(TCHAR cDriveLetter); ;HANDLE OpenVolume(TCHAR cDriveLetter); ;BOOL LockVolume(HANDLE hVolume); ;BOOL DismountVolume(HANDLE hVolume); ;BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPrevent); ;BOOL AutoEjectVolume(HANDLE hVolume); ;BOOL CloseVolume(HANDLE hVolume); ;StringFormat Output $szVolumeFormat = "\\\\.\\%s" $szRootFormat = "%s\\" $szErrorFormat = "Error %d: %s\n" ;------------------------------------------ ;Arbitrary variables ;;Global Const $INVALID_HANDLE_VALUE = 0 ;------------------------------------------ ;DeviceIoControl Contants Global Const $FSCTL_LOCK_VOLUME = int(0x090018) Global Const $FSCTL_DISMOUNT_VOLUME = int(0x00090020) Global Const $IOCTL_STORAGE_EJECT_MEDIA = int(0x002D4808) Global Const $IOCTL_STORAGE_MEDIA_REMOVAL = int(0x002D4804) ;------------------------------------------ ;Retry Constants Global Const $LOCK_TIMEOUT = 10000 ; 10 Seconds Global Const $LOCK_RETRIES = 20 $OpenVolume = InputBox("Ejecting...", "Enter the drive to eject", "G:") ConsoleWrite("Trying to Eject the drive " & EjectVolume($OpenVolume) & @crlf) Func ReportError($szMsg) ConsoleWrite(StringFormat($szErrorFormat, _WinAPI_GetLastErrorMessage (), $szMsg) & @CRLF) Exit EndFunc ;==>ReportError Func OpenVolume($cDriveLetter) ;HANDLE hVolume ;UINT uDriveType ;TCHAR szVolumeName[8] ;TCHAR szRootName[5] ;DWORD dwAccessFlags $szRootName = StringFormat($szRootFormat, $cDriveLetter) $uDriveType = DriveGetType($szRootName); ConsoleWrite($szRootName & @tab & $uDriveType & @crlf) Switch $uDriveType Case "Removable" $dwAccessFlags = 6 Case "CDROM" $dwAccessFlags = 2 Case Else ConsoleWrite("Cannot eject. Drive type is incorrect." & @CRLF) Return $INVALID_HANDLE_VALUE EndSwitch $szVolumeName = StringFormat($szVolumeFormat, $cDriveLetter) ;$szVolumeName = $szVolumeFormat & $cDriveLetter ConsoleWrite($szVolumeName & @crlf ) $hVolume = _WinAPI_CreateFile ($szVolumeName, 2,$dwAccessFlags, 6) #cs hVolume = CreateFile( szVolumeName, dwAccessFlags, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ); #ce If ($hVolume == $INVALID_HANDLE_VALUE) Then ReportError("CreateFile"); Return $hVolume; EndFunc ;==>OpenVolume Func CloseVolume($hVolume) Return _WinAPI_CloseHandle ($hVolume); EndFunc ;==>CloseVolume Func LockVolume($hVolume) Local $dwBytesReturned Local $dwSleepAmount Local $nTryCount local $iRead $dwSleepAmount = $LOCK_TIMEOUT / $LOCK_RETRIES; ; Do this in a loop until a timeout period has expired For $nTryCount = 0 To $nTryCount < $LOCK_RETRIES If _Device_Control($hVolume, $FSCTL_LOCK_VOLUME, $iRead) Then Return True Else Sleep($dwSleepAmount); EndIf Next Return False; EndFunc ;==>LockVolume Func DismountVolume($hVolume) ConsoleWrite("Dismount " & $hVolume & @crlf) Local $dwBytesReturned, $iRead local $aResult = _Device_Control($hVolume, $FSCTL_DISMOUNT_VOLUME, $iRead) msgbox(0,"",$aResult) Return $aResult ;Return $dwBytesReturned EndFunc ;==>DismountVolume Func PreventRemovalOfVolume($hVolume, $fPreventRemoval) Local $dwBytesReturned Local $aResult Local $lpInbuffer,$nInBufferSize,$lpOutBuffer,$nOutBufferSize,$lpOverlapped $PMRBUFFER = DllStructCreate("bool PreventMediaRemoval") DllStructSetData($PMRBUFFER,"PreventMediaRemoval",$fPreventRemoval) $lpBytesReturned = DllStructCreate("int Read") $pRead = DllStructGetPtr($lpBytesReturned, "Read") $aResult = Dllcall("kernel32.dll","int","DeviceIoControl","hwnd",$hVolume,"uint",$IOCTL_STORAGE_MEDIA_REMOVAL,"ptr",DllStructGetPtr($PMRBUFFER),"uint",DllStructGetSize($PMRBUFFER), _ "ptr",$lpOutBuffer,"uint",$nOutBufferSize,"ptr",$pRead,"ptr",$lpOverlapped) if $aResult = 0 then msgbox(0,"",_WinAPI_GetLastErrorMessage()) Return $aResult <> 0 ;& PMRBuffer, sizeof (PREVENT_MEDIA_REMOVAL), ;NULL, 0, ; & dwBytesReturned, ;NULL); EndFunc ;==>PreventRemovalOfVolume Func AutoEjectVolume($hVolume) Local $aResult, $iRead; $aResult = _Device_Control($hVolume, $IOCTL_STORAGE_EJECT_MEDIA, $iRead) Return $aResult EndFunc ;==>AutoEjectVolume Func EjectVolume($cDriveLetter) Local $hVolume; Local $fRemoveSafely = False; Local $fAutoEject = False; ; Open the volume. $hVolume = OpenVolume($cDriveLetter); If $hVolume == $INVALID_HANDLE_VALUE Then Return False ; Lock and dismount the volume. If LockVolume($hVolume) And DismountVolume($hVolume) Then $fRemoveSafely = True; ConsoleWrite("Volume Locked and Dismounted, trying to Eject " & @crlf) ; Set prevent removal to false and Eject the volume. If PreventRemovalOfVolume($hVolume, False) And AutoEjectVolume($hVolume) Then $fAutoEject = True; EndIf Else ConsoleWrite("Volume can't be locked or dismounted, please close possible opened files" & @crlf) EndIf ; Close the volume so other processes can use the drive. If CloseVolume($hVolume) = False Then Return False; EndIf If $fAutoEject Then ConsoleWrite(StringFormat("Media in drive %s has been ejected safely.\n", $cDriveLetter)) Else If $fRemoveSafely Then ConsoleWrite(StringFormat("Media in drive %s can be safely removed.\n", $cDriveLetter)) EndIf EndIf Return True; EndFunc ;==>EjectVolume Func _Device_Control($hDevice, $dwIoControlAutoit, ByRef $iRead) Local $aResult Local $lpInbuffer,$nInBufferSize,$lpOutBuffer,$nOutBufferSize,$lpOverlapped $tRead = DllStructCreate("int Data") $aResult = Dllcall("kernel32.dll","int","DeviceIoControl","hwnd",$hDevice,"uint",$dwIoControlAutoit,"ptr",$lpInBuffer,"uint",0, _ "ptr",$lpOutBuffer,"uint",0,"ptr",DllStructGetPtr($tRead),"ptr",$lpOverlapped) $iRead = DllStructGetData($tRead, "Data") ConsoleWrite("Device Control " & $iRead & @CRLF) Return $aResult<>0 EndFunc ;==>_Device_Control If anyone has any additional ideas to make this code better or anything else to improve it or know how to correctly make these scripts to always work with a USB no matter how many times its re-plugged into a computer that would be much appreciated!