How to read manufacturer serial number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nebojsa4
    New Member
    • Dec 2006
    • 6

    How to read manufacturer serial number

    Hi.
    First, sorry on my weak English to all.

    Qusetion:
    How to read (in VB) Manufacturer serial number of Hard disk drive?

    Not volume/serial number of C:, D:, etc. partitons.

    For reading volume/serial number of hard disk C: etc, You can use Microsoft Scripting Runtime (in VB):

    Dim ff As New Scripting.FileS ystemObject
    Dim drv As Drive
    Set drv = ff.GetDrive("C" )
    SerialNumber = drv.SerialNumbe r
    MsgBox "Serial/Volume number of C: is " + Trim(Str(Serial Number))

    But, I need manufacturer serial number of hard disk drive, because, after formatting hard disk You'll get another volume/serial number, and that is very bad if my program should check that parameter, if he can't recognize that this HDD is the same one but only formatted (and after formatting there is set another volume/serial number).

    How I see, many people swap meaning of volume and serial number of some drive.

    Thx to all.
  • sashi
    Recognized Expert Top Contributor
    • Jun 2006
    • 1749

    #2
    Originally posted by Nebojsa4
    Hi.
    First, sorry on my weak English to all.

    Qusetion:
    How to read (in VB) Manufacturer serial number of Hard disk drive?

    Not volume/serial number of C:, D:, etc. partitons.

    For reading volume/serial number of hard disk C: etc, You can use Microsoft Scripting Runtime (in VB):

    Dim ff As New Scripting.FileS ystemObject
    Dim drv As Drive
    Set drv = ff.GetDrive("C" )
    SerialNumber = drv.SerialNumbe r
    MsgBox "Serial/Volume number of C: is " + Trim(Str(Serial Number))

    But, I need manufacturer serial number of hard disk drive, because, after formatting hard disk You'll get another volume/serial number, and that is very bad if my program should check that parameter, if he can't recognize that this HDD is the same one but only formatted (and after formatting there is set another volume/serial number).

    How I see, many people swap meaning of volume and serial number of some drive.

    Thx to all.
    Hi there,

    Kindly refer to below attached code segment, hope it helps. Good luck & take care.

    API declaration
    Code:
    Option Explicit
    
    Private Declare Function GetVolumeInformation& Lib "kernel32" _
        Alias "GetVolumeInformationA" (ByVal lpRootPathName _
        As String, ByVal pVolumeNameBuffer As String, ByVal _
        nVolumeNameSize As Long, lpVolumeSerialNumber As Long, _
        lpMaximumComponentLength As Long, lpFileSystemFlags As _
        Long, ByVal lpFileSystemNameBuffer As String, ByVal _
        nFileSystemNameSize As Long)
        Const MAX_FILENAME_LEN = 256
    Form code
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Label1.Caption = SerNum("C") 'C is the standard harddisk
    End Sub
    
    Public Function SerNum(Drive$) As Long
        Dim No&, s As String * MAX_FILENAME_LEN
        Call GetVolumeInformation(Drive + ":\", s, MAX_FILENAME_LEN, _
        No, 0&, 0&, s, MAX_FILENAME_LEN)
        SerNum = No
    End Function

    Comment

    • Nebojsa4
      New Member
      • Dec 2006
      • 6

      #3
      Sorry, but that is not for what I looking for.
      If You try code witch I wrote You should see that both code show You the same number, or if You change drive letter to D code should wrote another number.

      For example:
      I have one hdd with 2 partitions (C: and D:). There is no matter witch partition choose (both partitions are the same drive - physically), program must show the same serial number.

      Usually, the manufacturer serial number consist some letters and numbers (example: WRT-234A34) - that I need. If my hdd is separated to 2, 3, etc partitions, any partition I check I must get the same serial number, because I need serial number of physically drive (component of computer).

      Anyway, thx on code.

      Comment

      • sashi
        Recognized Expert Top Contributor
        • Jun 2006
        • 1749

        #4
        Originally posted by Nebojsa4
        Sorry, but that is not for what I looking for.
        If You try code witch I wrote You should see that both code show You the same number, or if You change drive letter to D code should wrote another number.

        For example:
        I have one hdd with 2 partitions (C: and D:). There is no matter witch partition choose (both partitions are the same drive - physically), program must show the same serial number.

        Usually, the manufacturer serial number consist some letters and numbers (example: WRT-234A34) - that I need. If my hdd is separated to 2, 3, etc partitions, any partition I check I must get the same serial number, because I need serial number of physically drive (component of computer).

        Anyway, thx on code.
        Hi there,

        Am not very sure on how to that, in the meantime i'll check that for you, till then hang on. Take care.

        Comment

        • Nebojsa4
          New Member
          • Dec 2006
          • 6

          #5
          I'm not sure, but I think, maybe, there is some way to get that serial number from BIOS?

          Comment

          • Eftim Stoyanov
            New Member
            • Apr 2007
            • 1

            #6
            Dear Sir,

            Congratulations ! Your code and method is very clever and great! It is not the manufacturer serial number of HD. It is the volume number of the disk C: or D:, but it works perfectly! I use an old version of VB 6.0 and I had to make some corrections in the program, but it really returns the volume numbers of the disk C: and D: partitions!
            My E - mail address is eftim55@abv.bg!
            Eftim Stoyanov, Burgas, Bulgaria

            Originally posted by sashi
            Hi there,

            Kindly refer to below attached code segment, hope it helps. Good luck & take care.

            API declaration
            Code:
            Option Explicit
            
            Private Declare Function GetVolumeInformation& Lib "kernel32" _
                Alias "GetVolumeInformationA" (ByVal lpRootPathName _
                As String, ByVal pVolumeNameBuffer As String, ByVal _
                nVolumeNameSize As Long, lpVolumeSerialNumber As Long, _
                lpMaximumComponentLength As Long, lpFileSystemFlags As _
                Long, ByVal lpFileSystemNameBuffer As String, ByVal _
                nFileSystemNameSize As Long)
                Const MAX_FILENAME_LEN = 256
            Form code
            Code:
            Option Explicit
            
            Private Sub Command1_Click()
                Label1.Caption = SerNum("C") 'C is the standard harddisk
            End Sub
            
            Public Function SerNum(Drive$) As Long
                Dim No&, s As String * MAX_FILENAME_LEN
                Call GetVolumeInformation(Drive + ":\", s, MAX_FILENAME_LEN, _
                No, 0&, 0&, s, MAX_FILENAME_LEN)
                SerNum = No
            End Function

            Comment

            • Rajkumar GS
              New Member
              • Dec 2007
              • 4

              #7
              Originally posted by Eftim Stoyanov
              Dear Sir,

              Congratulations ! Your code and method is very clever and great! It is not the manufacturer serial number of HD. It is the volume number of the disk C: or D:, but it works perfectly! I use an old version of VB 6.0 and I had to make some corrections in the program, but it really returns the volume numbers of the disk C: and D: partitions!
              My E - mail address is eftim55@abv.bg!
              Eftim Stoyanov, Burgas, Bulgaria

              Hi,

              GetVolumeInform ation will return ONLY the volume serial number, which keeps changing on format.

              Use the following code to retrieve Model Number, Serial Number and Firmware Revision of a hard

              disk.

              Here by I am providing the Class file code & vb file code.

              I am pasting the code of the class file opened by notepad here by
              ---------------------------------------------------------------
              VERSION 1.0 CLASS
              BEGIN
              MultiUse = -1 'True
              Persistable = 0 'NotPersistable
              DataBindingBeha vior = 0 'vbNone
              DataSourceBehav ior = 0 'vbNone
              MTSTransactionM ode = 0 'NotAnMTSObject
              END
              Attribute VB_Name = "HDSN"
              Attribute VB_GlobalNameSp ace = False
              Attribute VB_Creatable = True
              Attribute VB_PredeclaredI d = False
              Attribute VB_Exposed = False
              Attribute VB_Ext_KEY = "SavedWithClass Builder6" ,"Yes"
              Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
              Option Explicit

              ' Antonio Giuliana, 2001-2003

              ' Costanti per l'individuazion e della versione di OS
              Private Const VER_PLATFORM_WI N32S = 0
              Private Const VER_PLATFORM_WI N32_WINDOWS = 1
              Private Const VER_PLATFORM_WI N32_NT = 2

              ' Costanti per la comunicazione con il driver IDE
              Private Const DFP_RECEIVE_DRI VE_DATA = &H7C088

              ' Costanti per la CreateFile
              Private Const FILE_SHARE_READ = &H1
              Private Const FILE_SHARE_WRIT E = &H2
              Private Const GENERIC_READ = &H80000000
              Private Const GENERIC_WRITE = &H40000000
              Private Const OPEN_EXISTING = 3
              Private Const CREATE_NEW = 1

              ' Enumerazione dei comandi per la CmnGetHDData
              Private Enum HDINFO
              HD_MODEL_NUMBER
              HD_SERIAL_NUMBE R
              HD_FIRMWARE_REV ISION
              End Enum

              ' Struttura per l'individuazion e della versione di OS
              Private Type OSVERSIONINFO
              dwOSVersionInfo Size As Long
              dwMajorVersion As Long
              dwMinorVersion As Long
              dwBuildNumber As Long
              dwPlatformId As Long
              szCSDVersion As String * 128
              End Type

              ' Struttura per il campo irDriveRegs della struttura SENDCMDINPARAMS
              Private Type IDEREGS
              bFeaturesReg As Byte
              bSectorCountReg As Byte
              bSectorNumberRe g As Byte
              bCylLowReg As Byte
              bCylHighReg As Byte
              bDriveHeadReg As Byte
              bCommandReg As Byte
              bReserved As Byte
              End Type

              ' Struttura per l'I/O dei comandi al driver IDE
              Private Type SENDCMDINPARAMS
              cBufferSize As Long
              irDriveRegs As IDEREGS
              bDriveNumber As Byte
              bReserved(1 To 3) As Byte
              dwReserved(1 To 4) As Long
              End Type

              ' Struttura per il campo DStatus della struttura SENDCMDOUTPARAM S
              Private Type DRIVERSTATUS
              bDriveError As Byte
              bIDEStatus As Byte
              bReserved(1 To 2) As Byte
              dwReserved(1 To 2) As Long
              End Type

              ' Struttura per l'I/O dei comandi al driver IDE
              Private Type SENDCMDOUTPARAM S
              cBufferSize As Long
              DStatus As DRIVERSTATUS ' ovvero DriverStatus
              bBuffer(1 To 512) As Byte
              End Type

              ' Per ottenere la versione del SO
              Private Declare Function GetVersionEx _
              Lib "kernel32" Alias "GetVersion ExA" _
              (lpVersionInfor mation As OSVERSIONINFO) As Long

              ' Per ottenere un handle al device IDE
              Private Declare Function CreateFile _
              Lib "kernel32" Alias "CreateFile A" _
              (ByVal lpFileName As String, _
              ByVal dwDesiredAccess As Long, _
              ByVal dwShareMode As Long, _
              ByVal lpSecurityAttri butes As Long, _
              ByVal dwCreationDispo sition As Long, _
              ByVal dwFlagsAndAttri butes As Long, _
              ByVal hTemplateFile As Long) As Long

              ' Per chiudere l'handle del device IDE
              Private Declare Function CloseHandle _
              Lib "kernel32" _
              (ByVal hObject As Long) As Long

              ' Per comunicare con il driver IDE
              Private Declare Function DeviceIoControl _
              Lib "kernel32" _
              (ByVal hDevice As Long, _
              ByVal dwIoControlCode As Long, _
              lpInBuffer As Any, _
              ByVal nInBufferSize As Long, _
              lpOutBuffer As Any, _
              ByVal nOutBufferSize As Long, _
              lpBytesReturned As Long, _
              ByVal lpOverlapped As Long) As Long

              ' Per azzerare buffer di scambio dati
              Private Declare Sub ZeroMemory _
              Lib "kernel32" Alias "RtlZeroMem ory" _
              (dest As Any, _
              ByVal numBytes As Long)

              ' Per copiare porzioni di memoria
              Private Declare Sub CopyMemory _
              Lib "kernel32" Alias "RtlMoveMem ory" _
              (Destination As Any, _
              Source As Any, _
              ByVal Length As Long)

              Private Declare Function GetLastError _
              Lib "kernel32" () As Long

              Private mvarCurrentDriv e As Byte ' Drive corrente
              Private mvarPlatform As String ' Piattaforma usata

              Public Property Get Copyright() As String

              ' Copyright
              Copyright = "HDSN Vrs. 1.00, (C) Antonio Giuliana, 2001-2003"

              End Property

              ' Metodo GetModelNumber
              Public Function GetModelNumber( ) As String

              ' Ottiene il ModelNumber
              GetModelNumber = CmnGetHDData(HD _MODEL_NUMBER)

              End Function

              ' Metodo GetSerialNumber
              Public Function GetSerialNumber () As String

              ' Ottiene il SerialNumber
              GetSerialNumber = CmnGetHDData(HD _SERIAL_NUMBER)

              End Function

              ' Metodo GetFirmwareRevi sion
              Public Function GetFirmwareRevi sion() As String

              ' Ottiene la FirmwareRevisio n
              GetFirmwareRevi sion = CmnGetHDData(HD _FIRMWARE_REVIS ION)

              End Function

              ' Proprieta' CurrentDrive
              Public Property Let CurrentDrive(By Val vData As Byte)

              ' Controllo numero di drive fisico IDE
              If vData < 0 Or vData > 3 Then
              Err.Raise 10000, , "Illegal drive number" ' IDE drive 0..3
              End If

              ' Nuovo drive da considerare
              mvarCurrentDriv e = vData

              End Property

              ' Proprieta' CurrentDrive
              Public Property Get CurrentDrive() As Byte

              ' Restituisce drive fisico corrente (IDE 0..3)
              CurrentDrive = mvarCurrentDriv e

              End Property

              ' Proprieta' Platform
              Public Property Get Platform() As String

              ' Restituisce tipo OS
              Platform = mvarPlatform

              End Property

              Private Sub Class_Initializ e()

              ' Individuazione del tipo di OS
              Dim OS As OSVERSIONINFO

              OS.dwOSVersionI nfoSize = Len(OS)
              Call GetVersionEx(OS )
              mvarPlatform = "Unk"
              Select Case OS.dwPlatformId
              Case Is = VER_PLATFORM_WI N32S
              mvarPlatform = "32S" ' Win32S
              Case Is = VER_PLATFORM_WI N32_WINDOWS
              If OS.dwMinorVersi on = 0 Then
              mvarPlatform = "W95" ' Win 95
              Else
              mvarPlatform = "W98" ' Win 98
              End If
              Case Is = VER_PLATFORM_WI N32_NT
              mvarPlatform = "WNT" ' Win NT/2000
              End Select

              End Sub

              Private Function CmnGetHDData(hd i As HDINFO) As String

              ' Rilevazione proprieta' IDE

              Dim bin As SENDCMDINPARAMS
              Dim bout As SENDCMDOUTPARAM S
              Dim hdh As Long
              Dim br As Long
              Dim ix As Long
              Dim hddfr As Long
              Dim hddln As Long
              Dim s As String

              Select Case hdi ' Selezione tipo caratteristica richiesta
              Case HD_MODEL_NUMBER
              hddfr = 55 ' Posizione nel buffer del ModelNumber
              hddln = 40 ' Lunghezza nel buffer del ModelNumber
              Case HD_SERIAL_NUMBE R
              hddfr = 21 ' Posizione nel buffer del SerialNumber
              hddln = 20 ' Lunghezza nel buffer del SerialNumber
              Case HD_FIRMWARE_REV ISION
              hddfr = 47 ' Posizione nel buffer del FirmwareRevisio n
              hddln = 8 ' Lunghezza nel buffer del FirmwareRevisio n
              Case Else
              Err.Raise 10001, "Illegal HD Data type" ' Altre informazioni non disponibili

              (Evoluzione futura)
              End Select

              Select Case mvarPlatform
              Case "WNT"
              ' Per Win NT/2000 apertura handle al drive fisico
              hdh = CreateFile("\\. \PhysicalDrive" & mvarCurrentDriv e, _
              GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRIT E, _
              0, OPEN_EXISTING, 0, 0)
              Case "W95", "W98"
              ' Per Win 9X apertura handle al driver SMART
              ' (in \WINDOWS\SYSTEM da spostare in \WINDOWS\SYSTEM \IOSUBSYS)
              ' che comunica con il driver IDE
              hdh = CreateFile("\\. \Smartvsd", _
              0, 0, 0, CREATE_NEW, 0, 0)
              Case Else
              ' Piattaforma non supportata (Win32S)
              Err.Raise 10002, , "Illegal platform (only WNT, W98 or W95)" ' Altre piattaforme

              non gestite
              End Select
              ' Controllo validità handle
              If hdh = 0 Then
              Err.Raise 10003, , "Error on CreateFile"
              End If

              ' Azzeramento strutture per l'I/O da driver
              ZeroMemory bin, Len(bin)
              ZeroMemory bout, Len(bout)

              ' Preparazione parametri struttura di richiesta al driver
              With bin
              .bDriveNumber = mvarCurrentDriv e
              .cBufferSize = 512
              With .irDriveRegs
              If (mvarCurrentDri ve And 1) Then
              .bDriveHeadReg = &HB0
              Else
              .bDriveHeadReg = &HA0
              End If
              .bCommandReg = &HEC
              .bSectorCountRe g = 1
              .bSectorNumberR eg = 1
              End With
              End With

              ' Richiesta al driver
              DeviceIoControl hdh, DFP_RECEIVE_DRI VE_DATA, _
              bin, Len(bin), bout, Len(bout), br, 0

              ' Formazione stringa di risposta
              ' da buffer di uscita
              ' L'ordine dei byte e' invertito
              s = ""
              For ix = hddfr To hddfr + hddln - 1 Step 2
              If bout.bBuffer(ix + 1) = 0 Then Exit For
              s = s & Chr(bout.bBuffe r(ix + 1))
              If bout.bBuffer(ix ) = 0 Then Exit For
              s = s & Chr(bout.bBuffe r(ix))
              Next ix

              ' Chiusura handle
              CloseHandle hdh

              ' Restituzione informazione richiesta
              CmnGetHDData = Trim(s)

              End Function

              --------------------------------------------------------------------------

              In the form, place a combobox with values 0,1,2,3 represents Primary Master, Primary Slave,

              Secondary Master & Secondary Slave HDDs.

              Code is

              Dim h As HDSN

              Private Sub cmdGo_Click()

              Dim hT As Long
              Dim uW() As Byte
              Dim dW() As Byte
              Dim pW() As Byte

              Set h = New HDSN

              With h
              .CurrentDrive = Val(cbDrive.Tex t)

              lstInfo.Clear
              lstInfo.AddItem "Current drive: " & .CurrentDrive
              lstInfo.AddItem ""
              lstInfo.AddItem "Model number: " & .GetModelNumber
              lstInfo.AddItem "Serial number: " & .GetSerialNumbe r
              lstInfo.AddItem "Firmware Revision: " & .GetFirmwareRev ision
              lstInfo.AddItem ""
              lstInfo.AddItem "Copyright: " & .Copyright
              End With

              Set h = Nothing

              End Sub

              Private Sub Form_Load()
              cbDrive.ListInd ex = 0
              End Sub

              --------------------------------------------------------------------
              Hope this will help you...
              Thanks
              Raj

              Comment

              • banhthanhvi
                New Member
                • Mar 2009
                • 1

                #8
                c#

                anyone have code in c#?

                Comment

                • 9815402440
                  New Member
                  • Oct 2007
                  • 180

                  #9
                  hi
                  i tried the code
                  and found it ok. i was looking for the same since long. many many thanks for the code
                  regards
                  manpreet singh dhillon hoshiarpur

                  Comment

                  • Alesh
                    New Member
                    • Feb 2014
                    • 1

                    #10
                    Hi All,

                    code to get serial HDD works fine, but when I run it on W7 and I have only SSD I do not get nothing. I can get correct serial HD on W7 with standard HDD.

                    Do you have any idea how to make it work for SSD?
                    In device manager I have type INTEL SSDSC2CW... ATA Device

                    Thanks,
                    Al

                    Comment

                    Working...