JChan
JChan

Reputation: 1451

How to capture windows shut down/ Restart message using C++

I want to check in my application programmatically if user shutdown/restart/logoff the computer. I tried to implement the below code and it giving the compilation error

error: invalid conversion from 'bool (*)(DWORD)' to 'BOOL (*)(DWORD)'
error: initializing argument 1 of 'BOOL SetConsoleCtrlHandler(BOOL (*)(DWORD), BOOL)'

void TestApp:: OnQuit()
{
    SetConsoleCtrlHandler(HandlerRoutine, TRUE);
}

//Windows Call Back function implementation
bool WINAPI HandlerRoutine(DWORD dwCtrlType)
{
   bool ret = false;    
   if (dwCtrlType == CTRL_LOGOFF_EVENT || dwCtrlType == CTRL_SHUTDOWN_EVENT)
      //Graceful Quit

   return ret;
}

My devlopement environment is QT Creator QT SDK and C++.

Upvotes: 0

Views: 928

Answers (1)

Wes Miller
Wes Miller

Reputation: 2241

As others have said, a BOOL is an int, not a bool. A bool has nominal values of true and false, 1 and 0. A BOOL uses FALSE == 0 and TRUE == !FALSE. Mostly of no matter here since they essentially work the same way.

The BOOL comes from the old heritage of WinAPI when C didn't have a built-in bool type.

So, all you really need to do is change bool to BOOL in you handler:

BOOL WINAPI HandlerRoutine(DWORD dwCtrlType)
^^^^
{
   BOOL ret = false;    
   ^^^^
   if (dwCtrlType == CTRL_LOGOFF_EVENT || dwCtrlType == CTRL_SHUTDOWN_EVENT)
      //Graceful Quit

   return ret;
}

Upvotes: 1

Related Questions