NtGetTickCount - NtDoc

Native API online documentation, based on the System Informer (formerly Process Hacker) phnt headers
#ifndef _NTEXAPI_H
#if (PHNT_MODE != PHNT_MODE_KERNEL)

/**
 * The NtGetTickCount routine retrieves the number of milliseconds that have elapsed since the system was started, up to 49.7 days.
 *
 * \return ULONG The return value is the number of milliseconds that have elapsed since the system was started.
 * \remarks The elapsed time is stored as a ULONG value. Therefore, the time will wrap around to zero if the system
 * is run continuously for 49.7 days. To avoid this problem, use the NtGetTickCount64 function. Otherwise, check
 * for an overflow condition when comparing times. The resolution of the NtGetTickCount function is limited to
 * the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds.
 * The resolution of the NtGetTickCount function is not affected by adjustments made by the GetSystemTimeAdjustment function.
 * \see https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-gettickcount
 */
FORCEINLINE
ULONG
NtGetTickCount(
    VOID
    )
{
#ifdef _WIN64

    return (ULONG)((USER_SHARED_DATA->TickCountQuad * USER_SHARED_DATA->TickCountMultiplier) >> 24);

#else

    ULARGE_INTEGER tickCount;

    while (TRUE)
    {
        tickCount.HighPart = (ULONG)USER_SHARED_DATA->TickCount.High1Time;
        tickCount.LowPart = USER_SHARED_DATA->TickCount.LowPart;

        if (tickCount.HighPart == (ULONG)USER_SHARED_DATA->TickCount.High2Time)
            break;

        YieldProcessor();
    }

    return (ULONG)((UInt32x32To64(tickCount.LowPart, USER_SHARED_DATA->TickCountMultiplier) >> 24) +
        UInt32x32To64((tickCount.HighPart << 8) & 0xffffffff, USER_SHARED_DATA->TickCountMultiplier));

#endif
}

#endif
#endif

View code on GitHub

NtDoc

No description available.

NTinternals.net (undocumented.ntinternals.net)

Function NtGetTickCount returns system Timer's ticks counter. This counter is also available in KUSER_SHARED_DATA structure as TickCountLow member.

Calling NtSetTimerResolution doesn't effect in counter's update resolution.

Documented by

See also