3

I have been trying to build a file explorer of my Android phone content that would be optimized to my needs. As FindFirstFile api could not work for MTP devices I had to learn Windows Portable Device Api.

I met alot of obstacles but got most things working:

  • enum devices and files
  • get file properties
  • rename file
  • create folder
  • copyDeviceFileToPc
  • copyPcFileToDevice

My only road block now is deleting files. I followed the MSDN sample perfectly but still met into problems. The problem is when I try to delete a file, the file gets deleted but the device hangs and becomes unresponsive. Not even Windows explorer is able to access it at that point. I have to disconnect and reconnect the device. However windows explorer is able to delete files on the device no problem.

This is my code

typedef const wchar_t *string_t;

bool wpd::Device::deleteFile(string_t file_id)
{
    PROPVARIANT file ={};
    IPortableDevicePropVariantCollection *filesToDelete = newIPortableDevicePropVariantCollection(); // Calls CoCreateInstance
    IPortableDeviceContent *devContentMgr;

    file.vt = VT_LPWSTR;
    file.pwszVal = (LPWSTR)file_id;
    filesToDelete->Add(&file);

    // IPortableDevice *dev initialized in constructor
    dev->Content(&devContentMgr);

    HRESULT hr = devContentMgr->Delete(PORTABLE_DEVICE_DELETE_NO_RECURSION,filesToDelete,NULL);

    filesToDelete->Release();
    devContentMgr->Release();

    return SUCCEEDED(hr);
}

Note that I am doing this on Windows Xp.

user13947194
  • 337
  • 5
  • 7
  • 1
    Hello! Did you manage to solve the problem? I have the same thing happening in the .NET Framework. And in C++ I've tried doing. – Snowy Owl Jul 13 '21 at 12:15
  • UPD After several experiments, I found that the more files on the device, the more freezing after deletion. If there are about 2000 files on the device, the freeze may take about 10 seconds. – Snowy Owl Jul 13 '21 at 17:42
  • @SnowyOwl I didn't manage to solve my problem. And infact, My Device hangs upon running my delete function regardless of the number of files in the current directory. – user13947194 Jul 14 '21 at 13:35
  • I was referring to the number of files in the current partition (like phone memory or SD card) and not in the working folder. That is, if there are many files in the phone's memory, then after the first deletion the program freezes. – Snowy Owl Jul 15 '21 at 14:34
  • @SnowyOwl Ok. But why does Windows Explorer not have this problem of deleting files from the device. – user13947194 Jul 16 '21 at 21:25
  • I don’t know this :( It is necessary to look in detail at what is the difference between calls through the COM object and through the Explorer. But I’m afraid it will take quite a lot of time to figure it out. – Snowy Owl Jul 17 '21 at 13:03
  • @Snowy Owl so you are guessing probably Windows Explorer uses a different MTP library than the one offered to us by the Windows SDK? – user13947194 Jul 18 '21 at 16:45
  • 1
    I can not tell. I do not have enough knowledge to understand what and how it uses from the insides of the Explorer. But judging by the hang, it still uses the same COM object. But apparently with some kind of additions that allow him to effectively remove. – Snowy Owl Jul 19 '21 at 10:27
  • 1
    Anyone got a solution yet? I was thinking maybe using IPortableDevice sendCommand method to attempt to fix it, not sure how I to do that though – hms11 May 05 '23 at 14:41

0 Answers0