11

In my project, I'm using the Windows Portable Device (WPD) API to enumerate the contents of a mobile device. WPD API Enumeration Guide. I'm able to enumerate over each object and view their properties as shown in the API programming guide. WPD API Properties Guide

However when I try to get an object's name that has a . within the name, the returned value is truncated at that .

HRESULT hr = objectProperties->GetStringValue(WPD_OBJECT_NAME, &strOriginalFileName);
if(FAILED(hr))
    return false;

PWSTR wideStr = strOriginalFileName;
char buffer[20];
wcstombs(buffer, wideStr, 20);

qDebug() << buffer;

So for example, an object (folder on the device) with the name of com.example is returned as com. This becomes an obvious issue when I'm trying to locate a specific filepath on the device.

I can't seem to figure out what's wrong. Am I misunderstanding how the filename actually is? Is example another property or something within the com object? I'm very confused.

EDIT: So I used the WPD API sample software to retrieve all the object properties of the com.example object and you can see that WPD itself cannot get the full folder name. enter image description here

Thanks for your time!

mrg95
  • 1,999
  • 5
  • 34
  • 70

1 Answers1

5

The WPD Application Programming Reference refers following 3 NAMEs.

WPD_OBJECT_HINT_LOCATION_DISPLAY_NAME: A friendlier name, mostly intended for display

WPD_OBJECT_NAME: The name of the object on device.

WPD_OBJECT_ORIGINAL_FILE_NAME: The original filename of the object on device.

The MS code sample in C++ uses WPD_OBJECT_ORIGINAL_FILE_NAME to get to the actual file name (underneath the object) while transferring files from device to PC.

I modified the MS code sample (to enumerate object properties) and it showed me the actual file name (nothing truncated from the filename com.ef1.first.second)

Here is the image

I used:

    Windows Windows 7 Ultimate (without SP1)
    Visual Studio 2013
    Android 4.4.4 (Moto-E)
    Connection type: MTP
    Memory type: Internal Memory as well as External (SD Card)

I wouldn't be surprised if it doesn't work on some combination of Windows versions, Windows SDK versions, android versions, Connection types (MTP, PTP, USB Mass Storage).


Here is the part of code that I modified (and that is how it worked).

// Reads properties for the user specified object.
void ReadContentProperties(_In_ IPortableDevice* device)
{
   //.... Edited for brevity
   tempHr = propertiesToRead->Add(WPD_OBJECT_NAME);
   if (FAILED(tempHr))
   {
      wprintf(L"! Failed to add WPD_OBJECT_NAME to IPortableDeviceKeyCollection, hr= 0x%lx\n", tempHr);
   }

   // Here is the added code
   tempHr = propertiesToRead->Add(WPD_OBJECT_ORIGINAL_FILE_NAME);
   if (FAILED(tempHr))
   {
      wprintf(L"! Failed to add WPD_OBJECT_ORIGINAL_FILE_NAME to IPortableDeviceKeyCollection, hr= 0x%lx\n", tempHr);
   }
    //.... Edited for brevity
}
Community
  • 1
  • 1
blackpen
  • 2,119
  • 10
  • 15
  • WPD_OBJECT_ORIGINAL_FILE_NAME is not a property of the object. The attached screenshot shows ALL of the associated properties to the object. I've been trying to get that property applied for days. It's on some files, but not others. (I believe). Any time I try to grab WPD_OBJECT_ORIGINAL_FILE_NAME the hr comes back as failed. – mrg95 Nov 06 '16 at 00:01
  • I had considered `WPD_OBJECT_ORIGINAL_FILE_NAME` as mandatory. May I know what file system your device is using? That may hint some more relevant information. – blackpen Nov 06 '16 at 01:21
  • Just an android phone. OnePlus One to be exact, but I'm gonna need my software to support a variety of different phones/tablets. – mrg95 Nov 06 '16 at 05:48
  • I don't understand why your answer is being upvoted (no offense whatsoever). It's just not correct though. I've known about WPD_OBJECT_ORIGINAL_FILE_NAME from the very beginning and it's always come back as failed. Some followup would be nice :) – mrg95 Nov 24 '16 at 04:06
  • @mc360pro, It is just that every example I came across has used this technique (including MS docs). But, I wouldn't be surprised if it didn't work for a certain combination of OS (version, update, patches), language, SDK version, portable device (manufacturer, OS, upgrades, file system type). I will experiment with few android device (with Win7) and let you know. – blackpen Nov 24 '16 at 07:52
  • Thanks. Yeah I'm using android on Win 7 as well. Thanks for your help :) – mrg95 Nov 24 '16 at 17:02
  • Sorry @mc360pro, I ran out of resources trying to install huge downloads for dotNet ... and then Windows SDK for C++ ... (both of which seem to need Win7 service pack 1). If you have time, you could try to read extended attributes on the folders like mentioned [here](http://stackoverflow.com/questions/35917595/accessing-mtp-vendor-extended-properties-through-wpd). – blackpen Nov 24 '16 at 22:57
  • I installed Visual Studio 2013 on Win7. I am running the WPD sample code to list all the objects on the device (android). I will get back to you once I am able to create, enumerate a folder with desired properties. – blackpen Nov 28 '16 at 04:44
  • I was able to create a file on the external sd card with name "com.ef1.first.second" and list all it's properties (by modifying the sample code to print the WPD_OBJECT_ORIGINAL_FILE_NAME). I will add a snapshot. – blackpen Nov 28 '16 at 05:01
  • Hmmm, so if it comes down to the devices used, how come the folder shows up normally when browsing the device using Windows Explorer? Windows got the full name somehow. I would assume that the name could still be retrieved even though the original name property is not set. Note that the actual folder name is com.mojang, so try installing Minecraft PE on the device and see if it works there... – mrg95 Nov 28 '16 at 21:36
  • You could try to reproduce the problem on a external memory card (mounted sd card). If you can't reproduce it, then there must be something different with your file system (on internal memory) that windows fails with. – blackpen Dec 03 '16 at 18:56
  • But Windows doesnt fail because it's fine on the file explorer built in to Windows. It's getting the full name somehow. – mrg95 Dec 04 '16 at 00:33
  • Is WPD able to get full file names for files which were created using Android UI (and have dots in them)? – blackpen Dec 04 '16 at 00:38
  • Also could you add your modified code to the question? – blackpen Dec 04 '16 at 00:39
  • I hope there aren't any issues with strange characters/letters with file names (such as control characters). – blackpen Dec 04 '16 at 00:40
  • Just created another folder with a . manually. Still can't grab it. Then I tried getting the original file name using the code from my example (just changed the property key) and hr failed. In fact, it failed for every object on the device. – mrg95 Dec 04 '16 at 04:46
  • Here is every name I got when using OBJECT_NAME https://gyazo.com/063894fda62496277f53820b2dc09993. You can see com, then minecraft stuff, then another com, which is the one i made manually. Yet here they are in windows explorer https://gyazo.com/bb9d893a5c99071ad56edca0d6e3e74d – mrg95 Dec 04 '16 at 04:50
  • Okay correction. It didn't fail for every object, only the first two. But I let my enumeration skip over it and display each value from each object. One sec..... OH! IT GOT THE NAMES! Wow I feel like an idiot. The issue was that it couldn't grab the original names of parent entries which caused the enumeration to quit. Sorry for carrying this on for so long. You were a huge help!!!!!! – mrg95 Dec 04 '16 at 05:00
  • Here was the output. So OBJECT_NAME does truncate at '.' because it thinks everything after is a file extension https://gyazo.com/82ad1184b71a92c116a4a8e672508b1d – mrg95 Dec 04 '16 at 05:01
  • So my full solution is to read both object name and original file name. Then if original has failed, use the regular object name for path checks. – mrg95 Dec 04 '16 at 05:08
  • 1
    **1)** The *object's name* got truncated at *first dot* (Except when the object's name starts with a dot). **2)** The *original name* always had full value (except for root level object's such as *SD Card* where it gave **). **3)** The object's name and original name may not always be same (object's name may be *New Folder* when original may be *com.example*) **4)** With MTP, the windows file explorer doesn't seem to display *empty folders* till you actually put files in them (immaterial of whether they have a dot in their object's name or original file name or not). – blackpen Dec 04 '16 at 10:46
  • I will keep my code setup for couple of months. If you get into any strange problems that you need to replicate, beep me. – blackpen Dec 04 '16 at 10:50
  • Number 4 isn't true on my end. Mine shows empty folders – mrg95 Dec 04 '16 at 20:10
  • I tested it twice. It is still quite possible that I may have missed something there. – blackpen Dec 04 '16 at 20:17