Errors while using WPD (Windows Portable Device) Apis for transferring image files

StackOverflow https://stackoverflow.com/questions/12351294

  •  01-07-2021
  •  | 
  •  

Question

I have been trying to write a sample application in c# using WPD Apis for transferring image files to a connected WPD supported device. I have been following THIS link. My problem is that everytime i try and transfer a file i keep getting the error: Value does not fall within expected range. Has anyone tried doing the same successfully. Any pointers are highly appreciated.

Below is the code snippet where i face the error

IPortableDeviceContent content;
        this._device.Content(out content);

        IPortableDeviceValues values = 
            GetRequiredPropertiesForContentType(fileName, parentObjectId);

        PortableDeviceApiLib.IStream tempStream;
        uint optimalTransferSizeBytes = 0;
        content.CreateObjectWithPropertiesAndData(
            values,
            out tempStream,
            ref optimalTransferSizeBytes,
            null);           

        System.Runtime.InteropServices.ComTypes.IStream targetStream = 
            (System.Runtime.InteropServices.ComTypes.IStream) tempStream;
        try
        {
            using (var sourceStream = 
                new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                var buffer = new byte[optimalTransferSizeBytes];
                int bytesRead;
                do
                {
                    bytesRead = sourceStream.Read(
                        buffer, 0, (int)optimalTransferSizeBytes);
                    IntPtr pcbWritten = IntPtr.Zero;
                    targetStream.Write(
                        buffer, (int)optimalTransferSizeBytes, pcbWritten);
                } while (bytesRead > 0);
            }
            targetStream.Commit(0);
        }
        finally
        {
            Marshal.ReleaseComObject(tempStream);
        }

The error appears in the line targetStream.Write(... And below is how i have set the parameters. I think there is something wrong with the parameters that i am setting or i am missing some required params.

IPortableDeviceValues values = 
            new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;

        var WPD_OBJECT_PARENT_ID = new _tagpropertykey();
        WPD_OBJECT_PARENT_ID.fmtid = 
            new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
                     0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
        WPD_OBJECT_PARENT_ID.pid = 3 ;
        values.SetStringValue(ref WPD_OBJECT_PARENT_ID, parentObjectId);

        FileInfo fileInfo = new FileInfo(fileName);
        var WPD_OBJECT_SIZE = new _tagpropertykey();
        WPD_OBJECT_SIZE.fmtid = 
            new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
                     0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
        WPD_OBJECT_SIZE.pid = 11;            
        values.SetUnsignedLargeIntegerValue(WPD_OBJECT_SIZE, (ulong) fileInfo.Length);

        var WPD_OBJECT_ORIGINAL_FILE_NAME = new _tagpropertykey();
        WPD_OBJECT_ORIGINAL_FILE_NAME.fmtid = 
            new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
                     0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
        WPD_OBJECT_ORIGINAL_FILE_NAME.pid = 12;
        values.SetStringValue(WPD_OBJECT_ORIGINAL_FILE_NAME, Path.GetFileName(fileName));

        var WPD_OBJECT_NAME = new _tagpropertykey();
        WPD_OBJECT_NAME.fmtid = 
            new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
                     0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
        WPD_OBJECT_NAME.pid = 4;
        values.SetStringValue(WPD_OBJECT_NAME, Path.GetFileName(fileName));

        var WPD_OBJECT_FORMAT = new _tagpropertykey();
        WPD_OBJECT_FORMAT.fmtid = new Guid(0xef2107d5, 0xa52a, 0x4243, 0xa2, 0x6b, 0x62, 0xd4, 0x17, 0x6d, 0x76, 0x03);
        WPD_OBJECT_FORMAT.pid = 6;
        values.SetGuidValue(WPD_OBJECT_FORMAT, WPD_OBJECT_FORMAT.fmtid);
Was it helpful?

Solution

I have been working on this same error for quite a while now and I think I might have found it. In my case, the problem was in the code setting WPD_PARENT_OBJECT_ID

In most examples, I saw that everyone was setting the object id like this:

string parentObjectId = "InternalStorage/SomeFolder/Parent";

var WPD_OBJECT_PARENT_ID = new _tagpropertykey();
WPD_OBJECT_PARENT_ID.fmtid = 
    new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
             0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
WPD_OBJECT_PARENT_ID.pid = 3 ;
values.SetStringValue(ref WPD_OBJECT_PARENT_ID, parentObjectId);

But in actuality, they literally want the id of the folder, which is completely different from the folder path.

//The object id depends on what the device assigns to the folder
string parentObjectId = "o6AC"; //Just as an example from my device

var WPD_OBJECT_PARENT_ID = new _tagpropertykey();
WPD_OBJECT_PARENT_ID.fmtid = 
    new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 
             0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
WPD_OBJECT_PARENT_ID.pid = 3 ;

When I set up my values in this way, the target stream had no problem writing to the device.

Just for some extra background info, the object id comes from the IPortableDeviceContent variable

IPortableDeviceContent content;
Device.Content( out content ); //Device should be set elseware

IEnumPortableDeviceObjectIDs objectIds;
content.EnumObjects( 0, parent.Id, null, out objectIds );

//objectIds is a collection of all the objects
uint fetched; //lets us know if an object was grabbed or not

//This is the actual Object Id of that specific folder on the device
//Populated by the ObjectIds.Next function
string ActualObjectId;

objectIds.Next( 1, out ActualObjectId, ref fetched );

Anyways I hope this helps!

OTHER TIPS

It will be helpful if you can give further details, such as which line(s) of code is giving this error, the manufacturer/model of the WPD device(s) you were trying with, etc.

This error is fairly generic and could mean that the parameters are not formatted correctly, or that you're giving out of range parameters when calling CreateObjectWithPropertiesAndData. If this is the case, then it will help to show the values of the parameters you are passing into the device.

Vance Palacio's answer works!

You can download the source code of the example (to transfer a file to a portable device using WPDApi) from here (https://dl.dropboxusercontent.com/u/40603470/WPDTransferToDevice.zip). Just remember to use the ID of the folder (e.g o6AC) instead of the full path.

var devices = new PortableDeviceCollection();
devices.Refresh();
var kindle = devices.First();
kindle.Connect();

kindle.TransferContentToDevice(@"d:\temp\Kindle_Users_Guide.azw", "06AC");

kindle.Disconnect();

I searched for a long time to come up with a complete solution, which this is.

I first want to give Christophe Geer a really big hand. He is the only person that I have found that had any example of transferring data to a phone using WPD (Windows Portable device) in c#.

There were only a few bugs that I had to fix and only a few important features that were missing that I added, such as file size and copying folders.

The result are a set of PortableDevice files that can be used to do most of the needed CRUD operations.

Check out my github site for the code and more details:

https://github.com/pstorli/WPDFileTransfer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top