CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2008
    Posts
    28

    Using WMI to get USB device drive letter

    Hi all.
    i am using WMI to retrieve the drive letter. Before that i need to retrieve the device ID for the USB devices present .
    now i have been able to fetch the device ID, not the PNP device id , but the device id which is got by queryingfrom WIN32_USBHub.
    now using this device ID which i received from WIN32_USBHub, i have to retrieve the drive letter. i.e i have to fetch the drive letter from WMI corresponding to the DeviceID which i have with me. bit i am not able to get any perfect mapping for this .

    please can i get some help on how to get the drive letter of a USB device from the DeviceID.

    Thanks .

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Using WMI to get USB device drive letter

    Looks like you have the HUB but not the device. You can iterate through the devices connected to a hub IIRC...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Using WMI to get USB device drive letter

    >> not the PNP device id, but the device id ... now using this device ID ... i have to retrieve the drive letter.
    Win32_USBHub.DeviceId is the same as Win32_USBHub.PNPDeviceId. This PNP Id isn't the same as the PNP Id of the logical disk. Here's how you can determine which connected USB devices correspond to what logical drives:

    Code:
    SELECT * FROM Win32_USBControllerDevice
    For Each as USBDevice
       ASSOCIATORS OF {Win32_PnPEntity.DeviceID="<USBDevice.Dependent>"}
        WHERE ResultClass = Win32_DiskDrive
       For Each as Drive
          ASSOCIATORS OF {Win32_DiskDrive.DeviceID="<Drive.DeviceID>"} 
           WHERE AssocClass = Win32_DiskDriveToDiskPartition
          For Each as Drive2Part
             ASSOCIATORS OF {Win32_DiskPartition.DeviceID="<Drive2Part.DeviceID>"} 
              WHERE AssocClass = Win32_LogicalDiskToPartition
             For Each as Disk2Part
                Win32_LogicalDisk = <Disk2Part.Dependent>
             End For
          End For      
       End For
    End For
    gg

  4. #4
    Join Date
    Dec 2008
    Posts
    1

    Re: Using WMI to get USB device drive letter

    I'm pretty new at this, how do I actually run the code below? Do I use Visual Studio, and if so how do I create the project?

    Quote Originally Posted by Codeplug View Post
    >> not the PNP device id, but the device id ... now using this device ID ... i have to retrieve the drive letter.
    Win32_USBHub.DeviceId is the same as Win32_USBHub.PNPDeviceId. This PNP Id isn't the same as the PNP Id of the logical disk. Here's how you can determine which connected USB devices correspond to what logical drives:

    Code:
    SELECT * FROM Win32_USBControllerDevice
    For Each as USBDevice
       ASSOCIATORS OF {Win32_PnPEntity.DeviceID="<USBDevice.Dependent>"}
        WHERE ResultClass = Win32_DiskDrive
       For Each as Drive
          ASSOCIATORS OF {Win32_DiskDrive.DeviceID="<Drive.DeviceID>"} 
           WHERE AssocClass = Win32_DiskDriveToDiskPartition
          For Each as Drive2Part
             ASSOCIATORS OF {Win32_DiskPartition.DeviceID="<Drive2Part.DeviceID>"} 
              WHERE AssocClass = Win32_LogicalDiskToPartition
             For Each as Disk2Part
                Win32_LogicalDisk = <Disk2Part.Dependent>
             End For
          End For      
       End For
    End For
    gg

  5. #5
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Using WMI to get USB device drive letter

    >> how do I actually run the code below?
    Well, you don't. It's pseudo-code. Most of it is actually taken from a VBS script sample on MSDN:
    http://msdn.microsoft.com/en-us/libr...92(VS.85).aspx
    See the last "How do I..." example.

    >> Do I use Visual Studio
    For scripting, all you really need is notepad.

    gg

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Using WMI to get USB device drive letter

    Quote Originally Posted by Codeplug;1797249>> [b
    Do I use Visual Studio[/b]
    For scripting, all you really need is notepad.

    REAL programmers use:
    Code:
    C:\>copy con file.sav
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

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

Featured