取得windows系统开机日期和时间

简介:

    很简单,原生态函数NtQuerySystemInformation已经为我们提供了这个功能,

虽然不那么优雅 :)


#include	<stdio.h>
#include	<stdbool.h>
#include	<windows.h>

#define NT_SUCCESS(x) ((x)>=0)
#define 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!\n");
		exit(-1);
	}

	LONG status;
	SYSTEM_TIME_INFORMATION sti;
		
	status = NtQuerySystemInformation(SystemTimeInformation,\
		&sti,sizeof(sti),0);
	
	printf("boot time (ms) == %I64X\n",sti.liKeBootTime.QuadPart);
	
	/*if(!NT_SUCCESS(status))
	{
		printf("NtQuerySystemInformation Failed!\n");
		goto quit;
	}*/
	
	if(NO_ERROR != status)
	{
		printf("NtQuerySystemInformation Failed!\n");
		exit(-1);
	}

	FILETIME ft;
	SYSTEMTIME st;
	memcpy(&ft,&sti.liKeBootTime,sizeof(ft));
	FileTimeToLocalFileTime(&ft,&ft);
	FileTimeToSystemTime(&ft,&st);
	
	printf("Date: %02d-%02d-%04d\nTime: %02d:%02d:%02d\n",st.wMonth,st.wDay,st.wYear,\
		st.wHour,st.wMinute,st.wSecond);
		
	getchar();
	return 0;
}


相关文章
|
10天前
如何隐藏windows10系统任务栏右下角的语言输入法图标?
如何隐藏windows10系统任务栏右下角的语言输入法图标?
|
1月前
|
安全 Linux Shell
全面对比linux和windows,选择哪个系统比较好
全面对比linux和windows,选择哪个系统比较好
63 0
|
1月前
|
监控 Windows
Windows系统中Wireshark抓包工具的安装使用
Windows系统中Wireshark抓包工具的安装使用
|
21天前
|
Shell Windows
Windows服务器 开机自启动服务
Windows服务器 开机自启动服务
13 0
|
1月前
|
应用服务中间件 nginx Windows
windows下快速安装nginx 并配置开机自启动
windows下快速安装nginx 并配置开机自启动
windows下快速安装nginx 并配置开机自启动
|
1月前
|
编译器 C语言 流计算
在Windows系统中创建C语言开发环境
【2月更文挑战第22天】在Windows系统中创建C语言开发环境
21 6
|
1月前
|
程序员 Docker Windows
Windows 10系统压缩C盘WSL虚拟磁盘文件
Windows 10系统压缩C盘WSL虚拟磁盘文件
33 1
|
1月前
|
安全 Linux 网络安全
|
2月前
|
安全 网络安全 虚拟化
VMware中安装Windows Server 2012系统
在VMware中安装Windows Server 2012系统
97 2
|
2月前
|
网络协议 Linux iOS开发
《熬夜整理》保姆级系列教程-玩转Wireshark抓包神器教程(2)-Wireshark在Windows系统上安装部署
【2月更文挑战第2天】《熬夜整理》保姆级系列教程-玩转Wireshark抓包神器教程(2)-Wireshark在Windows系统上安装部署
47 4