/*++ Module Name: uptime.c Abstract: Program to print UPTIME. Author: Ahdrey Shedel You may distribute under the terms of the GNU General Public License You can contact author at andreys@cr.cyco.com or http://www.chat.ru/~ashedel --*/ #define WIN32_LEAN_AND_MEAN #define _DLL #include #include #pragma warning(push, 4) #pragma comment(exestr, "\n\nUPTIME: Copyright (C) 1999 Andrey Shedel (andreys@cr.cyco.com)\n\n") #pragma comment(lib,"kernel32.lib") #pragma comment(lib,"ntdll.lib") #pragma comment(linker, "-entry:raw_main") #pragma comment(linker, "-opt:nowin98") #pragma comment(linker, "-subsystem:console") #pragma comment(linker, "-merge:.rdata=.text") typedef ULONG NTSTATUS; typedef short CSHORT; typedef enum _SYSTEM_INFORMATION_CLASS { SystemTimeInformation = 3, // 0x20 }SYSTEM_INFORMATION_CLASS; //46 members. typedef struct _TIME_INFORMATION { LARGE_INTEGER BootTime; // 0x00 LARGE_INTEGER CurrentTime; // 0x08 LARGE_INTEGER TimeZoneBias; // 0x10 ULONG CurrentTimeZoneId; // 0x18 ULONG Zero; }TIME_INFORMATION, *PTIME_INFORMATION; // 0x20 NTSYSAPI NTSTATUS NTAPI NtQuerySystemInformation( IN SYSTEM_INFORMATION_CLASS InformationClass, IN PVOID InfoBuffer, IN ULONG BufferSize, OUT PULONG LengthReturned ); typedef struct _TIME_FIELDS { CSHORT Year; // range [1601...] CSHORT Month; // range [1..12] CSHORT Day; // range [1..31] CSHORT Hour; // range [0..23] CSHORT Minute; // range [0..59] CSHORT Second; // range [0..59] CSHORT Milliseconds;// range [0..999] CSHORT Weekday; // range [0..6] == [Sunday..Saturday] } TIME_FIELDS; typedef TIME_FIELDS *PTIME_FIELDS; NTSYSAPI VOID NTAPI RtlTimeToElapsedTimeFields ( PLARGE_INTEGER Time, PTIME_FIELDS TimeFields ); __inline void __fastcall _puts(IN const char* msg) { ULONG Written; WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msg, strlen(msg), &Written, NULL); } void __fastcall uptime_print( IN __int64 time ) { TIME_FIELDS tf; char sz[100]; #define format_year_part "%5hu" #define format_time_part "%hu:%02hu:%02hu.%03hu" static const char format[] = format_year_part " " format_time_part; RtlTimeToElapsedTimeFields((PLARGE_INTEGER)&time, &tf); if(0 != tf.Day) { sprintf(sz, format, tf.Day, tf.Hour, tf.Minute, tf.Second, tf.Milliseconds); } else { sprintf(sz, format + sizeof(format_year_part), tf.Hour, tf.Minute, tf.Second, tf.Milliseconds); } _puts(sz); } __inline __int64 uptime_get(void) { TIME_INFORMATION info; ULONG length; if(NtQuerySystemInformation( SystemTimeInformation, &info, sizeof(info), &length )) { return 0; } return info.CurrentTime.QuadPart - info.BootTime.QuadPart; } void __cdecl raw_main(void) { uptime_print(uptime_get()); ExitProcess(0); }