WinAPI calls to access USB storage which has no drive letter?
Asked Answered
S

3

8

I've noticed, that some USB storage devices don't register in Windows as regular drives, in that they don't even get assigned a drive letter. Therefore, I can't apparently access them using the standard file operations C API.

What are the WinAPI calls I can use to do some normal file operations on those strange devices - so:

  • find/enumerate those devices at all,
  • browse the file/directory tree on the device and get some file stats (size, modification date, etc.),
  • read/write contents of the files,
  • create/delete files/directories?

Also, what are the general keywords by which those kinds devices/protocol are known/called?

Skull answered 30/4, 2013 at 8:50 Comment(3)
I'm assuming this is some kind of media player? Sometimes these devices present their storage via a shell namespace extension. You can use IShellFolder and friends to access them.Cynar
I have same issues with you and did you have any solution?Lansquenet
@Lansquenet Have a look at the answers below, they have links to MSDN with details about the APIs which may be relevantSkull
S
5

If you're talking about mobile devices that appears like mounted volumes but without any mounted points, well they are called Windows Portable Devices and they are not mounted volumes.

Windows doesn't access the file system directly, it only communicates with them through the Media Transfer Protocol (MTP) or Picture Transfert Protocol (PTP) and creates a shell namespace extension to present a virtual folder to the user.

If you want to communicate with these kind of devices, then you most probably want to use the WPD API.

Senlac answered 30/4, 2013 at 15:3 Comment(2)
The link to WPD API looks for now like something that might be useful, thanks! Have you ever used this API, by any chance?Skull
@Skull I think I got a code that lie around somewhere, I'll try to make a short example out of it if possible.Senlac
S
0

If you're talking about a USB mass storage device without any assigned drive letter then it's just a volume without a mount point. You'll need to mount the volume before reading/writing it.

You can use the Volume Management Functions :

Here is a quickly-written example in C that list all the existing volumes, mount the unmounted ones and show some information about each volume :

char volumeID[256], volumePathName[256], volumeName[256], volumeFS[256];
char newMountPoint[4] = " :\\";
unsigned long volumeSerialNumber;
unsigned long size;
HANDLE handle = FindFirstVolume(volumeID, 256);
do {
    printf("Volume GUID = %s\n", volumeID);
    GetVolumePathNamesForVolumeName(volumeID, volumePathName, 256, &size);
    if(strlen(volumePathName) == 0) {
        printf("Not mounted\n");
        newMountPoint[0] = firstFreeLetter();
        if(SetVolumeMountPoint(newMountPoint, volumeID)) {
            GetVolumePathNamesForVolumeName(volumeID, volumePathName, 256, &size);
            printf("Now mounted on %s\n", volumePathName);
        }
    }
    else {
        printf("Mounted on %s\n", volumePathName);
    }
    GetVolumeInformation(volumePathName, volumeName, 256, &volumeSerialNumber,
                         NULL, NULL, volumeFS, 256);
    printf("Volume name = %s, FS = %s, serial = %lu\n\n",
           volumeName, volumeFS, volumeSerialNumber);

}while(FindNextVolume(handle, volumeID, 256));

FindVolumeClose(handle);

I deliberetely simplify this example, but a volume can have multiple mount points (volumePathName is actually a multi-string). It uses this function to get the first available letter (after 'C') to mount a drive :

char firstFreeLetter() {
    unsigned long freeLetters = GetLogicalDrives();
    if(freeLetters < 4) return 0;
    char letter = 'C';
    for(unsigned long i=4; (freeLetters & i) != 0; ++letter, i <<= 1);
    return letter;
}
Senlac answered 30/4, 2013 at 9:0 Comment(2)
I think my case may be rather some kinda MTP or something, but I don't know this area too well, so I'd prefer if somebody more knowledgeable would answer me if this is the only possibility. And how to use the API, anyway. I'm not convinced if this device is mountable at all.Skull
@Skull Yes, sorry when I read USB Storage, I understood USB flash drive and I didn't thought of media devices. See my other answer, I'll probably delete that one.Senlac
U
0

Yes. There are few cases a USB drive doesn't have a drive letter.

  1. Bluetooth device, USB camera doesnt have drive letter. but this is not your case, since you mentioned storage device.
  2. A USB storage device which is registered in PC not as a storage device. eg. USB MP3 player (MTP device) will be detected as Music Player and doesn't have a drive letter.

I believe yours is case #2.

In order to access files in a storage device, you need to force mount it as a storage device with drive letter. Unless you have a mount point, you cannot access the files i believe. This depends on the devices. Some devices (MTP) have internal settings to decide whether to detect as storage or not. You can explore the settings in the MTP device.

Otherwise you need to force all the storage device to mount through code.

Unlookedfor answered 2/5, 2013 at 7:24 Comment(3)
You can't mount a media device and you don't need to in order to transfert files. That's the whole point of MTP/PTP, you don't have to mount or unmount (eject) the device, you don't need any device driver, you just communicate with it through the transfert procotol. If you change the device configuration to appear as an USB Mass Storage then it's not a PTP/MTP device anymore (and it's not always possible to do so).Senlac
Also note that your first and second cases are the same, a USB camera is usually a PTP device while a MP3 player or an Android Phone are usually MTP devices, but they are both storage devices and they use the same protocol.Senlac
Misinforming the answer as it is, it was still valuable and I'm grateful for it, as it introduced the MTP keyword to the discussion at all...Skull

© 2022 - 2024 — McMap. All rights reserved.