Retrieving a Drive’s Serial Number

I want to get the serial number from my hard drive and my floppy disk drive. How can I do this?

The GetVolumeInformation API call is used to retrieve information about hard drives, floppy drives, and even CD-ROM drives. Notice how your Windows CD player can recognize which CD is inserted? It is using the serial number of the CD-ROM (obtained with this API call) to make this determination. Just substitute the drive letter where it currently says “F:” and you can check on any of your drives.

The code that follows will return the serial number (as a long), as well as the volume name and volume type, such as FAT. However, the text in the returned strings is ended with a null character – chr$(0). That’s the reason for the InStr call — it only shows the portion of the string up to the null character. If you don’t do that, the DLL showing the MsgBox interprets the null character as the end of line and no further text is displayed.

Option ExplicitPrivate Declare Function GetVolumeInformation _   Lib “kernel32” Alias “GetVolumeInformationA” _   (ByVal lpRootPathName As String, _   ByVal lpVolumeNameBuffer As String, _   ByVal nVolumeNameSize As Long, _   lpVolumeSerialNumber As Long, _   lpMaximumComponentLength As Long, _   lpFileSystemFlags As Long, _   ByVal lpFileSystemNameBuffer As String, _   ByVal nFileSystemNameSize As Long) As Long   Private Sub cmdTest_Click()   Dim iSerialNum As Long   Dim sVolumeLabel As String   Dim sVolumeType As String   Dim iRetVal As Long   sVolumeLabel = Space(255)   sVolumeType = Space(255)   iRetVal = GetVolumeInformation(“f:”, _      sVolumeLabel, Len(sVolumeLabel), _      iSerialNum, _      0, 0, _      sVolumeType, Len(sVolumeType))      MsgBox “Disk Serial Number: ” & iSerialNum & vbCrLf _      & “Volume Type: ” & Left$(sVolumeType, InStr(sVolumeType, Chr$(0)) – 1) & vbCrLf _      & “Volume Label: ” & Left$(sVolumeLabel, InStr(sVolumeLabel, Chr$(0)) – 1)End Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS