Jump to content

LvlUp

Members
  • Posts

    3
  • Joined

  • Last visited

LvlUp's Achievements

Seeker

Seeker (1/7)

2

Reputation

  1. Hey there Solarlight, Glad to hear that the script was of help. You can definitely put a timeout into the InputBox. Just update that line of code to read as follows: $strPassword = InputBox("Security Check", "Enter your password.", "", "*", "","","","",#####) and simply replace the ##### with the number of seconds before it times out. On a timeout, it will cause $strPassword != "password" and thus lock the flash drive. If you are looking to store sensitive data on the drive, however, I'd recommend looking into a freeware app called TrueCrypt which is capable of creating an encrypted storage space on your flashdrive that you can use. It would require you to enter a password to open the protected portion of the flash drive, and if someone tried to examine the data without first decrypting it, all they would see is nonsensical garbage text.
  2. Hi Sambassador, I helped another forum member trying to do something similar earlier today. $process = "notepad.exe" $data = "ProgramData" While 1 If _IsAttached() Then If Not ProcessExists($process) Then Run($process) Else If ProcessExists($process) Then ProcessClose($process) EndIf Sleep(100) WEnd Func _IsAttached() $drives = DriveGetDrive("REMOVABLE") If Not IsArray($drives) Then Return False Else $attached = False For $x = 1 to $drives[0] If FileExists($drives[$x] & $data) Then $attached = True Next Return $attached EndIf EndFunc Set the variable on line 1 to be process that you need to have running, set the variable on line 2 to be a file that the program is looking for. On my system, this used between 0 and 1% CPU.
  3. Hey there Solarlight. I think I've got this fixed for you, and simplified this program WAY down. Everything has been rolled into one program with a single includes file #NoTrayIcon Opt("TrayMenuMode",1) $menu = TrayCreateItem("DriveLocker") $menu2 = TrayCreateItem("") $menuLock = TrayCreateItem("Lock Drive") $menuExit = TrayCreateItem("Exit") TraySetState() #include <eject.au3> 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) < 60000 $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.", "", "*") If ($strPassword = "password") Then FileDelete($strDriveLetter & "IAmLocked") _ReAnimate($strDriveLetter) Else EjectVolume($strDriveLetter) EndIf EndFunc Func _ReAnimate($strDriveLetter) ; Put your reanimation batch calls here 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 " & $letter[$i] & " drive?") = 6 Then FileWrite($letter[$i] & "IAmLocked","Locked") EjectVolume($letter[$i]) EndIf Next EndIf EndFunc Then, in your C:\Program Files\AutoIt3\Include\ directory, save this file as eject.au3 (I made some slight adjustments to the eject script you posted above from the previous example forum) #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: %sn" ;------------------------------------------ ;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 I'll give you a quick rundown of how this all works. 1) In lines 1 through 7 I'm creating a custom menu for your checker program that can be utilized by the system clock. You can use this to lock a drive or to exit the checker. On line 9, I'm including the eject.au3 file that I put in the second code block above. The main loop for your program is lines 11 through 30. In this look we start by pulling all of the removable drives attached to your system on line 12 and storing that to the $letter variable. On line 13, we check to ensure that we received an array back. If we didn't receive one, then we know there are no removable drives and there isn't anything more to do, so we can jump down to line 18. If we did find an array on line 13, we need to check every drive that was returned to see if it contains the locked file. That is where the For loop on lines 14-16 come into play. For each item in the $letter array, we run the _Check function. The check function simply does two things. It checks to see if your drive contains a file called "IAmLocked". If it does, calls the _Unlocked function, passing the drive letter that we already know is locked as its one parameter. If the drive doesn't have the "IAmLocked" file, then the unlock function is skipped and we continue on in the main function. Lets assume the drive is indeed locked and has that file present. The _Unlock function is then called. On line 37, we ask the user for a password to unlock the drive. We test that password on line 39. You can set your own password here to be whatever you like. If the password is correct, we delete the IAmLocked file on line 40 and we run the ReAnimate function on line 41. If we enter the password wrong, we call the EjectVolume function and pass it the drive letter. (This function is part of the include <eject.au3> file) Down in the _ReAnimate function, you can enter whatever logic you like here for your batch files to run. Just enter those commands on line 48. This takes you through the process of how to unlock your drive, and if that password attempt fails, the drive is ejected. This completes the main _Check function's needs and gets your program what you were looking for. I then took your program one step further by implementing a way to lock the removable drives again. Back up in the main function on line 18, we pull a timestamp. I prefer to use timestamps and timediffs in a loop rather than sleeping for a long period of time. This way we can still send menu commands and have the program respond to them without sleeping for minutes at a time. So, while the difference between the first timestamp and now is less than one minute (which is what that 60000 is set to), we'll poll the menu for either a Lock Drive command (on lines 23/24) or an Exit command (on lines 25/26). Exit is obviously easy to read. Lets take a look at the _LockDrive function down on lines 51 to 61. On line 52 we again pull for all the removable drives. We test to make sure that the $letter variable is an array on line 53, and if it is, we keep going. Otherwise the function ends. On 54-59, we loop through every letter in that array of drive letters. On 55, we are asking the user to "Lock this drive". If the user presses Yes, we write to the drive a file named "IAmLocked" (on line 56) and then eject the volume (on line 57). If the user presses No, then we simply move on to the next removable drive. And that completes your locking application. I hope you find the changes useful and the explanation informative. LvlUp
×
×
  • Create New...