这是一个旧的响应,但是......
下面是我用来重命名 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;
}