473,472 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how can i get serial numper of hard disk using api

how can i get serial numper of hard disk using api
Nov 21 '05 #1
3 9971
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" <ab***********@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
how can i get serial numper of hard disk using api

Nov 21 '05 #2
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
Nov 21 '05 #3
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" <Cr**********@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
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

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
15
by: tom | last post by:
Hi, How do I get the serial number of the harddisk in .NET? I want this to be the same number even if the user has reformatted, so I do not want the volume serial number. Thanx, t
79
by: Klaus Bonadt | last post by:
In order to protect software from being copied without licence, I would like to use something like a key, which fits only to the current system. The serial number of the CPU or the current...
14
by: Lauren Wilson | last post by:
Discovered this interesting comment on MSDN: "To programmatically obtain the hard disk's serial number that the manufacturer assigns, use the Windows Management Instrumentation (WMI)...
9
by: Nebojsa4 | last post by:
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...
6
by: Paul Bromley | last post by:
Ok - I have given up on trying to find the active IP address for a given PC. For licensing purposes I need to retrive a unique identifier from the PC that the program is installed on. The Hard disk...
7
by: Adele le Roux | last post by:
Hi All, How can I get the hard disk serial number of a remote computer's C:? The drive will NOT be mapped as a network drive. Thanks, Adele
0
by: preethaAjayan | last post by:
Could anybody please help me with a piece of code to get hard disk's serial number using C#, not volume serial number, actual number that manufactures give to hard disks.
6
by: Matthew Connor | last post by:
Hi all! I'm sure many of you rolled your eyes at the subject trying to recall how many times youv'e heard this question. :) But bear with because I THINK my question is at least slightly different...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.