以编程方式获取系统启动在C ++的时间(视窗) [英] Programmatically getting system boot up time in c++ (windows)

查看:60
本文介绍了以编程方式获取系统启动在C ++的时间(视窗)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以很简单,问题是如何让系统启动时间与C窗/ C ++。

So quite simply, the question is how to get the system boot up time in windows with c/c++.

搜索这并没有让我任何回答,我只发现了被读取文件的时间戳一个真正​​哈克的方法(​​不用说,我放弃了阅读的一半)。

Searching for this hasn't got me any answer, I have only found a really hacky approach which is reading a file timestamp ( needless to say, I abandoned reading that halfway ).

这是我发现其实是读出窗诊断另一种方法记录的事件?据说有一次的开机时间。

Another approach that I found was actually reading windows diagnostics logged events? Supposedly that has last boot up time.

有谁知道如何做到这一点(有希望没有太多丑陋的黑客)?

Does anyone know how to do this (with hopefully not too many ugly hacks)?

推荐答案

<一个href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/ms724411%28v=vs.85%29.aspx\"><$c$c>GetTickCount64 自从检索系统启动之后所经过的毫秒数。

GetTickCount64 "retrieves the number of milliseconds that have elapsed since the system was started."

一旦你知道过了多久,系统已经运行,它只是从当前时间中减去这个时间来确定,当它被启动的问题。

Once you know how long the system has been running, it is simply a matter of subtracting this duration from the current time to determine when it was booted. For example, using the C++11 chrono library (supported by Visual C++ 2012):

auto uptime = std::chrono::milliseconds(GetTickCount64());
auto boot_time = std::chrono::system_clock::now() - uptime;

这篇关于以编程方式获取系统启动在C ++的时间(视窗)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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