|
-
Dec 7th, 2005, 05:14 AM
#1
Thread Starter
Hyperactive Member
HOW to get the Hard Disk Serial number
Does anyone knows why this code is not working?
VB Code:
Public Class Form1
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
Public Function GetSerial(ByVal str As String) As Long
Dim Buf$, Name$, Flags&, Length&
Dim Serial As Long
GetVolumeInformation(str, Buf$, 255, Serial, Length, Flags, Name$, 255)
GetSerial = Serial
End Function
Private Sub Form_Load()
Label1.Text = CStr(GetSerial("C:\"))
'This prints the hard drive serial number of C: here. No other drive Is needed
End Sub
End Class
-
Dec 7th, 2005, 05:45 AM
#2
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.
-
Dec 7th, 2005, 07:31 AM
#3
Hyperactive Member
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:
' Imports System.Runtime.InteropServices
' Imports System.Text
<DllImport("kernel32", SetLastError:=True)> _
Public Shared Function GetVolumeInformation _
(ByVal lpRootPathName As String, _
ByVal lpVolumeName As StringBuilder, _
ByVal nVolumeNameSize As Integer, _
ByRef lpVolumeSerialNumber As Integer, _
ByRef lpMaximumComponentLength As Integer, _
ByRef lpFileSystemFlags As fsflags, _
ByVal lpFileSystemName As StringBuilder, _
ByVal nFileSystemNameSize As Integer) As Integer
End Function
<DllImport("kernel32")> _
Public Shared Function GetLastError() As Integer
End Function
Public Function GetSerial(ByVal str As String) As String
Dim volumeNameLength As Integer = 256
Dim volumeName As New StringBuilder(volumeNameLength)
Dim volumeSerialNumber As Integer
Dim maxCompLen As Integer
Dim fileSystemFlags As fsflags
Dim filesystemnamelength As Integer = 256
Dim fileSystemName As New StringBuilder(filesystemnamelength)
Dim ret As Boolean
ret = GetVolumeInformation(str, volumeName, volumeNameLength, _
volumeSerialNumber, maxCompLen, fileSystemFlags, fileSystemName, filesystemnamelength)
If ret = 0 Then
Me.Text = GetLastError()
End If
Me.Label1.Text = "Volume Name: " & volumeName.ToString & Environment.NewLine
Me.Label1.Text &= "Volume Serial Number: " & hex(volumeSerialNumber) & Environment.NewLine
Me.Label1.Text &= "FileSystem Name: " & fileSystemName.ToString & Environment.NewLine
Me.Label1.Text &= "Filesystem Flags: " & fileSystemFlags.ToString
Return volumeSerialNumber.ToString
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GetSerial("C:\")
Me.Label1.Dock = Dock.Fill
End Sub
<FlagsAttribute()> _
Public Enum fsflags
FILE_NAMED_STREAMS = &H40000
FILE_READ_ONLY_VOLUME = &H80000
FILE_SUPPORTS_OBJECT_IDS = &H10000
FILE_SUPPORTS_REPARSE_POINTS = &H80
FILE_SUPPORTS_SPARSE_FILES = &H40
FILE_VOLUME_QUOTAS = &H20
FS_CASE_IS_PRESERVED = 2
FS_CASE_SENSITIVE = 1
FS_FILE_COMPRESSION = &H10
FS_FILE_ENCRYPTION = &H20000
FS_PERSISTENT_ACLS = 8
FS_UNICODE_STORED_ON_DISK = 4
FS_VOL_IS_COMPRESSED = &H8000
End Enum
Last edited by jo0ls; Dec 7th, 2005 at 08:05 AM.
-
Dec 7th, 2005, 08:06 AM
#4
Hyperactive Member
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:
' imports system.management
' code based on MS example
Dim query As New SelectQuery("Win32_LogicalDisk")
'ManagementObjectSearcher retrieves a collection of WMI objects based on
' the query.
Dim search As New ManagementObjectSearcher(query)
' Display each entry for Win32_bios
Dim info As ManagementObject
For Each info In search.Get()
If Not info("VolumeSerialNumber") Is Nothing Then
label1.Text &= info("VolumeSerialNumber").ToString & Environment.NewLine
End If
Next
Both serial numbers match the one you get with a dir command at the command prompt.
-
Dec 7th, 2005, 08:22 AM
#5
Re: HOW to get the Hard Disk Serial number
 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.
-
Dec 7th, 2005, 08:58 AM
#6
Hyperactive Member
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()?
-
Dec 7th, 2005, 10:01 AM
#7
Hyperactive Member
Re: HOW to get the Hard Disk Serial number
 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.
 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:
For Each pm As ROOT.CIMV2.Win32.PhysicalMedia In ROOT.CIMV2.Win32.PhysicalMedia.GetInstances
Me.label1.Text &= pm.SerialNumber & Environment.NewLine
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|