如何从ULONGLONG毫秒创建一个SYSTEMTIME结构? [英] How can I create a SYSTEMTIME struct from ULONGLONG milliseconds?

查看:334
本文介绍了如何从ULONGLONG毫秒创建一个SYSTEMTIME结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而不是使用WMI获得最后的启动时间,我想使用 :: GetSystemTime() :: GetTickCount64 。但是一旦我到达毫秒,我不知道如何回到FILETIME对象。

Rather than using WMI to obtain the last boot time, I wanted to calculate it using ::GetSystemTime() and ::GetTickCount64. But once I get to milliseconds, I don't know how to get back to a FILETIME object.

我试过这个:

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(&lt);

    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(&lt, &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), &lt2);

    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;
}

但我肯定没有得到预期的结果。相反,我得到:

But I certainly don't get what's expected. Instead, I get:

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或因为它不适用于我。

Any ideas? Please don't tell me to use Boost or because it's not available to me.

推荐答案


系统时间(转换后)为:52428:52428:52428

The system time (post conversion) is: 52428:52428:52428

当得到weirdo值时, 52428 == 0xcccc。这是调试生成代码用于初始化变量的值。因此,您正在查看未初始化的内存。

When you get weirdo values, always convert to hex first. 52428 == 0xcccc. That's the value that the debug build generated code uses to initialize variables. So you are looking at uninitialized memory.

下一个防御策略是永远不要忽略Windows api函数的返回值。使用VERIFY()宏,如MFC中提供的宏。您将很容易看到FileTimeToSystemTime()失败。

Next defense strategy is to never ignore the return value of Windows api function. Use a VERIFY() macro, like the one available in MFC. You'll then easily see that FileTimeToSystemTime() fails.

错误是-10000。

这篇关于如何从ULONGLONG毫秒创建一个SYSTEMTIME结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆