PC Review


Reply
Thread Tools Rate Thread

how can i get serial numper of hard disk using api

 
 
=?Utf-8?B?YWJlZXIgZWwgYXNoa2Vy?=
Guest
Posts: n/a
 
      9th Mar 2005
how can i get serial numper of hard disk using api
 
Reply With Quote
 
 
 
 
Bob Powell [MVP]
Guest
Posts: n/a
 
      9th Mar 2005
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.





"abeer el ashker" <(E-Mail Removed)> wrote in message
news:7AB8C6AB-4CAE-49DB-AC17-(E-Mail Removed)...
> how can i get serial numper of hard disk using api



 
Reply With Quote
 
=?Utf-8?B?Q3JvdWNoaWUxOTk4?=
Guest
Posts: n/a
 
      9th Mar 2005
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
 
Reply With Quote
 
Howard Kaikow
Guest
Posts: n/a
 
      9th Mar 2005
Unless something has changed, those calls are not returning the hard drive
serial number, instead they return the soft serial number.

--
http://www.standards.com/; See Howard Kaikow's web site.
"Crouchie1998" <(E-Mail Removed)> wrote in message
news:ECBF205A-9AD2-40F2-9A13-(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard Disk Serial Number Adele le Roux Microsoft VB .NET 7 23rd Mar 2007 07:28 AM
hard disk serial number Ahmed Essa Microsoft ASP .NET 1 17th Nov 2005 05:34 PM
hard disk serial number Ahmed Essa Microsoft Dot NET 0 17th Nov 2005 01:04 PM
Getting the Hard Disk serial number Moira Microsoft Access VBA Modules 5 28th Oct 2005 01:07 PM
Re: How to get the hard disk serial number under Win XP? Michel Walsh Microsoft Access Form Coding 2 25th Aug 2003 02:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:09 PM.