Errors when using Apis WPD (Windows Portable Device) to transfer image files

I am trying to write an example application in C # using ApD WPD to transfer image files to a connected device that supports WPD. I followed IT . My problem is that every time I try to transfer a file, I get an error all the time: the value does not fall into the expected range. Has anyone tried to do the same successfully. Any pointers are highly appreciated.

Below is a snippet of code where I encounter an 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, how I set the parameters. I think that something is wrong with the parameters I set, or I miss some of the necessary parameters.

 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); 
+4
source share
4 answers

I’ve been working on the same mistake for a long time and I think I might have found it. In my case, the problem was setting the code WPD_PARENT_OBJECT_ID

In most examples, I saw that each set the object identifier as follows:

 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 fact, they literally want a folder identifier that 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 configured my values ​​this way, the target stream had no problems writing to the device.

For additional information about the object only, the object identifier 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 ); 

Anyway, I hope this helps!

+2
source

It will be useful if you can give more detailed information, for example, about which line of code contains this error, the manufacturer / model of the WPD device (s) you tried with, etc.

This error is quite common and may mean that the parameters are not formatted correctly, or that you give out-of-range parameters when calling CreateObjectWithPropertiesAndData. If so, it will help show the values ​​of the parameters that you pass to the device.

+2
source

Palacio Valencia answer works!

You can download the source code of the example (for transferring the file to the portable device using WPDApi) from here ( https://dl.dropboxusercontent.com/u/40603470/WPDTransferToDevice.zip ). Just remember to use the folder id (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(); 
0
source

I have been looking for a complete solution for a long time, which it is.

First I want to give Christoph Gear a really big hand. He is the only one I discovered that had any example of transferring data to a phone using WPD (Windows portable device) in c #.

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

The result is a set of PortableDevice files that can be used to perform most of the necessary CRUD operations.

Check out my github site for code and more details:

https://github.com/pstorli/WPDFileTransfer

-1
source

Source: https://habr.com/ru/post/1433354/


All Articles