This code id very useful to create a kind of USB protection (using a normal USB pen drive).
Each Pen Driver generate a different unique ID.
An example usage can be:
-1st store oh HD the pen drive seriale
-2nd compare the inserted pen drive serial with the one stored on HD .. when the serials are the same then 'do something'
Code:
Func _GetDeviceID($drive,$fullid=0)
$objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2")
If Not IsObj($objWMIService) Then Return -1 ;Failed to Connect to WMI on Local Machine
$colDevice = $objWMIService.ExecQuery("SELECT * from Win32_LogicalDiskToPartition")
$var=""
For $objItem in $colDevice
If StringInStr($objItem.Dependent,$drive) Then
$var=StringTrimLeft($objItem.Antecedent,stringInstr($objItem.Antecedent,"="))
EndIf
Next
If Not $var Then Return -2 ;Failed to Find Drive Letter
$colDevice = $objWMIService.ExecQuery("SELECT * from Win32_DiskDriveToDiskPartition")
$diskpartition = $var
$var=""
For $objItem in $colDevice
If StringInStr($objItem.Dependent,$diskpartition) Then
$var=StringTrimLeft($objItem.Antecedent,stringInstr($objItem.Antecedent,"="))
EndIf
Next
If Not $var Then Return -3 ;Failed to Find Physical Drive #
$colDevice = $objWMIService.ExecQuery("SELECT * from Win32_DiskDrive")
$physicaldrive = StringReplace(StringReplace($var,"\\","\"),'"',"")
$var=""
For $objItem in $colDevice
If $objItem.DeviceID = $physicaldrive Then
$var=$objItem.PNPDeviceID
EndIf
Next
If Not $var Then Return -4 ;Failed to Find DeviceID
If Not $fullid Then $var = StringTrimLeft($var,StringInstr($var,"\",0,-1)) ;Return Full DeviceID (Full is very dirty)
Return $var
EndFunc
Ex. Usage:
Code:
$idCurrent = _GetDeviceID($drive)