Sign in to follow this  
Followers 0
Shocker

Start ONLY from Mass Storage?

13 posts in this topic

Hi there,

is it possible that a Prog can ONLY be started from a Mass Storage like USB Stick, external HDD, SD Card whatever?

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Thx for reply! I test DriveGetDrive() for example with >THIS, but he show me a USB HDD as a FIXED Drive... :blink:

Edited by Shocker

Share this post


Link to post
Share on other sites

Have nothing yet... Looking for a "Startup Check" which test, whether used Drive is a Mass Storage or not.

Share this post


Link to post
Share on other sites

No, i don't expect that a code work 100% at first time  :D (but sometimes do). I search what i need and test different code snipets, or ask if i don't find right one.

Share this post


Link to post
Share on other sites

getdrives()

 Func getdrives()
; Fill the destination list
Local $drives = DriveGetDrive("ALL")
If @error == 1 Then
  MsgBox(0, "Error", "No removeable media found, cannot continue!" & @CRLF & "Please insert a micro SD card or other removeable media to copy to.", 15)
  ;GUISetState(@SW_SHOW, $mainwindow)
  GUISetState(@SW_HIDE, $cabswindow)
Else
  For $i = 1 To $drives[0]
   Local $drive = $i
   Local $type = DriveGetType($drives[$i] & "")
   If @error == 1 Then
    $type = "Error getting drive type"
   EndIf
   ConsoleWrite($drives[$i] & "\ " & $type & @CRLF)
  Next
EndIf
EndFunc   ;==>GetDrives

Result...

c: Fixed

d: Fixed

e: Fixed

f: CDROM

g: CDROM

h: Fixed -> is USB HDD

i: Removable -> is a USB Stick

Share this post


Link to post
Share on other sites

Type of drive to find:
"ALL", "CDROM", "REMOVABLE", "FIXED", "NETWORK", "RAMDISK", or "UNKNOWN"

use what you need ?

Share this post


Link to post
Share on other sites

You can use a WMI query to find out if the device is an USB one or not.

Here is an example :

Local $aUSBDrives = _ListUSBVolumes()


Local $iRunFromUSB = 0
For $i = 1 To $aUSBDrives[0]
    If StringRegExp( @ScriptDir, "(?i)^" & $aUSBDrives[$i]) Then
        $iRunFromUSB = 1
        ExitLoop
    EndIf
Next

If $iRunFromUSB Then
    MsgBox(0, "", "OK")
Else
    MsgBox(16, "", "The program must be launched from an USB drive !")
EndIf
 
 
 
; #FUNCTION# ====================================================================================================================
; Name ..........: _ListUSBVolumes
; Description ...: List USB volumes
; Syntax ........: _ListUSBVolumes()
; Parameters ....: None
; Return values .: An array of strings (USB drive letter followed by colon) of drives found.
;                  The zeroth array element contains the number of drives.
; Author ........: jguinch
; ===============================================================================================================================
Func _ListUSBVolumes()
    Local $aResult[1]
 
    Local $oDrive, $sDeviceID, $oPartitions, $oPartition, $oLogicalDisks, $oLogicalDisk, $sDriveLetter
    Local $oWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
 
    Local $oDiskDrives = $oWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive Where InterfaceType = 'USB'")
 
    For $oDrive In $oDiskDrives
        $sDeviceID = StringReplace($oDrive.DeviceID, "\", "\\")
        $oPartitions = $oWMIService.ExecQuery _
            ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & _
                $sDeviceID & """} WHERE AssocClass = " & _
                    "Win32_DiskDriveToDiskPartition")
 
        For $oPartition In $oPartitions
            $oLogicalDisks = $oWMIService.ExecQuery _
                ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
                    $oPartition.DeviceID & """} WHERE AssocClass = " & _
                        "Win32_LogicalDiskToPartition")
 
            For $oLogicalDisk In $oLogicalDisks
                $sDriveLetter = $oLogicalDisk.DeviceID
                If $sDriveLetter <> "" Then
                    Redim $aResult[ UBound($aResult) + 1]
                    $aResult[ UBound($aResult) - 1] = $sDriveLetter
                EndIf
            Next
        Next
    Next
 
    $aResult[0] = UBound($aResult) - 1
    Return $aResult
EndFunc
1 person likes this

Share this post


Link to post
Share on other sites

@jguinch

Thx for reply! It works very GOOD - MERCI INFINIMENT ;)

Is it same Code for both OS (X86/64)?

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