Get USB device by GUID and drive letter
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Get USB device by GUID and drive letter

  1. #1
    Join Date
    Sep 2010
    Posts
    3

    Get USB device by GUID and drive letter

    Hi all I have a tricky one here..
    I have some code I'm using to interact with USB devices (windows driver kit). I'm targeting "USB MASS Storage" and hopefully "USB Composite Device" basicaly I want any device that has a drive letter where i can read/write from.

    - basicaly i need to know the drive letter of the usb device
    - the code works for some devices, USB mass storage device having a GUID of
    const string GUID_DEVINTERFACE_DISK = "53f56307-b6bf-11d0-94f2-00a0c91efb8b";
    devices are thumb drives, removable HD's things that are class "USB MASS Storage"

    however a USB composite device has a different GUID of
    const string TEST_GUID = "36fc9e60-c465-11cf-8056-444553540000"; and the code fails to return the device.
    devices are Phones... class of "USB Composite Device"

    so i changed the GUID and ran the code and it did not find any device using the GUID for composite device... so it must be checking something else as well... here is the method that gets a device by its drive letter... please help me out in getting it to work with compisite devices as well.

    or at least point show me the general area what needs to be changed.

    im fairly new to c# and i dont understand this low level stuff....

    thanks...

    this method accepts a drive letter and attempts to find a device matching the drive letter.
    seems like it will work right, phones have drive letter.. but no it looks at GUID etc..

    --- code ---

    // usb mass storage device
    const string GUID_DEVINTERFACE_DISK = "53f56307-b6bf-11d0-94f2-00a0c91efb8b";
    //
    // Find a device based upon a Drive Letter
    //
    static public USBDevice FindDriveLetter(string DriveLetter)
    {
    USBDevice FoundDevice = null;
    string InstanceID = "";

    // We start by getting the unique DeviceNumber of the given
    // DriveLetter. We'll use this later to find a matching
    // DevicePath "symbolic name"
    int DevNum = GetDeviceNumber(@"\\.\" + DriveLetter.TrimEnd('\\'));
    if (DevNum < 0)
    {
    return FoundDevice;
    }

    Guid DiskGUID = new Guid(GUID_DEVINTERFACE_DISK);

    // We start at the "root" of the device tree and look for all
    // devices that match the interface GUID of a disk
    IntPtr h = SetupDiGetClassDevs(ref DiskGUID, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    if (h.ToInt32() != INVALID_HANDLE_VALUE)
    {
    bool Success = true;
    int i = 0;
    do
    {
    // create a Device Interface Data structure
    SP_DEVICE_INTERFACE_DATA dia = new SP_DEVICE_INTERFACE_DATA();
    dia.cbSize = Marshal.SizeOf(dia);

    // start the enumeration
    Success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref DiskGUID, i, ref dia);
    if (Success)
    {
    // build a DevInfo Data structure
    SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
    da.cbSize = Marshal.SizeOf(da);

    // build a Device Interface Detail Data structure
    SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA();
    didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // trust me

    // now we can get some more detailed information
    int nRequiredSize = 0;
    int nBytes = BUFFER_SIZE;
    if (SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, ref nRequiredSize, ref da))
    {
    // Now that we have a DevicePath... we can use it to
    // generate another DeviceNumber to see if it matches
    // the one we're looking for.
    if (GetDeviceNumber(didd.DevicePath) == DevNum)
    {
    // current InstanceID is at the "USBSTOR" level, so we
    // need up "move up" one level to get to the "USB" level
    IntPtr ptrPrevious;
    CM_Get_Parent(out ptrPrevious, da.DevInst, 0);

    // Now we get the InstanceID of the USB level device
    IntPtr ptrInstanceBuf = Marshal.AllocHGlobal(nBytes);
    CM_Get_Device_ID(ptrPrevious, ptrInstanceBuf, nBytes, 0);
    InstanceID = Marshal.PtrToStringAuto(ptrInstanceBuf);

    Marshal.FreeHGlobal(ptrInstanceBuf);
    break;
    }
    }
    }
    i++;
    } while (Success);
    SetupDiDestroyDeviceInfoList(h);
    }

    // Did we find an InterfaceID of a USB device?
    if (InstanceID.StartsWith("USB\\"))
    {
    FoundDevice = FindDeviceByInstanceID(InstanceID);
    }
    return FoundDevice;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Get USB device by GUID and drive letter

    I think HannesTheGreat did some code in the past. You might try searching or send him a PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,286

    Re: Get USB device by GUID and drive letter

    You might find this code helpful (it's in C++).

    http://www.codeguru.com/forum/showthread.php?t=499595

  4. #4
    Join Date
    Sep 2010
    Posts
    3

    Re: Get USB device by GUID and drive letter

    Quote Originally Posted by dglienna View Post
    I think HannesTheGreat did some code in the past. You might try searching or send him a PM.
    thanks.. I have sent email to HannesTheGreat...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center