[Measure/FreeDiskSpace]

- Prevent the system from displaying "No disk" error message box (e.g. removable media is not ready)
This commit is contained in:
spx 2009-10-14 09:18:12 +00:00
parent c5155982ee
commit bdcad72ed3
2 changed files with 87 additions and 22 deletions

View File

@ -33,6 +33,7 @@ CMeasureDiskSpace::CMeasureDiskSpace(CMeterWindow* meterWindow) : CMeasure(meter
m_Total = false; m_Total = false;
m_Label = false; m_Label = false;
m_IgnoreRemovable = true; m_IgnoreRemovable = true;
m_OldTotalBytes = 0;
} }
/* /*
@ -58,11 +59,20 @@ bool CMeasureDiskSpace::Update()
if (!m_Drive.empty()) if (!m_Drive.empty())
{ {
UINT type = GetDriveType(m_Drive.c_str()); UINT type = GetDriveType(m_Drive.c_str());
if (type != DRIVE_NO_ROOT_DIR)
{
if (type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives if (type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
{ {
ULARGE_INTEGER i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes; ULARGE_INTEGER i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes;
if(GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes)) UINT oldMode = SetErrorMode(0);
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box
SetLastError(ERROR_SUCCESS);
BOOL result = GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes);
DWORD err = GetLastError();
SetErrorMode(oldMode); // Reset
if (result)
{ {
LARGE_INTEGER tmpInt; LARGE_INTEGER tmpInt;
if (m_Total) if (m_Total)
@ -74,19 +84,61 @@ bool CMeasureDiskSpace::Update()
tmpInt.QuadPart = i64FreeBytes.QuadPart; tmpInt.QuadPart = i64FreeBytes.QuadPart;
} }
m_Value = (double)tmpInt.QuadPart; m_Value = (double)tmpInt.QuadPart;
if (i64TotalBytes.QuadPart != m_OldTotalBytes)
{
// Total size was changed, so set new max value.
tmpInt.QuadPart = i64TotalBytes.QuadPart;
m_MaxValue = (double)tmpInt.QuadPart;
m_OldTotalBytes = i64TotalBytes.QuadPart;
}
}
else if (err == ERROR_NOT_READY)
{
// Media isn't ready, so reset to zero.
m_Value = 0;
m_OldTotalBytes = 0;
} }
} }
else else
{ {
m_Value = 0; m_Value = 0;
m_OldTotalBytes = 0;
} }
if (m_Label) if (m_Label)
{
if (!m_IgnoreRemovable || type != DRIVE_REMOVABLE) // Ignore removable drives
{ {
WCHAR volumeName[MAX_PATH] = {0}; WCHAR volumeName[MAX_PATH] = {0};
GetVolumeInformation(m_Drive.c_str(), volumeName, MAX_PATH, NULL, NULL, NULL, NULL, 0);
UINT oldMode = SetErrorMode(0);
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box
BOOL result = GetVolumeInformation(m_Drive.c_str(), volumeName, MAX_PATH, NULL, NULL, NULL, NULL, 0);
SetErrorMode(oldMode); // Reset
if (result)
{
m_LabelName = volumeName; m_LabelName = volumeName;
} }
else
{
m_LabelName = L"";
}
}
else
{
m_LabelName = L"";
}
}
}
else // Drive path is invalid.
{
m_Value = 0;
m_OldTotalBytes = 0;
if (m_Label) m_LabelName = L"";
}
} }
return PostUpdate(); return PostUpdate();
@ -121,7 +173,7 @@ void CMeasureDiskSpace::ReadConfig(CConfigParser& parser, const WCHAR* section)
m_Drive = parser.ReadString(section, L"Drive", L"C:\\"); m_Drive = parser.ReadString(section, L"Drive", L"C:\\");
if (m_Drive.empty()) if (m_Drive.empty())
{ {
DebugLog(L"Drive letter is not given."); DebugLog(L"Drive path is not given.");
m_Value = 0; m_Value = 0;
m_LabelName = L""; m_LabelName = L"";
} }
@ -138,17 +190,28 @@ void CMeasureDiskSpace::ReadConfig(CConfigParser& parser, const WCHAR* section)
if (!m_Drive.empty()) if (!m_Drive.empty())
{ {
UINT type = GetDriveType(m_Drive.c_str()); UINT type = GetDriveType(m_Drive.c_str());
if (type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives if (type != DRIVE_NO_ROOT_DIR &&
type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
{ {
ULARGE_INTEGER i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes; ULARGE_INTEGER i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes;
if(GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes)) UINT oldMode = SetErrorMode(0);
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box
BOOL result = GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes);
SetErrorMode(oldMode); // Reset
if (result)
{ {
LARGE_INTEGER tmpInt; LARGE_INTEGER tmpInt;
tmpInt.QuadPart = i64TotalBytes.QuadPart; tmpInt.QuadPart = i64TotalBytes.QuadPart;
m_MaxValue = (double)tmpInt.QuadPart; m_MaxValue = (double)tmpInt.QuadPart;
m_OldTotalBytes = i64TotalBytes.QuadPart;
}
else
{
m_OldTotalBytes = 0;
} }
} }
} }
} }

View File

@ -37,6 +37,8 @@ private:
bool m_Total; bool m_Total;
bool m_Label; bool m_Label;
bool m_IgnoreRemovable; bool m_IgnoreRemovable;
ULONGLONG m_OldTotalBytes;
}; };
#endif #endif