' Declarations and such needed for the example:in Visual Basic
' (Copy them to the (declarations) section of a module.)
Declare Function GetVolumeInformation Lib "kernel32.dll" Alias "GetVolumeInformationA" (ByVal RPName As String, ByVal VNBuff As String, ByVal VNSize As Long, VSNum As Long, MaxL As Long, Flags As Long, ByVal SysBuffer As String, ByVal SysSize As Long) As Long
' Display the volume label, serial number, and file system name
' of the C: drive. Note how the serial number value is manipulated to
' display it properly.
Dim volname As String ' receives volume name of C:
Dim sn As Long ' receives serial number of C:
Dim snstr As String ' display form of serial number
Dim maxcomplen As Long ' receives maximum component length
Dim sysflags As Long ' receives file system flags
Dim sysname As String ' receives the file system name
Dim retval As Long ' return value
'right$ macro
Macro RightS(s,i)
mid(s,(-i))
end macro
' Initialize string buffers.
volname = Space(256)
sysname = Space(256)
' Get information about the C: drive's volume.
retval = GetVolumeInformation("C:\", volname, Len(volname), sn, maxcomplen, sysflags, sysname, Len(sysname))
' Remove the trailing nulls from the two strings.
volname = Left(volname, InStr(volname, chr(0)) - 1)
sysname = Left(sysname, InStr(sysname, chr(0)) - 1)
' Format the serial number properly.
snstr = LTrim(Hex(sn))
snstr = String(8 - Len(snstr), "0") + snstr
snstr = Left(snstr, 4) & "-" + RightS(snstr, 4)
' Display the volume name, serial number, and file system name.
Print "Volume Name: "+ volname
Print "Serial Number: "+ snstr
Print "File System: "+sysname
$ filename "GetHDDSerial_64.exe"
use rtl64
use minwin
'beware line break after alias
% hiwrd hiword
% lowrd loword
% bycopy
DECLARE FUNCTION GetVolumeInformation Lib "kernel32.dll" Alias "GetVolumeInformationA" (
ByVal lpRootPathName As String,
ByVal lpVolumeNameBuffer As String,
ByVal nVolumeNameSize As Long,
ByRef lpVolumeSerialNumber As Long,
ByRef lpMaximumComponentLength As Long,
ByRef lpFileSystemFlags As Long,
ByVal lpFileSystemNameBuffer As String,
ByVal nFileSystemNameSize As Long) As Long
'==========================================
' Obtain the Hard Drive Serial number
FUNCTION DriveSerialNumber(Drive AS STRING) AS STRING
LOCAL HWord AS DWORD
LOCAL LWord AS DWORD
LOCAL Volname AS STRING
LOCAL VolBuffer AS STRING
LOCAL lpVolumeSerialNumber AS LONG
LOCAL lpMaximumComponentLength AS LONG
LOCAL lpFileSystemFlags AS LONG
LOCAL FileSysBuffer AS STRING
IF GetVolumeInformation (
Drive,
null, 'VolBuffer,
0, 'len(VolBuffer),
lpVolumeSerialNumber,
lpMaximumComponentLength,
lpFileSystemFlags,
null,'FilesysBuffer,
0 'len(FilesysBuffer)
) <> 0 THEN
HWord = HIWRD(lpVolumeSerialNumber)
LWord = LOWRD(lpVolumeSerialNumber)
FUNCTION = RIGHT$("0000000"+HEX$(HWord), 4) + RIGHT$("0000000"+HEX$(LWord), 4)
ELSE
FUNCTION = "Cannot Read Drive.."
END IF
END FUNCTION
'===================================
DIM AS STRING Dsn , dDrive
dDrive = "c:\"
Dsn = DriveSerialNumber(dDrive)
print Dsn
These VB-like translations of the API headers hurt my eyes.
$ filename "GetHDDSerial.exe"
'use rtl64
'use rtl32
use minwin
extern lib "Kernel32.dll"
! GetVolumeInformation "GetVolumeInformationA" '8
end extern
'==========================================
' Obtain the Hard Drive Serial number
FUNCTION DriveSerialNumber(string Drive) AS STRING
dword HWord, LWord
string Volname = space 512, VolBuffer = space 512
long lpVolumeSerialNumber, lpMaximumComponentLength, lpFileSystemFlags
string FileSysBuffer = space 256
if GetVolumeInformation (
Drive,
VolBuffer,
len(VolBuffer),
@lpVolumeSerialNumber,
@lpMaximumComponentLength,
@lpFileSystemFlags,
FilesysBuffer,
len(FilesysBuffer)
) <> 0 then
HWord = HIWoRD(lpVolumeSerialNumber)
LWord = LOWoRD(lpVolumeSerialNumber)
print "lpMaximumComponentLength = " lpMaximumComponentLength
print "lpFileSystemFlags = 0x" hex lpFileSystemFlags
print "VolBuffer = " ltrim(rtrim(VolBuffer))
print "FilesysBuffer = " ltrim(rtrim(FilesysBuffer))
FUNCTION = "0000000" HEX(HWord, 4) "0000000" HEX(LWord, 4)
else
FUNCTION = "Cannot Read Drive.."
end if
END FUNCTION
'===================================
string Dsn , dDrive
dDrive = "c:\"
Dsn = DriveSerialNumber(dDrive)
print "Dsn = " Dsn
/*
BOOL WINAPI GetVolumeInformation(
_In_opt_ LPCTSTR lpRootPathName,
_Out_opt_ LPTSTR lpVolumeNameBuffer,
_In_ DWORD nVolumeNameSize,
_Out_opt_ LPDWORD lpVolumeSerialNumber,
_Out_opt_ LPDWORD lpMaximumComponentLength,
_Out_opt_ LPDWORD lpFileSystemFlags,
_Out_opt_ LPTSTR lpFileSystemNameBuffer,
_In_ DWORD nFileSystemNameSize
);
*/
These VB-like translations of the API headers hurt my eyes.Heh..probably hurt your eyes because is not your translation..
If it was prototyped like this we would not need the AddressOf operator??
! GetVolumeInformation Lib "kernel32.dll" Alias "GetVolumeInformationA"( char*,char*,long,long*,long*,long*,char*,long)