共用方式為


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

  • Anonymous
    September 27, 2006
    In part 1 we obtained a list of available WPD devices (the device paths to be precise). In this part,...

  • Anonymous
    September 27, 2006
    This post covers how to connect to a WPD device. We obtained a list of device paths in Enumerating WPD...

  • Anonymous
    December 05, 2006
    There currently is no managed/C# flavor of the WPD API. This, of course, doesn't mean that you are locked

  • Anonymous
    December 05, 2006
    Let's take a look at how we can enumerate WPD devices in C#. This post assumes that you already have

  • Anonymous
    May 31, 2015
    after declaring the variables you should do : hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); And instead of "CComPtr" yu should write "ComPtr" ;) BTW GREAT TUT! :)