Let's take a look at how we can enumerate WPD devices in C#. This post assumes that you already have a project set up using these instructions. (Update: You will also need to follow the disassembly/reassembly instructions below for this to work correctly.)
static void Main(string args)
// Get an instance of the device manager
= new PortableDeviceApiLib.PortableDeviceManagerClass();
// Probe for number of devices
uint cDevices = 1;
devMgr.GetDevices(null, ref cDevices);
// Re-allocate if needed
if (cDevices > 0)
string deviceIDs = new string[cDevices];
devMgr.GetDevices(deviceIDs, ref cDevices);
for (int ndxDevices = 0; ndxDevices < cDevices; ndxDevices++)
ndxDevices + 1, deviceIDs[ndxDevices]);
Console.WriteLine("No WPD devices are present!");
This code maps almost line-by-line to the C++ enumeration example. We manufacture an instance of the device manager using PortableDeviceApiLib.PortableDeviceManagerClass. We then use the instance to probe how many devices are there. If you remember, the GetDevices API will return back in cDevices the number of devices (actually the size of the string array that should be specified to retrieve all the device IDs).
Once we know the number of devices, we allocate an appropriately sized array and then call GetDevices again. This time, the array will contain all the device IDs. To display them, we simply iterate over the array and print them out.
Mike R. brought to my notice that the above sample only enumerates one device even if more than one are connected. This is a marshalling restriction - we can work around it by manually fixing up the generated Interop assembly. Follow the steps below to edit the assembly:
You can now rebuild your project. You can now first call GetDevices with a NULL parameter to get the count of devices and then call it again with an array to get the device IDs.