Get hard drive serial without API using VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tuxalot
    New Member
    • Feb 2009
    • 200

    Get hard drive serial without API using VBA

    Has anyone used this code to get the serial for a hard drive?

    Code:
    Function HDSerialNumber() As String
    Dim fsObj As Object
    Dim drv As Object
        Set fsObj = CreateObject("Scripting.FileSystemObject")
        Set drv = fsObj.Drives("C")
        HDSerialNumber = Left(Hex(drv.SerialNumber), 4) _
                         & "-" & Right(Hex(drv.SerialNumber), 4)
        Debug.Print HDSerialNumber
    
    End Function
    Is this a reliable way to do it or is there a better way?
  • Mihail
    Contributor
    • Apr 2011
    • 759

    #2
    The function seems to return all the time the same value for the same drive letter.
    But, as far as I don't know how to check the drive serial using other method I can't say if your code is working.

    The Drives() function do not accept a variable as argument as I expect.

    This are values that I obtain running the function:

    For "C" : 84E8-BE57
    For "D" : F8CB-609F

    For CD drives it seems to depend of CD (or DVD) but is all the time the same for a certain CD (DVD). I have two CD (DVD) drives attached to my PC and I make tests for both of them with the same results: same CD (or DVD) => same "serial".

    Hope this information help you.
    Any way I post here for subscribe purpose.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Here is another approach using the API:
      1. API Declaration:
        Code:
        Public 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
      2. Call to Function:
        Code:
        Dim strSerialNum As Long
        Dim strVName As String
        Dim strFSName As String
        
        'Get the Volume information
        GetVolumeInformation "C:\", strVName, 255, strSerialNum, 0, 0, strFSName, 255
        
        Debug.Print "The Serial Number of Drive C:\ is: " & Trim(Str$(strSerialNum))
      3. OUTPUT:
        Code:
        'With Drive Substitution:
        The Serial Number of Drive C:\ is: 1755256066
        The Serial Number of Drive F:\ is: 861870070

      Comment

      • tuxalot
        New Member
        • Feb 2009
        • 200

        #4
        Thanks for your input ADezii. I am not sure what number I am presented with when I run your code. With Hardware ID Extractor I find that the code I posted gives the partition ID (volume serial number) which unfortunately will change if the drive is reformatted.

        Is there a way to verify that the code you provided gives the real serial of the hard drive?

        DiskId32 from www(dot)winsim( dot)com/diskid32/diskid32(dot)ht ml gives different information than both of the solutions posted here.

        Now I'm confused.

        Comment

        Working...