The volume serial number is pretty easy to get, but it is also trivial for users to change.
The media serial number is much harder to get. Most attempts use WMI, but you can't rely on WMI being present and active (it's a heavy service intended for administration, not applications). Worse yet many manufacturers don't even use unique values.
Function GetDriveSerialNumber$(Path$)
Dim a$, f%
ShellAndWait Environ$("COMSPEC") & " /C VOL " & Left$(Path$, 2) & " > temp.txt", vbHide
f = FreeFile
Open "temp.txt" For Input As f
a$ = Input$(LOF(f), f)
Close f
Kill "temp.txt"
a$ = Replace$(a$, vbCrLf, "")
GetDriveSerialNumber$ = Right$(Trim$(a$), 9)
End Function
note you may need to use elevated privilages if you are running as limited user, you may also be able to use apis
i do not remember if APIs that return serial number of HDD will work for usb, but they will not work as limited user
see this thread for more info http://www.vbforums.com/showthread.p...ght=hdd+serial
edit on testing i believe the code in the first link returns the id of the usb interface, not the hdd serial number
and the wmi for getting serial number of hdd also appears to not work with external drives
Last edited by westconn1; Feb 23rd, 2011 at 04:16 AM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
only if some code runs on start up and no one reformats, changes user, has internet connection etc, etc
i might think about it some more
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete