C/C++ : How to find the idle time for local windows machine

Posted on 2008-11-19
Last Modified: 2012-05-05

Could anyone please tell me how could I find the my machine's idle time using C/C++? Basically I am building client-server application where I will need to prompt server side user if his/her machine is not idle for 5 minutes. so when connection request received on my Server service, it should first check whether machine is idle for 5 minutes and if not then only it will prompt the dialog else not.

Question by:deshaw
    LVL 86

    Accepted Solution

    You can call 'GetLastInputInfo()' ( to accomplish this. See e.g. ("RtwIdleDll Control - DLL to track the system idle time"). The scoop would be to
    //returns the time from system startup to last input in milliseconds
    DWORD RtwIdleDll::RtwIdleDllControl::GetLastInputTime(void)
        LASTINPUTINFO lastInput;
        lastInput.cbSize = sizeof(LASTINPUTINFO);
        BOOL success = GetLastInputInfo(&lastInput);
            DWORD err=GetLastError();
            // report error, throw exception, etc
        DWORD lastInputTime = lastInput.dwTime;
        return lastInputTime;

    Open in new window

    LVL 3

    Expert Comment

    the source code you are looking for is avilable in
    LVL 3

    Expert Comment

    sorry wrong url for this question
    LVL 1

    Author Comment

    jrk, The code is not working properly. It seems the output is not in milliseconds.
    LVL 1

    Author Comment

    While researching on GetLastInputInfo, I found that it returns session wise idle time and not system wide. Could you tell me how to get system wide idle time.

    Icreased the points for this.

    "This function is useful for input idle detection. However, GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function."


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now