我不想使用 WMI 来获取上次启动时间,而是想使用::GetSystemTime()
and来计算它::GetTickCount64
。但是一旦我达到毫秒,我不知道如何回到 FILETIME 对象。
我试过这个:
static ULONGLONG FileTimeToMillis(const FILETIME &ft)
{
ULARGE_INTEGER uli;
uli.LowPart = ft.dwLowDateTime; // could use memcpy here!
uli.HighPart = ft.dwHighDateTime;
return uli.QuadPart/10000;
}
static void MillisToSystemTime(ULONGLONG millis, SYSTEMTIME *st)
{
UINT64 t = static_cast<UINT64>(-10000) * static_cast<UINT64>(millis);
FILETIME ft;
ft.dwLowDateTime = static_cast<DWORD(t & 0xFFFFFFFF);
ft.dwHighDateTime = static_cast<DWORD>(t >> 32);
::FileTimeToSystemTime(&ft, st);
}
void main()
{
SYSTEMTIME st, lt, st2, lt2;
::GetSystemTime(&st);
::GetLocalTime(<);
cout << "The system time is: " << st.wHour << ":" << st.wMinute << ":" << st.wSecond << endl;
cout << "The local time is: " << lt.wHour << ":" << lt.wMinute << ":" << lt.wSecond << endl;
FILETIME sft, lft;
::SystemTimeToFileTime(&st, &sft);
::SystemTimeToFileTime(<, &lft);
cout << "The system time in millis is: " << FileTimeToMillis(sft) << endl;
cout << "The local time in millis is: " << FileTimeToMillis(lft) << endl;
MillisToSystemTime(FileTimeToMillis(sft), &st2);
MillisToSystemTime(FileTimeToMillis(sft), <2);
cout << "The system time (post conversion) is: " << st2.wHour << ":" << st2.wMinute << ":" << st2.wSecond << endl;
cout << "The local time (post conversion) is: " << lt2.wHour << ":" << lt2.wMinute << ":" << lt2.wSecond << endl;
}
但我当然没有得到预期的结果。相反,我得到:
The system time is: 15:5:2
The local time is: 10:5:2
The system time in millis is: 12984678302935
The local time in millis is: 12984660302935
The system time (post conversion) is: 52428:52428:52428
The local time (post conversion) is: 52428:52428:52428
有任何想法吗?请不要告诉我使用 Boost 或因为它对我不可用。