gpt4 book ai didi

c# - 使用 WPD (Windows Portable Device) Apis 传输图像文件时出错

转载 作者:行者123 更新时间:2023-11-30 12:13:04 26 4
gpt4 key购买 nike

我一直在尝试使用 WPD Apis 在 c# 中编写示例应用程序,以将图像文件传输到连接的 WPD 支持设备。我一直在关注THIS关联。我的问题是,每次我尝试传输文件时,我都会收到错误消息:值不在预期范围内。有没有人试过成功地做同样的事情。非常感谢任何指点。

下面是我遇到错误的代码片段

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);
}

错误出现在 targetStream.Write(...下面是我如何设置参数。我认为我设置的参数有问题,或者我缺少一些必需的参数。

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);

最佳答案

我已经研究了同样的错误很长一段时间了,我想我可能已经找到了。就我而言,问题出在代码设置 WPD_PARENT_OBJECT_ID

在大多数例子中,我看到每个人都这样设置对象 id:

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);

但实际上,他们确实想要文件夹的id,这与文件夹路径完全不同。

//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 ;

当我以这种方式设置我的值时,目标流写入设备没有问题。

只是为了一些额外的背景信息,对象 ID 来自 IPortableDeviceContent 变量

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 );

无论如何,我希望这对您有所帮助!

关于c# - 使用 WPD (Windows Portable Device) Apis 传输图像文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12351294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com