Jump to content

Error in function that displays lyrics and serial number of the stick.


Recommended Posts

I'm using a code to return the serial number and the letter of a flash drive, works very well if the connected device is a USB stick gives more error if you have a USB plugged drawer on the computer, how can I do to avoid this error?

#include <Array.au3>
 
eachUsbDevice()
 
Func getUsbSerial()
Local $letter[1], $disk[1], $aArray[1][2]
Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2")
Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", 0x10 + 0x20)
For $objitem In $colItems
If $objitem.interfacetype = "USB" Then
For $partition In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $objitem.DeviceId & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
Next
For $loDisk In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $partition.DeviceId & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
Next
$aRegExSer = StringRegExp($objitem.pnpdeviceid, "\x5C([0-9a-fxA-FX]+)&", 3)
If IsArray($aRegExSer) Then
_ArrayAdd($disk, $aRegExSer[0])
_ArrayAdd($letter, $loDisk.DeviceId)
EndIf
EndIf
Next
ReDim $aArray[UBound($disk)][2]
For $i = 0 To UBound($disk) - 1
$aArray[$i][0] = $disk[$i]
$aArray[$i][1] = $letter[$i]
Next
Return $aArray
EndFunc   ;==>getUsbSerial
 
Func eachUsbDevice()
Local $ret = getUsbSerial()
For $i = 1 To UBound($ret) - 1
MsgBox(262144, 'disk', "the serial "& $ret[$i] [0] &" belongs the letter = " & $ret[$i][1])
Next
EndFunc   ;==>eachUsbDevice

Error when I have a USB plugged drawer:

For $loDisk In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $partition.DeviceId & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
    For $loDisk In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $partition^ ERROR
Edited by Belini
Link to comment
Share on other sites

Link to comment
Share on other sites

Does not work on all models of stick in some use DriveGetDrive recognizes how hard disk

Edited by Belini
Link to comment
Share on other sites

Maybe >SetupDiGet functions are the solution

EDIT:

As MSDN documentation reports in GetDriveType reference:

To determine whether a drive is a USB-type drive, call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property

Edited by j0kky
Link to comment
Share on other sites

For my case will not serve why does not specify whether the drive is a flash drive and also does not return the letter-affinity.

Class name: DiskDrive
Friendly name: General USB Flash Disk USB Device
Device Description: Disk drive ; I need to know if it is a removable usb drive

 

 

Link to comment
Share on other sites

It was just an example of an UDF that uses SetupDiGetDeviceRegistryProperty function but I've written too:

To determine whether a drive is a USB-type drive, call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property

Edited by j0kky
Link to comment
Share on other sites

The code I posted works fine for me and the best fix would be just to not give error when a connected USB drawer.

Link to comment
Share on other sites

Problem solved:

#include <Array.au3>
 
eachUsbDevice()
 
Func getUsbSerial()
Local $colItems, $letter[1], $disk[1], $aArray[1][2]
Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2")
If IsObj($objWMIService) Then ; <========================= changed here
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", 0x10 + 0x20)
For $objitem In $colItems
If $objitem.interfacetype = "USB" And $objitem.DeviceId Then; <========================= changed here
For $partition In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & $objitem.DeviceId & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
Next
If (IsObj($partition) And $partition.DeviceId) Then; <========================= changed here
For $loDisk In $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & $partition.DeviceId & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
Next
$aRegExSer = StringRegExp($objitem.pnpdeviceid, "\x5C([0-9a-fxA-FX]+)&", 3)
If IsArray($aRegExSer) Then
_ArrayAdd($disk, $aRegExSer[0])
_ArrayAdd($letter, $loDisk.DeviceId)
EndIf
EndIf
EndIf
Next
ReDim $aArray[UBound($disk)][2]
For $i = 0 To UBound($disk) - 1
$aArray[$i][0] = $disk[$i]
$aArray[$i][1] = $letter[$i]
Next
Return $aArray
EndIf
EndFunc   ;==>getUsbSerial
 
Func eachUsbDevice()
Local $ret = getUsbSerial()
For $i = 1 To UBound($ret) - 1
MsgBox(262144, 'disk', "the serial " & $ret[$i][0] & " belongs the letter = " & $ret[$i][1])
Next
EndFunc   ;==>eachUsbDevice

 

Thanks to @joelson0007 forum Autoit Brazil.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...