0

我正在制作一个PortableDeviceLib的分支,它与 WPD/MTP 设备上的存储服务一起使用。并偶然发现了一个问题。我想重命名设备上的文件对象,所以首先想到的是更改对象的属性。但我这样做是个例外。如果我更改,名称会更改,但文件名与预期相同,也不例外。WPD_OBJECT_ORIGINAL_FILE_NAMEWPD_OBJECT_NAME

所以问题是,如何重命名 WPD 设备上的对象?为什么我得到异常更改文件名属性?

我想重命名的原因是,当将文件复制到设备时,如果文件已经存在,它会得到一个扩展名dup0,dup1等的副本。

4

1 回答 1

1

这是一个旧的响应,但是......
下面是我用来重命名 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;
}
于 2013-06-20T18:21:07.550 回答