How to list all physical drives and find there drive letter?

HUANG da 21 Reputation points
2021-10-22T06:25:24.797+00:00

What I am doing is, to help users (me) to find the raw data sdcard (its physical drive no). and to warn them (me) not to WRITE any thing on that drive letter.

for finding part, I have done this:

for (1 to 10) {
CreateFile(""\.\PhysicalDriveX", ...
if invalid handle, break.
}

I could get all physical drives.

But for warning part, how can I get there drive letter?
I found a sample code like this:

GetLogicalDriveStrings()
for (those drive strings) {
handle = CreateFile(drive string, ...
DeviceIoControl(handle , IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, ...
}

But for C drive, I got a "3 = ERROR_PATH_NOT_FOUND",
for D~G drive, sdcard read without card, I got "21 = ERROR_NOT_READY",
for H drive, sdcard read with a raw data card, I got "1005 = ERROR_UNRECOGNIZED_VOLUME"

Please help.
(Using pure C with win32 api)

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
722 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,461 Reputation points
    2021-10-22T09:10:12.327+00:00

    I have this old code in my archives which seems to work (as Admin) :

    WCHAR sBuf[1000];
    WCHAR sBuffer1[255], sBuffer2[50];
    DWORD nSize, nCpt;
    nSize = GetLogicalDriveStrings(1000, sBuf);
    nCpt = 0;
    while (nCpt < nSize)
    {
        wsprintf(sBuffer1, L"\\\\.\\%s", &sBuf[nCpt]);
        sBuffer1[6] = 0;
        // strcpy(sBuffer1, &num[6]);
        HANDLE  hVol = NULL;
        hVol = CreateFile(sBuffer1,
            GENERIC_READ,
            FILE_SHARE_READ | FILE_SHARE_WRITE,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            NULL);
        int                         maxDisks;
        BOOL                        b;
        DWORD                       cbBytes;
        PVOLUME_DISK_EXTENTS        PVolDiskExtent;
        PVolDiskExtent = (PVOLUME_DISK_EXTENTS)LocalAlloc(LMEM_FIXED, sizeof(VOLUME_DISK_EXTENTS));
        STORAGE_DEVICE_NUMBER sdn;
        DWORD dwBytesReturned = 0;
        b = DeviceIoControl(hVol, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof(sdn), &dwBytesReturned, NULL);
        int nType = sdn.DeviceType;
        b = DeviceIoControl(hVol, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, PVolDiskExtent, sizeof(VOLUME_DISK_EXTENTS), &cbBytes, NULL);
        int nDiskNumber = 0;
        if (!b)
        {
            int n = GetLastError();
            if (n == ERROR_MORE_DATA)
            {
                maxDisks = PVolDiskExtent->NumberOfDiskExtents;
                LocalFree(PVolDiskExtent);
                PVolDiskExtent = (PVOLUME_DISK_EXTENTS)LocalAlloc(LMEM_FIXED, sizeof(VOLUME_DISK_EXTENTS) + (sizeof(DISK_EXTENT) * maxDisks));
                b = DeviceIoControl(hVol, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, NULL, 0, PVolDiskExtent, sizeof(VOLUME_DISK_EXTENTS) + (sizeof(DISK_EXTENT) * maxDisks),
                    &cbBytes,
                    NULL);
                nDiskNumber = PVolDiskExtent->Extents[0].DiskNumber;
            }
            else
                nDiskNumber = -1;
        }
        else
        {
            nDiskNumber = PVolDiskExtent->Extents[0].DiskNumber;                           
        }
        WCHAR wsText[255] = L"";
        wsprintf(wsText, L"Disk : %s - Disk Number : %d - Type : %d\n", &sBuf[nCpt], nDiskNumber, nType);
        OutputDebugString(wsText);
        CloseHandle(hVol);
        nCpt += lstrlen(&sBuf[nCpt]) + 1;
    }
    
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 11,256 Reputation points Microsoft Vendor
    2021-10-22T08:26:19.99+00:00

    Have you seen the Assign Drive Letter Sample? And according to IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, DeviceIoControl receives a handle to device.


  2. HUANG da 21 Reputation points
    2021-10-26T05:59:58.033+00:00

    WRONG: CreateFile(L"C:\", ...

    RIGHT: CreateFile(L"\\.\C:", ...

    0 comments No comments