使用 GetTickCount64 检索启动时间

发布于 2024-10-26 02:05:46 字数 2093 浏览 4 评论 0原文

我试图通过获取当前时间 SYSTEMTIME 结构来提取启动时间,然后将其转换为 FILETIME,然后将其转换为 ULARGE_INTEGER,从中减去 GetTickCount64(),然后继续将所有内容转换回 SYSTEMTIME。

我将此函数与“NET STATISTICS WORKSTATION”进行比较,由于某种原因,我的输出关闭了几个小时,这似乎与任何时区差异都不匹配。

这是视觉工作室示例代码:

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
#define KILOBYTE 1024
#define BUFF KILOBYTE


int _tmain(int argc, _TCHAR* argv[])
{
    ULARGE_INTEGER ticks, ftime;
    SYSTEMTIME current, final;
    FILETIME ft, fout;

    OSVERSIONINFOEX osvi;

    char output[BUFF];
    int retval=0;
    ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
    ZeroMemory(&final, sizeof(SYSTEMTIME));
    GetVersionEx((OSVERSIONINFO *) &osvi);

    if (osvi.dwBuildNumber >= 6000) ticks.QuadPart = GetTickCount64();
    else ticks.QuadPart = GetTickCount();
    //Convert miliseconds to 100-nanosecond time intervals
    ticks.QuadPart = ticks.QuadPart * 10000;
    //GetLocalTime(&current); -- //doesn't really fix the problem
    GetSystemTime(&current);
    SystemTimeToFileTime(&current, &ft);
    printf("INITIAL: Filetime lowdatetime %u, highdatetime %u\r\n", ft.dwLowDateTime, ft.dwHighDateTime);
    ftime.LowPart=ft.dwLowDateTime;
    ftime.HighPart=ft.dwHighDateTime;
    //subtract boot time interval from current time
    ftime.QuadPart = ftime.QuadPart - ticks.QuadPart;
    //Convert ULARGE_INT back to FILETIME
    fout.dwLowDateTime = ftime.LowPart;
    fout.dwHighDateTime = ftime.HighPart;
    printf("FINAL: Filetime lowdatetime %u, highdatetime %u\r\n", fout.dwLowDateTime, fout.dwHighDateTime);
    //Convert FILETIME back to system time
    retval = FileTimeToSystemTime(&fout, &final);
    printf("Return value is %d\r\n", retval);
    printf("Current time %d-%.2d-%.2d %.2d:%.2d:%.2d\r\n", current.wYear, current.wMonth, current.wDay,  current.wHour, current.wMinute, current.wSecond);
    printf("Return time %d-%.2d-%.2d %.2d:%.2d:%.2d\r\n", final.wYear, final.wMonth, final.wDay,  final.wHour, final.wMinute, final.wSecond);
    return 0;
}

I'm trying to extract boot time by getting current time SYSTEMTIME structure, then converting it to FILETIME which I then convert to ULARGE_INTEGER from which I subtract GetTickCount64() and then proceed on converting everything back to SYSTEMTIME.

I'm comparing this function to 'NET STATISTICS WORKSTATION' and for some reason my output is off by several hours, which don't seem to match any timezone differences.

Here's visual studio example code:

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
#define KILOBYTE 1024
#define BUFF KILOBYTE


int _tmain(int argc, _TCHAR* argv[])
{
    ULARGE_INTEGER ticks, ftime;
    SYSTEMTIME current, final;
    FILETIME ft, fout;

    OSVERSIONINFOEX osvi;

    char output[BUFF];
    int retval=0;
    ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
    ZeroMemory(&final, sizeof(SYSTEMTIME));
    GetVersionEx((OSVERSIONINFO *) &osvi);

    if (osvi.dwBuildNumber >= 6000) ticks.QuadPart = GetTickCount64();
    else ticks.QuadPart = GetTickCount();
    //Convert miliseconds to 100-nanosecond time intervals
    ticks.QuadPart = ticks.QuadPart * 10000;
    //GetLocalTime(¤t); -- //doesn't really fix the problem
    GetSystemTime(¤t);
    SystemTimeToFileTime(¤t, &ft);
    printf("INITIAL: Filetime lowdatetime %u, highdatetime %u\r\n", ft.dwLowDateTime, ft.dwHighDateTime);
    ftime.LowPart=ft.dwLowDateTime;
    ftime.HighPart=ft.dwHighDateTime;
    //subtract boot time interval from current time
    ftime.QuadPart = ftime.QuadPart - ticks.QuadPart;
    //Convert ULARGE_INT back to FILETIME
    fout.dwLowDateTime = ftime.LowPart;
    fout.dwHighDateTime = ftime.HighPart;
    printf("FINAL: Filetime lowdatetime %u, highdatetime %u\r\n", fout.dwLowDateTime, fout.dwHighDateTime);
    //Convert FILETIME back to system time
    retval = FileTimeToSystemTime(&fout, &final);
    printf("Return value is %d\r\n", retval);
    printf("Current time %d-%.2d-%.2d %.2d:%.2d:%.2d\r\n", current.wYear, current.wMonth, current.wDay,  current.wHour, current.wMinute, current.wSecond);
    printf("Return time %d-%.2d-%.2d %.2d:%.2d:%.2d\r\n", final.wYear, final.wMonth, final.wDay,  final.wHour, final.wMinute, final.wSecond);
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转角预定愛 2024-11-02 02:05:46

我运行它,发现使用 GetLocalTime 时它可以正常工作,而不是使用 GetSystemTime(以 UTC 表示)。因此 GetSystemTime 不一定与 PC 上的“时钟”匹配是有道理的。

但除此之外,问题可能是对 GetVersionEx 的调用。正如所写,我认为它总是会为所有值返回零。在调用它之前,您需要此行:

osvi.dwOSVersionInfoSize = sizeof( osvi );

否则 dwBuildNumber 将为零,并且它将调用 GetTickCount,这仅适用于 49 天左右。另一方面,如果是这样的话,我认为你会得到一个差异更大的结果。

我不完全确定(如所写的)检查对于选择要调用的滴答计数函数是必要的。如果 GetTickCount64 不存在,则由于缺少入口点,应用程序将不会加载(除非使用了延迟加载......在这种情况下我不确定)。我认为有必要使用 LoadLibrary 和 GetProcAddress 在这两个函数之间动态做出决定,并使其在旧平台上工作。

I ran it and found that it works correctly when using GetLocalTime as opposed to GetSystemTime, which is expressed in UTC. So it would make sense that GetSystemTime would not necessarily match the "clock" on the PC.

Other than that, though, the issue could possibly be the call to GetVersionEx. As written, I think it will always return zeros for all values. You need this line prior to calling it:

osvi.dwOSVersionInfoSize = sizeof( osvi );

Otherwise that dwBuildNumber will be zero and it will call GetTickCount, which is only good for 49 days or so. On the other hand, if that were the case, I think you would get a result with a much larger difference.

I'm not completely sure that (as written) the check is necessary to choose which tick count function to call. If GetTickCount64 doesn't exist, the app would not load due to the missing entrypoint (unless perhaps delay loading was used ... I'm not sure in that case). I believe that it would be necessary to use LoadLibrary and GetProcAddress to make the decision dynamically between those two functions and have it work on an older platform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文