您的位置:首页 > 其它

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

2012-11-30 17:04 274 查看
很简单,原生态函数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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: