How to get Process Uptime in Windows

Hi there! Welcome to my blog where I share some tips and tricks on how to use C++ for various tasks. Today I’m going to show you how to get process uptime using the GetProcessTimes function and the <chrono> library.

What is process uptime?

Process uptime is the amount of time that a process has been running since it was created. It can be useful for monitoring performance, debugging, or logging purposes. For example, you might want to know how long your application has been running without crashing, or how long a certain task took to complete.

How to get process uptime using GetProcessTimes?

The GetProcessTimes function is a Windows API function that retrieves timing information for the specified process. It takes a handle to the process and four pointers to FILETIME structures as parameters. The FILETIME structures receive the creation time, exit time, kernel time, and user time of the process respectively.

The creation time is the point in time when the process was started. The exit time is the point in time when the process terminated. If the process has not exited yet, this value is undefined. The kernel time and user time are the amounts of time that the process has executed in kernel mode and user mode respectively.

The FILETIME structure contains two 32-bit values that combine to form a 64-bit count of 100-nanosecond intervals since January 1, 1601 UTC. To convert this value to a more human-readable format, we can use some functions from the <chrono> library, which provides utilities for measuring and manipulating durations and points in time.

Here’s an example of how we can use GetProcessTimes and <chrono> to get process uptime:

#include <Windows.h>
#include <Psapi.h>
#include <winnt.h>
#include <winternl.h>
#include <chrono>

using namespace std;
using namespace std::chrono;

double process::get_uptime_sec(DWORD pid) {
    double r{0};
    if(hProcess) {
        FILETIME creationTime, exitTime, kernelTime, userTime;
            hProcess, &creationTime, &exitTime, &kernelTime, &userTime)) {

            // convert to time_point
            file_clock::duration d{
                (static_cast<int64_t>(creationTime.dwHighDateTime) << 32) | creationTime.dwLowDateTime};
            file_clock::time_point creation_time{d};

            // get duration
            auto uptime = file_clock::now() - creation_time;
            auto uptime_sec = duration_cast<seconds>(uptime);
            r = uptime_sec.count();
    return r;

Hey, I really appreciate your interest in my work. If you want to reach out to me directly, you can send me an email anytime. I'd love to hear from you. But if you have something to say that others might benefit from, feel free to leave a comment below. I'll try to reply as soon as I can. Like what I do? Please: