USB storage device serial numbers

Phil Davis revdev at pdslabs.net
Mon Dec 25 08:59:59 CST 2006


Here's a Windows-only function that returns the serial numbers of all USB 
storage devices known to the OS (cameras, external drives, flash drives). Watch 
line wraps in the queryRegistry lines especially!

This was tested on XP only.

Merry Christmas!
Phil Davis



on mouseUp
   put usbDriveSerialNumbers() into fld 1
end mouseUp



function usbDriveSerialNumbers
   # Return a return-delimited list of USB storage device serial numbers

   -- bail out is platform is not Windows
   if the platform <> "Win32" then return empty

   -- get 'Count' DWORD value from registry
   put 
queryRegistry("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum\Count") 
into regCount

   -- convert registry DWORD value to a signed integer
   put empty into tCount -- init required for use in binaryDecode
   get binaryDecode("i1",regCount,tCount)

   -- bail out if no USB storage devices were found
   if tCount = zero then return empty

   -- extract serial numbers from registry entries
   put zero into x
   set the itemDelimiter to backslash
   repeat tCount times -- starting with entry 0
     put 
queryRegistry("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Enum\" 
& x) into tData
     put last item of tData & cr after tList
     add 1 to x
   end repeat
   delete last char of tList -- the trailing CR

   -- pass info back to caller
   return tList
end usbDriveSerialNumbers



More information about the use-livecode mailing list