Sign in to follow this  
Followers 0

Password Protected Flash Drive

13 posts in this topic

Posted

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!

Share this post


Link to post
Share on other sites



Posted

This is the 3rd time you've posted an almost identical thread for the exact same issue, and that's not including the thread you posted to chat asking people to go to your thread for help. Stick to one at a time.

Share this post


Link to post
Share on other sites

Posted

chause4.

Stick to one at a time

Good advice - follow it or you may find it difficult to post at all for a while. :oops:

M23

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

chause4,

Alright fine, but how am I supposed to get help with my program

No-one here is under any obligation to help you - remember we are all volunteers and there is no requirement to offer assistance to other members. Perhaps no-one can offer a solution - do you want "cannot help" posts from all 48,696 (as at 1756 GMT today) members? :oops:

Anyway, simply reposting the same code several times is just going to annoy others and attract unwanted attention - as it indeed has - so please do not do it again. Just be patient. :bye:

M23

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

Your "checker" program exits as soon the file isn't on any removable drive, so it's not running in the back ground all the time.

Second, you're recursing back into the function every 3 minutes and the script will crash because of this. It will take an extremely long time to crash, but if you ever change the sleep period it's going to happen sooner.

Other than that, I can't help much because you have far too many scripts running at one time to make any sense out of what is doing what, you could probably condense this into 2 scripts, one being Checker and the other doing everything else.

Share this post


Link to post
Share on other sites

Posted (edited)

As BrewManNH said this code could easily be consolidated into two scripts, secondly I think hard coding the drive letters is not only going to cause you problems but also problems for those who manage to run this, which brings me onto how do I use this? To use this analogy you've provided lego bricks which should be a house but you're expecting me to build it first without an instruction manual.

Edited by guinness

Share this post


Link to post
Share on other sites

Posted

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?

Share this post


Link to post
Share on other sites

Posted (edited)

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

Edited by LvlUp
Solarlight27 likes this

Share this post


Link to post
Share on other sites

Posted (edited)

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..

Edited by Solarlight27

Share this post


Link to post
Share on other sites

Posted

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.

Solarlight27 likes this

Share this post


Link to post
Share on other sites

Posted (edited)

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!!

;)

Edited by Solarlight27

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.