Solved

HD Serial Number

Posted on 1998-08-05
5
282 Views
Last Modified: 2010-05-03
Can anyone tell me how to find out a Hard Disks Serial number using VB5, Thanks.
0
Comment
Question by:peat042998
  • 3
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
waty earned 40 total points
ID: 1467932
Private Const MAX_FILENAME_LEN = 256

Private Declare Function GetVolumeInformation& Lib "Kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal pVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long)


Public Function GetSerialNumber(sDrive As String) As Long
   Dim ser As Long
   Dim S As String * MAX_FILENAME_LEN
   Dim s2 As String * MAX_FILENAME_LEN
   Dim i As Long
   Dim j As Long
   
   Call GetVolumeInformation(sDrive + ":\" & Chr$(0), S, MAX_FILENAME_LEN, ser, i, j, s2, MAX_FILENAME_LEN)
   GetSerialNumber = ser
End Function

0
 
LVL 6

Expert Comment

by:clifABB
ID: 1467933
And my answer was (which waty beat me to):

Place the following code in the declarations section of a bas module:
Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Const OFS_MAXPATHNAME = 128
Public Const OF_READ = &H0
Type OFSTRUCT
        cBytes As Byte
        fFixedDisk As Byte
        nErrCode As Integer
        Reserved1 As Integer
        Reserved2 As Integer
        szPathName(OFS_MAXPATHNAME) As Byte
End Type
Type FileTime
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type
Type BY_HANDLE_FILE_INFORMATION
        dwFileAttributes As Long
        ftCreationTime As FileTime
        ftLastAccessTime As FileTime
        ftLastWriteTime As FileTime
        dwVolumeSerialNumber As Long
        nFileSizeHigh As Long
        nFileSizeLow As Long
        nNumberOfLinks As Long
        nFileIndexHigh As Long
        nFileIndexLow As Long
End Type

The following code will retrieve the volume serial number:
Private Sub Command1_Click()
    Dim lpReOpenBuff As OFSTRUCT
    Dim FileHandle As Long
    Dim FileInfo As BY_HANDLE_FILE_INFORMATION
    Dim Ret As Long
   
    FileHandle = OpenFile("c:\autoexec.bat", lpReOpenBuff, OF_READ)
    Ret = GetFileInformationByHandle(FileHandle, FileInfo)
    Print "Volume Serial Number is: " & FileInfo.dwVolumeSerialNumber
    Ret = CloseHandle(FileHandle)
End Sub

Note:
1. You must specify a file ("c:\autoexec.bat" in this example)
2. The volume serial number will be returned as a decimal.  You will have to convert it to a hex$ to make it look like what appears when to do a 'dir' in DOS
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1467934
I only posted it, cause I hated to have to do that much work for nothing.  :)
0
 
LVL 14

Expert Comment

by:waty
ID: 1467935
Sorry ClifABB, the way you post it is very interesting for finding other informations about files.
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1467936
waty:
I took it from MSDN online and trimmed the stuff that wasn't needed.  I like yours better, you don't have to specify a file.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now