Solved

HD Serial Number

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…
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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now