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


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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can call 'GetLastInputInfo()' (http://msdn.microsoft.com/en-us/library/ms646302(VS.85).aspx) to accomplish this. See e.g. http://www.codeproject.com/KB/system/rtwidledll.aspx ("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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
the source code you are looking for is avilable in
sorry wrong url for this question
deshawAuthor Commented:
jrk, The code is not working properly. It seems the output is not in milliseconds.
deshawAuthor Commented:
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.

from http://msdn.microsoft.com/en-us/library/ms646302(VS.85).aspx
"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."

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.