C++获取系统启动时间

这里写自定义目录标题

这里写自定义目录标题

获取系统启动时间

转换时间戳

code

cpp 复制代码
#include<stdio.h> 
#include<windows.h>  
#include <ctime>

#include <iostream>
#define NT_SUCCESS(x) ((x)>=0) 
const UINT SystemTimeInformation = 3;

typedef struct {
	LARGE_INTEGER liKeBootTime;
	LARGE_INTEGER liKeSystemTime;
	LARGE_INTEGER liExpTimeZoneBias;
	ULONG uCurrentTimeZoneId;
	DWORD dwReserved;
} SYSTEM_TIME_INFORMATION;

typedef long(__stdcall *fnNtQuerySystemInformation)(
	IN  UINT SystemInformationClass,
	OUT PVOID SystemInformation,
	IN  ULONG SystemInformationLength,
	OUT PULONG ReturnLength OPTIONAL);

static fnNtQuerySystemInformation NtQuerySystemInformation = NULL;

int main(void)
{
	NtQuerySystemInformation = (fnNtQuerySystemInformation)GetProcAddress(LoadLibrary("ntdll.dll"),
		"NtQuerySystemInformation");
	if (NtQuerySystemInformation == NULL)
	{
		printf("Get NtQuerySystemInformation Addr Failed,err=%u\n", GetLastError());
		return 0;
	}
	LONG status;
	SYSTEM_TIME_INFORMATION sti;
	status = NtQuerySystemInformation(SystemTimeInformation, &sti, sizeof(sti), 0);
	if (NO_ERROR != status)
	{
		printf("NtQuerySystemInformation Failed!n");
		return 0;
	}
	FILETIME ft1, ft2;
	SYSTEMTIME st;
	memcpy_s(&ft1, sizeof(ft1), &sti.liKeBootTime, sizeof(sti.liKeBootTime));
	//将其转为文件时间  
	//将一个FILETIME结构转换成本地时间  
	if (0 == FileTimeToLocalFileTime(&ft1, &ft2))
	{
		printf("FileTimeToLocalFileTime Failed err=%u\nn", GetLastError());
		return 0;
	}
	//将文件时间转为系统时间
	if (0 == FileTimeToSystemTime(&ft2, &st))
	{
		printf("FileTimeToSystemTimeFailed err=%u\nn", GetLastError());
		return 0;
	}
	printf("Date: %02d-%02d-%04d Time: %02d:%02d:%02d \n", st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond);



	LARGE_INTEGER li;
	li.LowPart = ft1.dwLowDateTime;
	li.HighPart = ft1.dwHighDateTime;

	// 需要对LARGE_INTEGER结构的值进行校正,因为FILETIME以100纳秒为单位,
	// 而Unix时间戳以秒为单位。同时需要考虑从1601年1月1日到1970年1月1日的时间偏移。
	li.QuadPart -= 116444736000000000ULL; // 116444736000000000 = 100纳秒 * (3600 * 24 * 365 * 30 + 3600 * 24 * 15)
	LONG64 timestamp = (LONG64)(li.QuadPart / 10000000); // 
	printf("Unix timestamp: %ld\n", timestamp);





	// 计算时间戳
	FILETIME epoch;
	ZeroMemory(&epoch, sizeof(FILETIME));
	SYSTEMTIME stEpoch = { 1970, 1, 0, 1, 0, 0, 0, 0 };
	SystemTimeToFileTime(&stEpoch, &epoch);

	ULARGE_INTEGER ullBootTime;
	ULARGE_INTEGER ullEpoch;
	ullBootTime.LowPart = ft2.dwLowDateTime;
	ullBootTime.HighPart = ft2.dwHighDateTime;
	ullEpoch.LowPart = epoch.dwLowDateTime;
	ullEpoch.HighPart = epoch.dwHighDateTime;

	// 计算时间戳(单位:秒)
	LONG64 nSysStartRunTime = static_cast<LONG64>((ullBootTime.QuadPart - ullEpoch.QuadPart) / 10000000);
	std::cout << "系统启动时间: " << nSysStartRunTime << std::endl;

	getchar();
	return 0;
}
相关推荐
智者知已应修善业7 小时前
【输入两个数字,判断两数相乘是否等于各自逆序数相乘】2023-10-24
c语言·c++·经验分享·笔记·算法·1024程序员节
CoderYanger17 小时前
动态规划算法-子数组、子串系列(数组中连续的一段):21.乘积最大子数组
开发语言·算法·leetcode·职场和发展·动态规划·1024程序员节
CoderYanger17 小时前
A.每日一题——3432. 统计元素和差值为偶数的分区方案
java·数据结构·算法·leetcode·1024程序员节
CoderYanger19 小时前
动态规划算法-子数组、子串系列(数组中连续的一段):26.环绕字符串中唯一的子字符串
java·算法·leetcode·动态规划·1024程序员节
韩家阿杰2 天前
RabbitMQ技术的使用
1024程序员节
CoderYanger3 天前
动态规划算法-简单多状态dp问题:15.买卖股票的最佳时机含冷冻期
开发语言·算法·leetcode·动态规划·1024程序员节
CoderYanger3 天前
递归、搜索与回溯-FloodFill:33.太平洋大西洋水流问题
java·算法·leetcode·1024程序员节
CoderYanger3 天前
动态规划算法-斐波那契数列模型:2.三步问题
开发语言·算法·leetcode·面试·职场和发展·动态规划·1024程序员节
CoderYanger3 天前
动态规划算法-简单多状态dp问题:16.买卖股票的最佳时机含手续费
开发语言·算法·leetcode·动态规划·1024程序员节
CoderYanger3 天前
C.滑动窗口-求子数组个数-越短越合法——3258. 统计满足 K 约束的子字符串数量 I
java·开发语言·算法·leetcode·1024程序员节