2

我试图通过获取当前时间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;
}
4

1 回答 1

2

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

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

osvi.dwOSVersionInfoSize = sizeof( osvi );

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

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

于 2011-03-23T21:27:48.427 回答