[转载]Windows 2000系统中如何获取系统的启动时间

来源:互联网 发布:融资出让股份算法 编辑:程序博客网 时间:2024/04/30 02:35
// timeboot.cpp (Windows NT/2000)
//
// This example will show how you can get the time
// when your system was booted.
//
// (c)1999 Ashot Oganesyan K, SmartLine, Inc
// mailto:ashot@aha.ru, [url]http://www.protect-me.com[/url], [url]http://www.codepile.com[/url]

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

#define SystemTimeInformation 3

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


// ntdll!NtQuerySystemInformation (NT specific!)
//
// The function copies the system information of the
// specified type into a buffer
//
// NTSYSAPI
// NTSTATUS
// NTAPI
// NtQuerySystemInformation(
//   IN UINT SystemInformationClass,   // information type
//   OUT PVOID SystemInformation,     // pointer to buffer
//   IN ULONG SystemInformationLength, // buffer size in bytes
//   OUT PULONG ReturnLength OPTIONAL   // pointer to a 32-bit
//                           // variable that receives
//                           // the number of bytes
//                           // written to the buffer
// );
typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);


PROCNTQSI NtQuerySystemInformation;


void main(void)
{
SYSTEM_TIME_INFORMATION Sti;
LONG             status;
FILETIME           ftSystemBoot;
SYSTEMTIME         stSystemBoot;

NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(
                          GetModuleHandle("ntdll"),
                          "NtQuerySystemInformation"
                          );

if (!NtQuerySystemInformation)
  return;

status = NtQuerySystemInformation(SystemTimeInformation,&Sti,sizeof(Sti),0);
if (status!=NO_ERROR)
  return;

ftSystemBoot = *(FILETIME *)&(Sti.liKeBootTime);

FileTimeToLocalFileTime(&ftSystemBoot,&ftSystemBoot);
FileTimeToSystemTime(&ftSystemBoot,&stSystemBoot);

printf("Date: %02d-%02d-%04d/nTime: %02d:%02d:%02d/n",
      stSystemBoot.wMonth,stSystemBoot.wDay,stSystemBoot.wYear,
      stSystemBoot.wHour,stSystemBoot.wMinute,stSystemBoot.wSecond);
}
原创粉丝点击