首页 > 代码库 > 获取开机时间
获取开机时间
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1、用当前时间减去从开机到现在经过了的时间:</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="cpp"> DWORD dwStartTicks = GetTickCount() / 1000 / 60; // 分钟 SYSTEMTIME sysTime; GetSystemTime(&sysTime); FILETIME fileTime; ULARGE_INTEGER ulTime; SystemTimeToFileTime(&sysTime, &fileTime); ulTime.HighPart = fileTime.dwHighDateTime; ulTime.LowPart = fileTime.dwLowDateTime; DWORD dwCurTime = ulTime.QuadPart / (10 * 1000 * 1000 * 60); // 分钟
这种方法不太精确。
2、利用未公开的系统函数 NtQuerySystemInformation:
此函数在NTDLL.DLL中导出。
typedef struct { LARGE_INTEGER liKeBootTime; LARGE_INTEGER liKeSystemTime; LARGE_INTEGER liExpTimeZoneBias; ULONG uCurrentTimeZoneId; DWORD dwReserved; } SYSTEM_TIME_INFORMATION; LONG status; SYSTEM_TIME_INFORMATION Sti; HMODULE hMod = LoadLibraryW(L"NTDLL.DLL"); if(hMod) { typedef HRESULT (__stdcall * PFNtQuerySystemInformation)(IN UINT SystemInformationClass,OUT PVOID SystemInformation,IN ULONG SystemInformationLength, OUT PULONG ReturnLength OPTIONAL); PFNtQuerySystemInformation pfnNtQuerySystemInformation = (PFNtQuerySystemInformation)GetProcAddress(hMod, "NtQuerySystemInformation"); if(pfnNtQuerySystemInformation) pfnNtQuerySystemInformation(3, &Sti, sizeof(Sti), 0); FreeLibrary(hMod); } DWORD dwCurTime = Sti.liKeBootTime.QuadPart / (1000 * 60); // 分钟
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。