sysinfo;win32: update error checking

This commit is contained in:
Danny Robson 2019-06-20 17:21:35 +10:00
parent 3cace81ea5
commit b9d534048d

View File

@ -21,11 +21,12 @@ cruft::util::operator<< (std::ostream &os, sysinfo const &)
os << "{ ";
{
OSVERSIONINFO version;
OSVERSIONINFO version {};
version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (!GetVersionExA (&version))
win32::error::throw_code ();
os << "os: { name: win32, "
os << "os: { name: win32"
<< ", version: " << +version.dwMajorVersion << '.'
<< +version.dwMinorVersion << '.'
<< +version.dwPlatformId
@ -36,23 +37,33 @@ cruft::util::operator<< (std::ostream &os, sysinfo const &)
std::string name;
{
if (!GetComputerNameExA (ComputerNamePhysicalNetBIOS, nullptr, &size))
if (0 == GetComputerNameExA (ComputerNamePhysicalNetBIOS, nullptr, &size)) {
auto const code = win32::error::last_code ();
if (code != ERROR_MORE_DATA)
win32::error::throw_code (code);
}
name.resize (size);
if (0 == GetComputerNameExA (ComputerNamePhysicalNetBIOS, &name[0], &size))
win32::error::throw_code ();
name.resize (size);
if (!GetComputerNameExA (ComputerNamePhysicalNetBIOS, &name[0], &size))
win32::error::throw_code ();
os << ", hostname: " << name;
}
{
size = 0;
if (!GetUserNameA (nullptr, &size))
win32::error::throw_code ();
if (0 == GetUserNameA (nullptr, &size)) {
auto const code = win32::error::last_code ();
if (code != ERROR_INSUFFICIENT_BUFFER)
win32::error::throw_code (code);
}
name.resize (size);
if (!GetUserNameA (&name[0], &size))
if (0 == GetUserNameA (&name[0], &size))
win32::error::throw_code ();
name.resize (size);
os << ", username: " << name;
}
@ -61,7 +72,7 @@ cruft::util::operator<< (std::ostream &os, sysinfo const &)
MEMORYSTATUSEX status {};
status.dwLength = sizeof (status);
if (!GlobalMemoryStatusEx (&status))
if (0 == GlobalMemoryStatusEx (&status))
win32::error::throw_code ();
os << ", ram: { total: " << status.ullTotalPhys << ", available: " << status.ullAvailPhys << " },";