Enumerating WPD devices (Part 1)


The IPortableDeviceManager interface can be used to enumerate WPD devices. The GetDevices method returns a list of device paths for available WPD devices. This device path is typically a PnP path and can be used later to connect to the device.

Error-checking has been omitted in the interests of brevity.

    HRESULT hr = S_OK;
    CComPtr<IPortableDeviceManager> spDevMgr;
    LPTSTR* ppszDeviceListArray = NULL;
    DWORD cdwDevices = 0;

    hr = CoCreateInstance(CLSID_PortableDeviceManager,
                            NULL,
                            CLSCTX_INPROC_SERVER,
                            IID_IPortableDeviceManager,
                            (VOID**) &spDevMgr);

    // Get a count of the devices available by passing
    // a NULL buffer
    hr = spDevMgr->GetDevices(NULL, &cdwDevices);

    // Allocate enough memory for the device path names
    ppszDeviceListArray = new LPTSTR[cdwDevices];

    // Get the list of device paths using the valid buffer
    hr = spDevMgr->GetDevices(ppszDeviceListArray, &cdwDevices);

And that’s it! ppszDeviceListArray now contains the list of available WPD device paths. We can loop through this list to either get some more information about the devices or connect to the device. In the next part, we’ll use the other methods of the IPortableDeviceManager interface to display information about each device.

Comments (5)

  1. dimeby8 says:

    In part 1 we obtained a list of available WPD devices (the device paths to be precise). In this part,…

  2. dimeby8 says:

    This post covers how to connect to a WPD device. We obtained a list of device paths in Enumerating WPD…

  3. dimeby8 says:

    There currently is no managed/C# flavor of the WPD API. This, of course, doesn’t mean that you are locked

  4. dimeby8 says:

    Let’s take a look at how we can enumerate WPD devices in C#. This post assumes that you already have

  5. Csoki says:

    after declaring the variables you should do :

    hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);

    And instead of "CComPtr" yu should write "ComPtr" 😉

    BTW GREAT TUT! 🙂