Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

HD Serial Number

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

808 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