How to stop the computer from shutting down within OF?

my program has some clear up to do before exiting. so i put those codes in exit(). when Esc is pressed, exit() is evoked correctly. however, when the computer is turned off directly by pressing the power button, exit() is not evoked. is there a way to stop the computer from closing? just like that, a dialogue pop up asking you to save your txt file before the computer shuts down?
p.s. i’m on windows8.1 and i hope to find a way for both windows7 and windows8. thanks!

Quick google search showed ShutdownBlockReasonCreate. Apparently you need the hWnd window handle from OF. I believe there was a way of retreiving it… not sure though.

Hope it helps.

you can retrieve it with:

HWND window = ofGetWin32Window();

after google search, i think we need both ShutdownBlockReasonCreate and capture WM_QUERYENDSESSION message. but i don’t know how to capture the WM_QUERYENDSESSION message. i found some examples about this. but they are all for win32 form application.

Probably the best way is to modify GLFW to handle this message :

Other option is not create a second window that you won’t show and that handle the message for you. More hacky and might break if windows kills your app before trying to kill the second window.

thank you silverbahamut! by modifying GLFW i finally make it. for other people’s reference:

(1) if you simply want to prevent the computer from shutting down, do the following:
[a] modify GLFW’s win32_window.c, in static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam), add

case WM_QUERYENDSESSION:
{
       return FALSE;
}

[b] in setup() of testApp.cpp, add

HWND window = ofGetWin32Window();
if (ShutdownBlockReasonCreate(window, _T("do not shutdown")))
{
      std::cout<<"ShutdownBlockReasonCreate"<<std::endl;
}

(2) if you want to do some clean up before the computer shut down, do the following:
[a] modify GLFW’s win32_window.c, in static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam), add

case WM_QUERYENDSESSION:
{
       _glfwInputWindowCloseRequest(window);
       return FALSE;
}

[b] in setup() of testApp.cpp, add

HWND window = ofGetWin32Window();
if (ShutdownBlockReasonCreate(window, _T("do not shutdown")))
{
      std::cout<<"ShutdownBlockReasonCreate"<<std::endl;
}

[c] in exit() of testApp.cpp, add the clean up codes.

2 Likes

Thanks for sharing :slight_smile: !