how can i get serial numper of hard disk using api

  • Thread starter Thread starter Guest
  • Start date Start date
This is an automated conversion of a C# routine I've used...

<DllImport("kernel32.dll")> _
Private Shared Function GetVolumeInformation(ByVal PathName As String, ByVal
VolumeNameBuffer As StringBuilder, ByVal VolumeNameSize As UInt32, ByRef
VolumeSerialNumber As UInt32, ByRef MaximumComponentLength As UInt32, ByRef
FileSystemFlags As UInt32, ByVal FileSystemNameBuffer As StringBuilder,
ByVal FileSystemNameSize As UInt32) As Long
End Function

Friend Function GetVolumeSerial(ByVal strDriveLetter As String) As String
Dim serNum As System.UInt32 = 0
Dim maxCompLen As System.UInt32 = 0
Dim VolLabel As StringBuilder = New StringBuilder(256)
Dim VolFlags As UInt32 = New UInt32
Dim FSName As StringBuilder = New StringBuilder(256)
strDriveLetter += ":\"
Dim Ret As Long = GetVolumeInformation(strDriveLetter, VolLabel,
CType(VolLabel.Capacity, UInt32), serNum, maxCompLen, VolFlags, FSName,
CType(FSName.Capacity, UInt32))
Return Convert.ToString(serNum)
End Function

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Why do you want to use API to get the HDD Serial Number?

The VB.NET way to do it is:

Add a reference to System.Management DLL & then use this code:

Imports System.Management

Public Function GetVolumeSerialNumber(ByVal driveletter As String) As String
Dim mobjSearcher As New ManagementObjectSearcher _
("SELECT * FROM Win32_LogicalDisk WHERE Name = '" & driveletter & ":'")
For Each obj As ManagementObject In mobjSearcher.Get
Return obj("VolumeSerialNumber")
Next
End Function

Useage:

MessageBox.Show(GetVolumeSerialNumber("C:\"))

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

If you want to use API then you can convert this old VB 6 function:

'HDD Serial No.
Public Declare Function GetVolumeSerialNumber 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 VolumeSerialNumber(ByVal strRootPath As String) As String

Dim strVolLabel As String
Dim lngVolSize As Long
Dim lngSerial As Long
Dim lngMaxLen As Long
Dim lngFlags As Long
Dim strName As String
Dim lngNameSize As Long
Dim strSerial As String

If GetVolumeSerialNumber(strRootPath, strVolLabel, lngVolSize,
lngSerial, lngMaxLen, lngFlags, strName, lngNameSize) Then
'Create an 8 character string
strSerial = Format(Hex(lngSerial), "00000000")
'Adds the '-' between the first 4 characters and the last 4 characters
VolumeSerialNumber = Left(strSerial, 4) + "-" + Right(strSerial, 4)
Else
'If the call to API function fails the function returns a zero
serial number
VolumeSerialNumber = "0000-0000"
End If

End Function

I hope this helps
 
Unless something has changed, those calls are not returning the hard drive
serial number, instead they return the soft serial number.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top