C# and the WPD API

There currently is no managed/C# flavor of the WPD API. This, of course, doesn't mean that you are locked out from using the WPD API if you want to use C#. Since the API is a set of COM interfaces, we simply have to interop across from C#. Granted it isn't as easy as a native C# implementation, but it gets the job done.

This and future posts assume that you are using Visual Studio. I used 2005, but other versions should work fine.

To start, create a new C# Console Application project. Next select the "Project>Add Reference ..." menu option. Click the "COM" tab in the "Add Reference" dialog and make sure the following items are selected and then click OK:
PortableDeviceApi 1.0 Type Library
PortableDeviceTypes 1.0 Type Library

Once the references have been added, you can use the Object Browser (Ctrl+Alt+J) to check out the interesting stuff the interop brought in. You'll see two new nodes - Interop.PortableDeviceApiLib and Interop.PortableDeviceTypesLib. Under these nodes, you will find PortableDeviceApiLib and PortableDeviceTypesLib. We will need to use the latter names to reference other objects. As an exercise, spend some time comparing the objects and methods exposed through interop versus those available in WPD API through C++.

In our next post, we'll re-visit the C++ device enumeration example but with a C# twist.