Creating WPD PROPVARIANTs in C# without using interop

Previous posts have covered how to create, manage and marshal PROPVARIANTs using interop. Here's a way on how to create a PROPVARIANT without interop.

The IPortableDeviceValues interface exposes a SetStringValue method that allows a regular C# string to be added into the collection. IPortableDeviceValues also exposes a GetValue method which lets us retrieve any added property value as a PROPVARIANT. Armed with these two facts, it's pretty obvious how we can manufacture a string PROPVARIANT.

 static void StringToPropVariant(string value,
        out PortableDeviceApiLib.tag_inner_PROPVARIANT propvarValue)
{
    // We'll use an IPortableDeviceValues object to transform the
    // string into a PROPVARIANT
    PortableDeviceApiLib.IPortableDeviceValues pValues =
        (PortableDeviceApiLib.IPortableDeviceValues)
            new PortableDeviceTypesLib.PortableDeviceValuesClass();

    // We insert the string value into the IPortableDeviceValues object
    // using the SetStringValue method
    pValues.SetStringValue(ref PortableDevicePKeys.WPD_OBJECT_ID, value);

    // We then extract the string into a PROPVARIANT by using the 
    // GetValue method
    pValues.GetValue(ref PortableDevicePKeys.WPD_OBJECT_ID, 
                                out propvarValue);
}

We create an instance of an IPortableDeviceValues object named pValues. We then call SetStringValue with a dummy property key and the string to be converted. pValues now contains exactly one item - a string value which is internally held as a PROPVARIANT. To extract the internally held PROPVARIANT, we call GetValue which returns us a PROPVARIANT value.

This example function can be extended for other PROPVARIANT types. Use Object Browser and take a look at the Set* methods exposed by IPortableDeviceValues. Follow the same paradigm - use Set*Value for your source target type and then use GetValue to extract it as a PROPVARIANT.

Comments

  • Anonymous
    January 08, 2007
    This is a C# follow-up post on the earlier C++ playlist creation post . Be sure to read the earlier post

  • Anonymous
    June 25, 2008
    When I try to use this, the vt member gets filled in correctly, but the value does not get set. According to the debugger, the only other fileds are:    public struct tag_inner_PROPVARIANT    {        public __MIDL___MIDL_itf_PortableDeviceApi_0001_0000_0001 __MIDL____MIDL_itf_PortableDeviceApi_0001_00000001;        public ushort vt;        public byte wReserved1;        public byte wReserved2;        public uint wReserved3; wReserved1-3 and they are all 0. Right now I am only dealing with UInt32 data. Also, is there an easy way to get the data back out of the PROPVARIANT structure? Thanks, Chris

  • Anonymous
    June 26, 2008
    Well, after re-reading some of the previous posts, I figured this method was indeed working even though I can't see the data in teh PROPVARIANT structure. I implemented the PropVariantToUInt32() counterparts to the ToPropVariant helper functions using the GetUnsignedIntegerValue method to extract the value from the PROPVARIANT. I thus confirmed that the above process works nicely in both directions. Sorry I didn'tget this before. Thank you so much for all of your posts. There is no way I could be successful without them. Chris