Results 1 to 7 of 7

Thread: HOW to get the Hard Disk Serial number

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    HOW to get the Hard Disk Serial number

    Does anyone knows why this code is not working?
    VB Code:
    1. Public Class Form1
    2.  
    3.     Private 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
    4.  
    5.  
    6.     Public Function GetSerial(ByVal str As String) As Long
    7.         Dim Buf$, Name$, Flags&, Length&
    8.         Dim Serial As Long
    9.         GetVolumeInformation(str, Buf$, 255, Serial, Length, Flags, Name$, 255)
    10.         GetSerial = Serial
    11.     End Function
    12.  
    13.     Private Sub Form_Load()
    14.         Label1.Text = CStr(GetSerial("C:\"))
    15.         'This prints the hard drive serial number of C: here. No other drive Is needed
    16.     End Sub
    17.  
    18. End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: HOW to get the Hard Disk Serial number

    Here's a quote from the MSDN topic for that function:
    This function returns the volume serial number that the operating system assigns when a hard disk is formatted. To programmatically obtain the hard disk's serial number that the manufacturer assigns, use the Windows Management Instrumentation (WMI) Win32_PhysicalMedia property SerialNumber.
    Which serial number is it that you are after? I can give you an example of using WMI to get the manufacturers serial number if that's what you want.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: HOW to get the Hard Disk Serial number

    The code isn't working because it was originally vb6 code...
    Here is a .Net (2003) version (after much faffing about)
    (for vb 2005 you could use uintegers)

    VB Code:
    1. ' Imports System.Runtime.InteropServices
    2.     ' Imports System.Text
    3.  
    4.     <DllImport("kernel32", SetLastError:=True)> _
    5.  Public Shared Function GetVolumeInformation _
    6.         (ByVal lpRootPathName As String, _
    7.         ByVal lpVolumeName As StringBuilder, _
    8.         ByVal nVolumeNameSize As Integer, _
    9.         ByRef lpVolumeSerialNumber As Integer, _
    10.         ByRef lpMaximumComponentLength As Integer, _
    11.         ByRef lpFileSystemFlags As fsflags, _
    12.         ByVal lpFileSystemName As StringBuilder, _
    13.         ByVal nFileSystemNameSize As Integer) As Integer
    14.  
    15.     End Function
    16.  
    17.     <DllImport("kernel32")> _
    18.     Public Shared Function GetLastError() As Integer
    19.     End Function
    20.  
    21.     Public Function GetSerial(ByVal str As String) As String
    22.  
    23.         Dim volumeNameLength As Integer = 256
    24.         Dim volumeName As New StringBuilder(volumeNameLength)
    25.         Dim volumeSerialNumber As Integer
    26.         Dim maxCompLen As Integer
    27.         Dim fileSystemFlags As fsflags
    28.         Dim filesystemnamelength As Integer = 256
    29.         Dim fileSystemName As New StringBuilder(filesystemnamelength)
    30.         Dim ret As Boolean
    31.         ret = GetVolumeInformation(str, volumeName, volumeNameLength, _
    32.         volumeSerialNumber, maxCompLen, fileSystemFlags, fileSystemName, filesystemnamelength)
    33.         If ret = 0 Then
    34.             Me.Text = GetLastError()
    35.         End If
    36.         Me.Label1.Text = "Volume Name: " & volumeName.ToString & Environment.NewLine
    37.         Me.Label1.Text &= "Volume Serial Number: " & hex(volumeSerialNumber) & Environment.NewLine
    38.         Me.Label1.Text &= "FileSystem Name: " & fileSystemName.ToString & Environment.NewLine
    39.         Me.Label1.Text &= "Filesystem Flags: " & fileSystemFlags.ToString
    40.         Return volumeSerialNumber.ToString
    41.  
    42.     End Function
    43.  
    44.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    45.         GetSerial("C:\")
    46.         Me.Label1.Dock = Dock.Fill
    47.  
    48.     End Sub
    49.  
    50.     <FlagsAttribute()> _
    51.     Public Enum fsflags
    52.         FILE_NAMED_STREAMS = &H40000
    53.         FILE_READ_ONLY_VOLUME = &H80000
    54.         FILE_SUPPORTS_OBJECT_IDS = &H10000
    55.         FILE_SUPPORTS_REPARSE_POINTS = &H80
    56.         FILE_SUPPORTS_SPARSE_FILES = &H40
    57.         FILE_VOLUME_QUOTAS = &H20
    58.         FS_CASE_IS_PRESERVED = 2
    59.         FS_CASE_SENSITIVE = 1
    60.         FS_FILE_COMPRESSION = &H10
    61.         FS_FILE_ENCRYPTION = &H20000
    62.         FS_PERSISTENT_ACLS = 8
    63.         FS_UNICODE_STORED_ON_DISK = 4
    64.         FS_VOL_IS_COMPRESSED = &H8000
    65.     End Enum
    Last edited by jo0ls; Dec 7th, 2005 at 08:05 AM.

  4. #4
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: HOW to get the Hard Disk Serial number

    I think the serial numbers in wmi and the api call are the same. Unless I'm in the wrong class or have the wrong property:

    VB Code:
    1. ' imports system.management
    2.         ' code based on MS example
    3.         Dim query As New SelectQuery("Win32_LogicalDisk")
    4.  
    5.         'ManagementObjectSearcher retrieves a collection of WMI objects based on
    6.         ' the query.
    7.         Dim search As New ManagementObjectSearcher(query)
    8.  
    9.         ' Display each entry for Win32_bios
    10.         Dim info As ManagementObject
    11.         For Each info In search.Get()
    12.             If Not info("VolumeSerialNumber") Is Nothing Then
    13.                 label1.Text &= info("VolumeSerialNumber").ToString & Environment.NewLine
    14.             End If
    15.         Next


    Both serial numbers match the one you get with a dir command at the command prompt.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: HOW to get the Hard Disk Serial number

    Quote Originally Posted by jo0ls
    I think the serial numbers in wmi and the api call are the same.
    Did you read the quote I provided? The Win32_LogicalDisk class corresponds to a partition, not a physical drive. The Win32_DiskDrive class corresponds to a disk drive, which may be a hard drive, a CD-ROM drive, a memory card reader, etc. The Win32_PhysicalMedia class corresponds to the media in the disk drive, which may be a hard drive, a CD or DVD, a memory card, etc. If you want to get the manufacturers serial number, rather than the one that Windows assigns when the partition is created, you have to use the Win32_PhysicalMedia class. There is also the Win32_DiskDrivePhysicalMedia class which corresponds to the relationship between the two. It has two properties that correspond to a Win32_DiskDrive object and a Win32_PhysicalMedia object. I found that the best way to use the three together to get information on a hard drive, including serial number, is to use the mgmtclassgen.exe utility to generate .NET classes that correspond to the WMI classes. You can then work with strongly-typed objects with specific properties instead of ManagementObjects with named collection items.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: HOW to get the Hard Disk Serial number

    Isn't there an environment variable with the HDD serial number in it though? Could't you just call that variable using environ()?

  7. #7
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: HOW to get the Hard Disk Serial number

    Quote Originally Posted by tacoman667
    Isn't there an environment variable with the HDD serial
    Don't think so, its not listed in system properties->advanced->environment variables.
    Quote Originally Posted by jmcilhinney
    Did you read the quote I provided?
    Not sure why I missed that...

    So correcting my second post - use Win32_PhysicalMedia instead of Win32_LogicalDisk
    and SerialNumber instead of VolumeSerialNumber

    And for the other other way...

    (visual studio commandline) assuming c:\temp already exists:
    Code:
    C:\>mgmtclassgen.exe Win32_PhysicalMedia /L VB /P c:\temp\PhysicalMedia.vb
    start a new project and add existing item -> c:\temp\PhysicalMedia.vb
    add a reference to system.management

    and then you can loop through the drives and get the info with:
    VB Code:
    1. For Each pm As ROOT.CIMV2.Win32.PhysicalMedia In ROOT.CIMV2.Win32.PhysicalMedia.GetInstances
    2.             Me.label1.Text &= pm.SerialNumber & Environment.NewLine
    3.         Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width