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.

Comments (3)
  1. dimeby8 says:

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

  2. dimeby8 says:

    If you followed the exercise from our first C# post , you must have noticed that the PortableDeviceApi

  3. kiquenet says:

    The one line of Power-shell script below un-mounts a USB cable attached Windows Portable Device (WPD) from the Windows Operating System (XP thru W8/2012)

    And just in case you have not yet started playing with Powershell, here is the equivalent VBScript (maybe be can port to C#):

    Set objWMIService = GetObject ("winmgmts:\.rootcimv2")

    Set colItems = objWMIService.ExecQuery ("Select * from Win32ext_WPD Where strFriendlyName = 'SAMSUNG-SGH-I747'")

    For Each objItem in colItems

    Set objWMIWPDStatic = objWMIService.Get("Win32ext_WPD")

    Set objInParam = objWMIWPDStatic.Methods_("EjectDevice").inParameters.SpawnInstance_()

    objInParam.Properties_.Item("strObjectDeviceId") =  objItem.strId

    Set objOutParams = objWMIService.ExecMethod("Win32ext_WPD", "EjectDevice", objInParam)

    Exit For


    Note change ‘SAMSUNG-SGH-I747′ to the phone/tablet name you see in Windows Explorer

    About Win32ext_WPD

    "Select * from Win32ext_WPD Where strFriendlyName = 'SAMSUNG-SGH-I747'"

    Not well document googleing, not found more 2 references.

    Maybe port to C# using:

    var oScope = new ManagementScope(@"\" + MachineName + @"rootcimv2");



    Any final solution with full source code sample application ?

    IMHO, better samples for minimize learning curve are real applications with full source code and good patterns.

Comments are closed.

Skip to main content