Welcome to MSDN Blogs Sign in | Join | Help

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.

Published Thursday, December 21, 2006 3:51 PM by dimeby8

Comments

# re: Connecting to a WPD device in C#

Saturday, December 06, 2008 12:46 PM by Jairam

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 comments are disabled
 
Page view tracker