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.