简体   繁体   中英

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++.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM