-
Nov 23rd, 2022, 11:41 PM
#1
Thread Starter
Addicted Member
-
Nov 24th, 2022, 01:06 AM
#2
Re: Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
Check out this IsVolRemovable function:
Code:
Option Explicit
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileW" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, ByVal lpOverlapped As Long) As Long
Private Type STORAGE_HOTPLUG_INFO
Size As Long
MediaRemovable As Byte
MediaHotplug As Byte
DeviceHotplug As Byte
WriteCacheEnableOverride As Byte
End Type
Public Function IsVolRemovable(DrvLetter As String) As VbTriState
Const IOCTL_STORAGE_GET_HOTPLUG_INFO As Long = &H2D0C14
Const INVALID_HANDLE_VALUE As Long = -1
Const OPEN_EXISTING As Long = 3
Const FILE_SHARE_READ As Long = &H1
Const FILE_SHARE_WRITE As Long = &H2
Dim hVol As Long
Dim lSize As Long
Dim uInfo As STORAGE_HOTPLUG_INFO
hVol = CreateFile(StrPtr("\\.\" & Left$(DrvLetter, 1) & ":"), 0, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, ByVal 0)
If hVol <> INVALID_HANDLE_VALUE Then
Call DeviceIoControl(hVol, IOCTL_STORAGE_GET_HOTPLUG_INFO, ByVal 0, 0, uInfo, LenB(uInfo), lSize, 0)
Call CloseHandle(hVol)
IsVolRemovable = (uInfo.MediaRemovable + uInfo.DeviceHotplug > 0)
Else
IsVolRemovable = vbUseDefault
End If
End Function
Private Sub Form_Load()
Debug.Print IsVolRemovable("C:")
Debug.Print IsVolRemovable("K:")
End Sub
cheers,
</wqw>
-
Nov 24th, 2022, 03:03 AM
#3
Re: Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
Can probably get away with the much simpler GetDriveType:
Code:
Public Enum DriveTypes
DRIVE_UNKNOWN = 0
DRIVE_NO_ROOT_DIR = 1
DRIVE_REMOVABLE = 2
DRIVE_FIXED = 3
DRIVE_REMOTE = 4
DRIVE_CDROM = 5 'can be a CD or a DVD
DRIVE_RAMDISK = 6
End Enum
Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal lpRootPathName As String) As Long
DRIVE_FIXED is your regular HDD/SSD.
-
Nov 24th, 2022, 03:17 AM
#4
Re: Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
Yes, the simple GetDriveType API call correctly returns DRIVE_CDROM for optical drives.
Apparently the IsVolRemovable above tries to recognize USB thumb drives too. I have no other explanation why it's done so overcomplicated.
cheers,
</wqw>
-
Nov 24th, 2022, 03:29 AM
#5
Re: Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
Thumb drives show up as DRIVE_REMOVABLE.
-
Nov 24th, 2022, 04:52 AM
#6
Re: Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
 Originally Posted by fafalone
Thumb drives show up as DRIVE_REMOVABLE.
Not always. Sometimes it just returns DRIVE_FIXED
cheers,
</wqw>
-
Nov 24th, 2022, 05:15 PM
#7
Thread Starter
Addicted Member
-
Dec 1st, 2022, 11:13 PM
#8
Addicted Member
Re: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
Code:
Private Function CheckDriveType(Optional sDriveLetter As String = "C:") As String
Dim objWMIServices As SWbemServices, objLogicalDisk As SWbemObject, objDiskPartition As SWbemObject, objDiskDrive As SWbemObject
Set objWMIServices = GetObject("winmgmts:")
For Each objLogicalDisk In objWMIServices.InstancesOf("Win32_LogicalDisk")
If objLogicalDisk.Name = UCase(sDriveLetter) Then Exit For
Next objLogicalDisk
For Each objDiskPartition In objLogicalDisk.Associators_("Win32_LogicalDiskToPartition")
Exit For
Next objDiskPartition
For Each objDiskDrive In objDiskPartition.Associators_("Win32_DiskDriveToDiskPartition")
Exit For
Next objDiskDrive
CheckDriveType = objDiskDrive.MediaType
End Function
I don't have a CDROM drive anymore but I whipped up this piece of code in a jiffy and it correctly identifies all internal SSDs as "Fixed hard disk media" and all USB sticks as "Removable Media".
You need to set a reference to "Microsoft WMI Scripting V1.2 Library" to run this code. Hope this helps.
-
Dec 4th, 2022, 02:21 PM
#9
Re: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
Of course WMI might either be disabled on the user's machine or secured against non-admin user access. It is not meant to be used in applications, it's an admin scripting tool.
-
Dec 6th, 2022, 07:25 PM
#10
Frenzied Member
Re: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
Is there a method of differentiating between an HDD an an SSD?
Also, perhaps a method of reading into Vb6 code the name and/or specs of the drive?
-
Dec 7th, 2022, 06:49 AM
#11
Addicted Member
Re: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
WMI can do all of that. For example you can check if the drive has "TRIM" enabled and then it'll be an SSD for sure.
-
Dec 7th, 2022, 02:54 PM
#12
Frenzied Member
Re: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
 Originally Posted by VanGoghGaming
WMI can do all of that. For example you can check if the drive has "TRIM" enabled and then it'll be an SSD for sure.
Could you post some code for this beginner. Say to list all the drives on a machine. I've already got your earlier code, but have split the long line.
Code:
'Disk Identifier
'Code from forum:VanGoghGaming
'Project Reference: Microsoft WMI Scripting V1.2 Library
Option ExplicitPrivate Function CheckDriveType(Optional sDriveLetter As String = "C:") As String
Dim objWMIServices As SWbemServices, objLogicalDisk As SWbemObject, _
objDiskPartition As SWbemObject, objDiskDrive As SWbemObject
Set objWMIServices = GetObject("winmgmts:")
For Each objLogicalDisk In objWMIServices.InstancesOf("Win32_LogicalDisk")
If objLogicalDisk.Name = UCase(sDriveLetter) Then Exit For
Next objLogicalDisk
For Each objDiskPartition In objLogicalDisk.Associators_("Win32_LogicalDiskToPartition")
Exit For
Next objDiskPartition
For Each objDiskDrive In objDiskPartition.Associators_("Win32_DiskDriveToDiskPartition")
Exit For
Next objDiskDrive
CheckDriveType = objDiskDrive.MediaType
End Function
Private Sub Form_Load()
End Sub
It's just how to show the results. I suspect it may be trivial.
Last edited by el84; Dec 7th, 2022 at 03:16 PM.
Thanks all!
-
Dec 7th, 2022, 04:34 PM
#13
Addicted Member
Re: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
Code:
Option Explicit
Private Function PartitionHasLogicalDisks(objDiskPartition As SWbemObject, objLogicalDisk As SWbemObject) As Boolean
Dim objLogicalDisks As SWbemObjectSet
Set objLogicalDisks = objDiskPartition.Associators_("Win32_LogicalDiskToPartition")
If objLogicalDisks.Count > 0 Then
For Each objLogicalDisk In objLogicalDisks: Exit For: Next objLogicalDisk: PartitionHasLogicalDisks = True
End If
End Function
Private Function CheckAllDrives() As String
Dim objWMIServices As SWbemServices, objLogicalDisk As SWbemObject, objDiskPartition As SWbemObject, objDiskDrive As SWbemObject, sMediaTypes As String
Set objWMIServices = GetObject("winmgmts:")
For Each objDiskDrive In objWMIServices.InstancesOf("Win32_DiskDrive")
For Each objDiskPartition In objDiskDrive.Associators_("Win32_DiskDriveToDiskPartition")
If PartitionHasLogicalDisks(objDiskPartition, objLogicalDisk) Then
sMediaTypes = sMediaTypes & objLogicalDisk.Name & " - " & objDiskDrive.MediaType & vbNewLine
End If
Next objDiskPartition
Next objDiskDrive
CheckAllDrives = sMediaTypes
End Function
Private Sub Form_Load()
MsgBox CheckAllDrives
Unload Me
End Sub
This will list all drives on your computer along with their respective media type.
-
Dec 7th, 2022, 05:53 PM
#14
Frenzied Member
Re: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
Many thanks for that last code
This is the result that I got
Attachment 186389
I do have a CD drive --not mentioned.
Also I am interested in any other info on the disk, like a way of being sure of whether a given disk is an SDD, or even the capacity and amount of space used.
I am not asking for a whole rewrite.
I'd be interested in what must be changed or added
-
Dec 7th, 2022, 09:43 PM
#15
Addicted Member
Re: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
Although your attachment isn't working, I think you need to have a disc loaded into the CDROM if you want it to show up in that list. Also the method for checking whether a given disk is an SSD, is not something trivial. WMI is a vast subject and you have a lot of googling to do if you want to learn more about it.
-
Dec 8th, 2022, 05:58 AM
#16
Frenzied Member
Re: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader
 Originally Posted by VanGoghGaming
Although your attachment isn't working, I think you need to have a disc loaded into the CDROM if you want it to show up in that list...
I've tried that without success.
But funnily enough, a standard drivelistbox control shows it on my desktop, whether or not there is a CD in that drive!
Last edited by el84; Dec 8th, 2022 at 11:17 PM.
Thanks all!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|