Solved

HD Serial Number

Posted on 1998-08-05
5
292 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

773 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