以编程方式获取系统启动时间在c + +(窗口)

所以很简单,问题是如何使用c / c ++在windows中获得系统启动时间。

寻找这个没有得到我任何答案,我只发现了一个非常冒险的方法,正在阅读文件时间戳(不用说,我放弃了半读)。

另一种方法,我发现实际上是阅读Windows诊断logging事件? 据说这是最后一次启动时间。

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

GetTickCount64 “检索自系统启动以来经过的毫秒数。”

一旦知道系统已经运行了多长时间,从当前时间减去这个持续时间就可以确定系统何时启动。 例如,使用C ++ 11计时库(由Visual C ++ 2012支持):

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

您也可以使用WMI来获取启动的准确时间。 WMI并不是因为心灵的不适,而是会让你找到你想要的东西。

有问题的信息在LastBootUpTime属性下的Win32_OperatingSystem对象上。 您可以使用WMI工具检查其他属性。

显示属性实例的WMI Explorer

编辑:你也可以从命令行得到这个信息,如果你喜欢。

 wmic OS Get LastBootUpTime 

作为C#中的一个例子,它看起来像下面的样子( 使用C ++它相当详细 ):

 static void Main(string[] args) { // Create a query for OS objects SelectQuery query = new SelectQuery("Win32_OperatingSystem", "Status=\"OK\""); // Initialize an object searcher with this query ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); string dtString; // Get the resulting collection and loop through it foreach (ManagementObject envVar in searcher.Get()) dtString = envVar["LastBootUpTime"].ToString(); }