将内容从设备传输到电脑
WPD 应用程序完成的一个常见操作是将内容从连接的设备传输到电脑。
内容传输是使用下表中所述的接口完成的。
接口 | 说明 |
---|---|
IPortableDeviceContent 接口 | 提供对 IPortableDeviceProperties 接口的访问。 |
IPortableDeviceProperties 接口 | 提供对特定于属性的方法的访问。 |
IPortableDeviceResources 接口 | 用于存储给定配置文件的属性键。 |
IStream 接口 | 用于读取和写入数据。 |
TransferContentFromDevice
示例应用程序的 ContentTransfer.cpp 模块中的 函数演示应用程序如何将联系人信息从连接的设备传输到电脑。
函数完成 TransferContentFromDevice
的第一个任务是提示用户在设备上输入父对象的对象标识符, (将基于该对象) 传输内容。
HRESULT hr = S_OK;
WCHAR szSelection[81] = {0};
CComPtr<IPortableDeviceContent> pContent;
CComPtr<IPortableDeviceResources> pResources;
CComPtr<IPortableDeviceProperties> pProperties;
CComPtr<IStream> pObjectDataStream;
CComPtr<IStream> pFinalFileStream;
DWORD cbOptimalTransferSize = 0;
CAtlStringW strOriginalFileName;
if (pDevice == NULL)
{
printf("! A NULL IPortableDevice interface pointer was received\n");
return;
}
// Prompt user to enter an object identifier on the device to transfer.
printf("Enter the identifer of the object you wish to transfer.\n>");
hr = StringCbGetsW(szSelection,sizeof(szSelection));
if (FAILED(hr))
{
printf("An invalid object identifier was specified, aborting content transfer\n");
}
下一步是检索示例用于访问特定于内容的方法的 IPortableDeviceContent 对象。
if (SUCCEEDED(hr))
{
hr = pDevice->Content(&pContent);
if (FAILED(hr))
{
printf("! Failed to get IPortableDeviceContent from IPortableDevice, hr = 0x%lx\n",hr);
}
}
下一步是检索 IPortableDeviceResources 对象,示例使用该对象来访问特定于资源的方法。
if (SUCCEEDED(hr))
{
hr = pContent->Transfer(&pResources);
if (FAILED(hr))
{
printf("! Failed to get IPortableDeviceResources from IPortableDeviceContent, hr = 0x%lx\n",hr);
}
}
下一步是检索 IStream 对象,示例使用该对象读取它从设备传输的数据。
if (SUCCEEDED(hr))
{
hr = pResources->GetStream(szSelection, // Identifier of the object we want to transfer
WPD_RESOURCE_DEFAULT, // We are transferring the default resource (which is the entire object's data)
STGM_READ, // Opening a stream in READ mode, because we are reading data from the device.
&cbOptimalTransferSize, // Driver supplied optimal transfer size
&pObjectDataStream);
if (FAILED(hr))
{
printf("! Failed to get IStream (representing object data on the device) from IPortableDeviceResources, hr = 0x%lx\n",hr);
}
}
下一步是在设备上检索对象的文件名。 此字符串用于在电脑上创建相应的文件名。 如果对象在设备上没有文件名,则对象的标识符将转换为字符串并用于创建目标文件名。
if (SUCCEEDED(hr))
{
hr = pContent->Properties(&pProperties);
if (SUCCEEDED(hr))
{
hr = GetStringValue(pProperties,
szSelection,
WPD_OBJECT_ORIGINAL_FILE_NAME,
strOriginalFileName);
if (FAILED(hr))
{
printf("! Failed to read WPD_OBJECT_ORIGINAL_FILE_NAME on object '%ws', hr = 0x%lx\n", szSelection, hr);
strOriginalFileName.Format(L"%ws.data", szSelection);
printf("* Creating a filename '%ws' as a default.\n", (PWSTR)strOriginalFileName.GetString());
// Set the HRESULT to S_OK, so we can continue with our newly generated
// temporary file name.
hr = S_OK;
}
}
else
{
printf("! Failed to get IPortableDeviceProperties from IPortableDeviceContent, hr = 0x%lx\n", hr);
}
}
在此之后,示例将创建一个目标 IStream 对象。
if (SUCCEEDED(hr))
{
hr = SHCreateStreamOnFile(strOriginalFileName, STGM_CREATE|STGM_WRITE, &pFinalFileStream);
if (FAILED(hr))
{
printf("! Failed to create a temporary file named (%ws) to transfer object (%ws), hr = 0x%lx\n",(PWSTR)strOriginalFileName.GetString(), szSelection, hr);
}
}
最后,将源 IStream 对象复制到电脑上的目标。
if (SUCCEEDED(hr))
{
DWORD cbTotalBytesWritten = 0;
// Since we have IStream-compatible interfaces, call our helper function
// that copies the contents of a source stream into a destination stream.
hr = StreamCopy(pFinalFileStream, // Destination (The Final File to transfer to)
pObjectDataStream, // Source (The Object's data to transfer from)
cbOptimalTransferSize, // The driver specified optimal transfer buffer size
&cbTotalBytesWritten); // The total number of bytes transferred from device to the finished file
if (FAILED(hr))
{
printf("! Failed to transfer object from device, hr = 0x%lx\n",hr);
}
else
{
printf("* Transferred object '%ws' to '%ws'.\n", szSelection, (PWSTR)strOriginalFileName.GetString());
}
}
相关主题