Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

HD Serial Number

Posted on 1998-08-05
5
Medium Priority
?
334 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 80 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

916 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