Results 1 to 16 of 16

Thread: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader

  1. #1

    Thread Starter
    Addicted Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    178

    Resolved [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader

    Hi friends,

    I'm building a feature based on wqweto's cZipArchive and for some reason, I have to check for some files to unzip, if the destination path is a HDD/SSD before to write the file.
    I explain, the zip contains files that was on a D:\blablabla folder and the user want to restaure the files on another computer that only have C:\ . On this computur, D:\ is an optical drive, so the files should not be unziped to the orinal path. In this case, with the checking return, the script would unzip files to another defined folder.

    Is it (simply) possible ? I don't know which word to search (I found some parts of codes but they was for VB.NET

    Not asking an entire code but at least a example that I can modifiy to match with what I would do

    Thanks
    Last edited by Couin; Nov 25th, 2022 at 06:24 PM.
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    4,439

    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>

  3. #3
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    3,681

    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.

  4. #4
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    4,439

    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>

  5. #5

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    4,439

    Re: Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader

    Quote Originally Posted by fafalone View Post
    Thumb drives show up as DRIVE_REMOVABLE.
    Not always. Sometimes it just returns DRIVE_FIXED

    cheers,
    </wqw>

  7. #7

    Thread Starter
    Addicted Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    178

    Re: Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader

    Hi friends,

    Thanks a lot.

    I built a small app with both solution and will try it on different machines to compare results between GetDriveType and IsVolRemovable .

    I join the project zip for who search for the same function



    Edit : Added another version that directly list all drives from A to Z
    Attached Files Attached Files
    Last edited by Couin; Nov 24th, 2022 at 06:37 PM.
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  8. #8
    Addicted Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    135

    Resolved 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.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    23,880

    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.

  10. #10
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,112

    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?
    Thanks all!

  11. #11
    Addicted Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    135

    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.

  12. #12
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,112

    Re: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader

    Quote Originally Posted by VanGoghGaming View Post
    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!

  13. #13
    Addicted Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    135

    Cool 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.

  14. #14
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,112

    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
    Thanks all!

  15. #15
    Addicted Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    135

    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.

  16. #16
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,112

    Re: [RESOLVED] Check if a drive letter is a HDD/SSD or a CD/DVD/Card reader

    Quote Originally Posted by VanGoghGaming View Post
    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
  •  



Click Here to Expand Forum to Full Width