Partager via


Connecting to a WPD device in C#

This is the C# equivalent of an earlier C++ post on connecting to a WPD device. We'll concentrate on the implementation here - you may refer back to that earlier post for further explanation.

 // Create our client information collection
PortableDeviceApiLib.IPortableDeviceValues pValues = 
        (PortableDeviceApiLib.IPortableDeviceValues)
            new PortableDeviceTypesLib.PortableDeviceValuesClass();

// We have to provide at the least our name, version, revision
pValues.SetStringValue(
        ref PortableDevicePKeys.WPD_CLIENT_NAME, "Sample Client");
pValues.SetUnsignedIntegerValue(
        ref PortableDevicePKeys.WPD_CLIENT_MAJOR_VERSION, 1);
pValues.SetUnsignedIntegerValue(
        ref PortableDevicePKeys.WPD_CLIENT_MINOR_VERSION, 0);
pValues.SetUnsignedIntegerValue(
        ref PortableDevicePKeys.WPD_CLIENT_REVISION, 2);

// Create a new IPortableDevice instance
PortableDeviceApiLib.PortableDeviceClass pPortableDevice = 
        new PortableDeviceApiLib.PortableDeviceClass();

// We are now ready to open a connection to the device
// We'll assume deviceID contains a valid WPD device path and connect to it
pPortableDevice.Open(deviceID, pValues);

As you can see, it's pretty straight-forward to open a connection to a WPD device. We will set up our client information in a PortableDeviceValues collection and then send in the collection to the Open API call. If the device ID was valid, then a device connection will be opened and we can begin to use pPortableDevice to communicate with the device.

Comments

  • Anonymous
    December 06, 2008
    Am completely new to PortableDeviceAPI. I was trying the above code snippet and Visual Studio refused to accept "PortableDevicePKeys". Am I missing any reference, or declaration? The datatype for any of the set functions is not PortableDevicePKeys. Can you please help me on this?

  • Anonymous
    December 16, 2010
    I have the same issue as Jairam, were you able to solve this? I've been stuck at this for a while now

  • Anonymous
    April 03, 2015
    Hi friends, You must read the entire blog if you wants to know how to generate a PortableDevicePKeys. Please refer to article Where are the WPD property keys in C#? Best Regards.