您当前的位置: 首页 >  windows

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

发布时间:2012-11-30 17:04:05 ,浏览量:0

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

虽然不那么优雅 :)

#include	
#include	
#include	

#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;
}
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    107766博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0848s