Programming Tips - Win32: find fastest drive (RAM disk)

Date: 2022oct8 OS: Windows Language: C/C++ Q. Win32: find fastest drive (RAM disk) A. Here we use GetDriveType() to find a RAM disk as first choice and the first non-removable drive as second choice.
void FindFastestDrive(LPSTR szFastestDrive, const size_t sizeFastestDrive, int &fastestType) { szFastestDrive[0] = '\0'; fastestType = -1; ULARGE_INTEGER available, total, free; ULARGE_INTEGER OneHundredMeg = { 100 * 1024 * 1024, 0 }; char szDrive[MAX_PATH] = "?:\\"; for (szDrive[0] = 'a'; szDrive[0] <= 'z'; szDrive[0]++) { // Yes it could be A: (Softperfect RAM disk shows that as an option) int type = GetDriveType(szDrive); if (type == DRIVE_NO_ROOT_DIR) { continue; } if (!GetDiskFreeSpaceEx(szDrive, &available, &total, &free)) { continue; } if (total.QuadPart <= OneHundredMeg.QuadPart) { // The default size in SoftPerfect RAM disk type = DRIVE_RAMDISK; } if (type == DRIVE_FIXED) { if (fastestType < 0) { fastestType = type; lstrcpyn(szFastestDrive, szDrive, (int)sizeFastestDrive); } } else if (type == DRIVE_RAMDISK) { fastestType = type; lstrcpyn(szFastestDrive, szDrive, (int)sizeFastestDrive); break; } } } void ExampleUse() { char szSpeedy[MAX_PATH]; int speedyType; FindFastestDrive(szSpeedy, sizeof(szSpeedy), speedyType); printf("speedy=%s\n", szSpeedy); }
You can use WMI to see if a drive is SSD or HDD but the above worked for our application.