我正在制作PortableDeviceLib的分支,它与WPD/MTP设备上的存储服务一起工作。偶然发现了一个问题。我想重命名一个设备上的文件对象,所以首先想到的是改变对象的WPD_OBJECT_ORIGINAL_FILE_NAME
属性。但是这样做会得到一个异常。如果我更改WPD_OBJECT_NAME
,名称会更改,但文件名与预期相同,并且没有例外。
问题是,如何在WPD设备上重命名对象?为什么我得到异常更改文件名属性?
我想重命名的原因是,当将文件复制到设备时,如果文件已经存在,它会获得扩展名为dup0
, dup1
等的副本
这是一个老的响应,但是…
下面是我用来在WPD设备上重命名对象的代码:
__declspec(dllexport) HRESULT __cdecl supportsCommand( CComPtr<IPortableDevice>& device, PROPERTYKEY theKey ) {
HRESULT err = S_OK;
CComPtr<IPortableDeviceCapabilities> capabilities;
CComPtr<IPortableDeviceKeyCollection> commands;
uint numCommands = 0;
device->Capabilities( &capabilities );
err = capabilities->GetSupportedCommands( &commands );
err = commands->GetCount( &numCommands );
PROPERTYKEY key = WPD_PROPERTY_NULL;
for( uint index = 0; index < numCommands; index++) {
err = commands->GetAt( index, &key );
if( IsEqualPropertyKey(theKey, key) )
return true;
}
return false;
}
__declspec(dllexport) HRESULT __cdecl renameObject( CComPtr<IPortableDevice>& device, LPWCSTR objectId, LPWCSTR newName ) {
HRESULT err = S_OK;
if (supportsCommand(device, WPD_COMMAND_OBJECT_PROPERTIES_SET) == false)
return (HRESULT)-1;
CComPtr<IPortableDeviceValues> properties, values, results;
err = CoCreateInstance( CLSID_PortableDeviceValues, NULL, CLSCTX_INPROC_SERVER, IID_IPortableDeviceValues, (VOID**) &properties );
err = CoCreateInstance( CLSID_PortableDeviceValues, NULL, CLSCTX_INPROC_SERVER, IID_IPortableDeviceValues, (VOID**) &values );<br>
// Mount the command.
err = properties->SetGuidValue( WPD_PROPERTY_COMMON_COMMAND_CATEGORY
, WPD_COMMAND_OBJECT_PROPERTIES_SET.fmtid );
err = properties->SetUnsignedIntegerValue( WPD_PROPERTY_COMMON_COMMAND_ID
, WPD_COMMAND_OBJECT_PROPERTIES_SET.pid );<br>
// Set the values
err = properties->SetStringValue( WPD_PROPERTY_OBJECT_PROPERTIES_OBJECT_ID, objectId );
err = values->SetStringValue( WPD_OBJECT_ORIGINAL_FILE_NAME, newName );
err = properties->SetIPortableDeviceValuesValue( WPD_PROPERTY_OBJECT_PROPERTIES_PROPERTY_VALUES, values );
err = device->SendCommand( 0, properties, &results );<br>
// Show the results
uint count = 0;
results->GetCount( &count );
PROPERTYKEY key;
PROPVARIANT var;
for( uint i = 0; i < count; i++ ) {
results->GetAt( i, &key, &var );
// ...show key and var...
}
return err;
}