與其使用WMI獲取最後一次啓動時間,我想用::GetSystemTime()
和::GetTickCount64
來計算它。但是,一旦我達到毫秒,我不知道如何回到FILETIME對象。如何從ULONGLONG毫秒創建SYSTEMTIME結構?
我嘗試這樣做:
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或因爲它不適用於我。
在僞代碼中,您希望'FileTimeToSystemTime(GetSystemTimeAsFileTime - (10000 * GetTickCount64))' – arx
@arx MSDN建議不要對FILETIME結構本身執行算術運算。 –
這就是爲什麼它是僞代碼。 – arx