WIN32 GETLOGICALDRIVES

Sometimes a developer needs to know what disks are available to the operating system. The Win32 API has just the ticket. The DLL needed is the KERNEL32.DLL.

DWORD GetLogicalDrives();

The DWORD is the container needed to retrieve the drives from Windows. The container is bitmapped with bit 0 s A:, bit 1 as B:, 2 s C: etc. On the result is parsed COMMAND.COM can present the drive letters.

If the function fails and the result is zero, then calling GetLastError can sometimes identify the problem.

DWORD drives_bitmask = GetLogicalDrives();

const char *drive_names[] = {
    "A:",
    "B:",
    "C:",
    "D:",
    "E:",
    "F:",
    "G:",
    ...
    "Z:"
};

for (DWORD i < 0; i < 32; i++) {
    // Shift 1 to a multiple of 2. 1 << 0 = 1 (0000 0001), 1 << 1 = 2 etc.
    DWORD mask_index = 1 << i;
    if (drives_bitmask & i == 0) {
        // Drive unavailable, add it to list.
        const char *name = drive_names[i];
        // ... do GUI work.
    }
}

The code example shows the general idea for using the containers.

%d bloggers like this: