取得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;
}


你可能感兴趣的:(windows,windows,windows,windows,开机时间)