News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Boot time

Started by TimoVJL, November 08, 2025, 03:01:42 PM

Previous topic - Next topic

TimoVJL

Inspirate from
http://masm32.com/board/index.php?msg=140972
I might fix this later.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define NTSTATUS  LONG

typedef struct _SYSTEM_TIMEOFDAY_INFORMATION
{
    LARGE_INTEGER BootTime;                     // Number of 100-nanosecond intervals since the system was started.
    LARGE_INTEGER CurrentTime;                  // The current system date and time.
    LARGE_INTEGER TimeZoneBias;                 // Number of 100-nanosecond intervals between local time and Coordinated Universal Time (UTC).
    ULONG TimeZoneId;                           // The current system time zone identifier.
    ULONG Reserved;                             // Reserved
    ULONGLONG BootTimeBias;                     // Number of 100-nanosecond intervals between the boot time and Coordinated Universal Time (UTC).
    ULONGLONG SleepTimeBias;                    // Number of 100-nanosecond intervals between the sleep time and Coordinated Universal Time (UTC).
} SYSTEM_TIMEOFDAY_INFORMATION, *PSYSTEM_TIMEOFDAY_INFORMATION;

#define SystemTimeOfDayInformation 3
// enum SYSTEM_INFORMATION_CLASS

//NTSTATUS WINAPI NtQuerySystemInformation(int SystemInformationClass,
//    PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength);

typedef int (WINAPI NTQUERYSYSTEMINFORMATION)(int SystemInformationClass,
    PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength);
typedef NTQUERYSYSTEMINFORMATION *LNTQUERYSYSTEMINFORMATION;
// FILETIME SYSTEMTIME
void __cdecl WinMainCRTStartup(void)
{
    HANDLE hLib;
    static LNTQUERYSYSTEMINFORMATION NtQuerySystemInformation;
    SYSTEM_TIMEOFDAY_INFORMATION stodi;
    FILETIME ft;
    SYSTEMTIME st;
    char szBuf[50];
    hLib = LoadLibrary("ntdll.dll");
    NtQuerySystemInformation = (LNTQUERYSYSTEMINFORMATION)GetProcAddress(hLib, "NtQuerySystemInformation");
    NtQuerySystemInformation(SystemTimeOfDayInformation, &stodi, sizeof(stodi), 0);
    (*(LONGLONG*)&stodi.BootTime) -= (*(LONGLONG*)&stodi.TimeZoneBias);
    ft.dwLowDateTime = ((LARGE_INTEGER*)&stodi.BootTime)->LowPart;
    ft.dwHighDateTime = ((LARGE_INTEGER*)&stodi.BootTime)->HighPart;
    FileTimeToSystemTime(&ft, &st);
    wsprintf(szBuf, "Date: %04d-%02d-%02d\nTime: %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay,
        st.wHour, st.wMinute, st.wSecond);
    MessageBox(0, szBuf, "Boot time", MB_OK);
    ExitProcess(0);
}
May the source be with you

Vortex

Hi Timo,

Keep up the nice work.
Code it... That's all...

Vortex

Hi Timo,

Another method to get the boot time is to use the GetTickCount64 API function.
Code it... That's all...