Skip to content

Commit 2221e54

Browse files
committed
OrcLib: SystemDetails: add GetShutdownTimeFromRegistry
1 parent 2b4c12e commit 2221e54

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/OrcLib/SystemDetails.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "BinaryBuffer.h"
2727
#include "Utils/Time.h"
2828
#include "Utils/TypeTraits.h"
29+
#include "Utils/Guard.h"
2930

3031
namespace fs = std::filesystem;
3132
using namespace std::string_view_literals;
@@ -1007,6 +1008,34 @@ HRESULT Orc::SystemDetails::GetUserLanguage(std::wstring& strLanguage)
10071008
return S_OK;
10081009
}
10091010

1011+
Result<std::chrono::system_clock::time_point> Orc::SystemDetails::GetShutdownTimeFromRegistry()
1012+
{
1013+
const wchar_t key[] = L"SYSTEM\\CurrentControlSet\\Control\\Windows";
1014+
1015+
Guard::RegistryHandle hKey;
1016+
auto status = RegOpenKeyW(HKEY_LOCAL_MACHINE, key, &hKey.value());
1017+
if (status != ERROR_SUCCESS)
1018+
{
1019+
auto ec = Win32Error(status);
1020+
Log::Debug(L"Failed RegOpenKeyW (key: {}) [{}]", key, ec);
1021+
return ec;
1022+
}
1023+
1024+
FILETIME shutdownTime;
1025+
const wchar_t value[] = L"ShutdownTime";
1026+
DWORD valueType = 0L;
1027+
DWORD cbData = sizeof(shutdownTime);
1028+
status = RegQueryValueExW(hKey.value(), value, NULL, &valueType, (LPBYTE)&shutdownTime, &cbData);
1029+
if (status != ERROR_SUCCESS)
1030+
{
1031+
auto ec = Win32Error(status);
1032+
Log::Debug(L"Failed RegQueryValueExW (key: {}, value: {}) [{}]", key, value, ec);
1033+
return ec;
1034+
}
1035+
1036+
return FromFileTime(shutdownTime);
1037+
}
1038+
10101039
SystemDetails::DriveType SystemDetails::GetPathLocation(const std::wstring& strAnyPath)
10111040
{
10121041
WCHAR szVolume[ORC_MAX_PATH];

src/OrcLib/SystemDetails.h

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class SystemDetails
114114
static HRESULT GetSystemLanguage(std::wstring& strLocale);
115115
static HRESULT GetUserLanguage(std::wstring& strLocale);
116116

117+
static Result<std::chrono::system_clock::time_point> GetShutdownTimeFromRegistry();
117118
static HRESULT GetCurrentWorkingDirectory(std::filesystem::path& cwd);
118119

119120
static HRESULT GetProcessBinary(std::wstring& strFullPath);

0 commit comments

Comments
 (0)