Chapter 27: Using the Windows Application Programming Interface
957
RetVal = _
apiGetComputerName(ComputerName, NameSize)
If RetVal > 0 Then
GetComputerName = Left$(ComputerName, NameSize)
Else
GetComputerName = vbNullString
End If
End Function
GetDriveTypeA
From time to time, you may need to know what types of hard disks and optical drives are installed
on a user’s computer. You wouldn’t want to, for instance, try writing to an optical drive on the
user’s computer unless you knew it was capable of read/write operations. The GetDriveTypeA
API function returns a value indicating the type of drive specified by the lpszPath parameter.
Declare Function apiGetDriveType Lib “Kernel32” _
Alias “GetDriveTypeA”( _
ByVal lpszPath As String) As Long
You pass the function the path to the drive you want to test, and GetDriveTypeA returns a long
integer value representing the drive type. The following constants can be used to determine the
drive type:
Const DRIVE_UNKNOWN = 0
Const DRIVE_NOT_AVAILABLE = 1
Const DRIVE_REMOVABLE = 2
Const DRIVE_FIXED = 3
Const DRIVE_REMOTE = 4
Const DRIVE_CDROM = 5
Const DRIVE_RAMDISK = 6
Read/write optical drives (such as a CD-RW or DVD-RW drive) and USB thumb drives are
reported as DRIVE_REMOVABLE.
The following function cycles through each drive possibility and prints its type in the debug win-
dow using the constants listed earlier and GetDriveTypeA:
Function GetAllDriveTypes() As String
Dim DriveInfo As String
Dim PathName As String
Dim intChar As Integer
Dim RetVal As Long
Dim DriveType As String
For intChar = 65 To 90
‘ The backward slash is not required,
‘ but does not interfere with this API call.
‘ The colon is required, however:
PathName = Chr$(intChar) & “:\”