0

I am making a fork of PortableDeviceLib which works with Storage service on a WPD/MTP device. And stumbled upon a problem. I want to rename a file object on a device, so the first thought is to change WPD_OBJECT_ORIGINAL_FILE_NAME property of an object. But i get an exception doing this. If I change WPD_OBJECT_NAME, the name changes, but the filename is the same as expected, and there is no exception.

So the question is, how to rename an object on WPD device? Why do I get exception changing filename property?

The reason, I want to rename is, when copying files to device, if the file already exists, it gets a duplicate with extension dup0, dup1, etc.

naixx
  • 1,176
  • 11
  • 17

1 Answers1

1

It's an old response, but...
Below is the code I use to rename an object on WPD device:

__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;
}
  • this is only a partial answer, but combine it with https://github.com/notpod/Notpod2 and https://github.com/derekwilson/PodcastUtilities and after some digging I found a solution – Janeks Bergs Dec 13 '16 at 20:06