Is it possible to detect that a log-off has been aborted on windows?












0















I'm wondering if it's possible to programmatically restart an application that has been terminated in the beginning of a shutdown that will be canceled later.

On Windows, if an application calls ShutdownBlockReasonCreate on shutdown , it can know whether the user has cancelled it or not, by checking if the ENDSESSION_CRITICAL bit is set in the lParam.

In my case I don't want to block any shutdown .



Is this possible ? Thanks for any suggestion.










share|improve this question

























  • Why the c++ tag?

    – abhishek_naik
    Nov 20 '18 at 23:26











  • A bit kludgy, but you could use Task Scheduler to create a task with a specific time trigger of (say) 20 seconds in the future.

    – Jonathan Potter
    Nov 20 '18 at 23:52











  • @abhishek_naik, I used C++ tag so as to not have solutions for C#, VB

    – perrahmaouy
    Nov 21 '18 at 0:12











  • @JonathanPotter, creating a task in Task Scheduler requires admin privileges, which I don't have, and don't want to ask for.

    – perrahmaouy
    Nov 21 '18 at 0:14













  • @perrahmaouy no, any user can schedule a task to run as themselves.

    – Jonathan Potter
    Nov 21 '18 at 0:29
















0















I'm wondering if it's possible to programmatically restart an application that has been terminated in the beginning of a shutdown that will be canceled later.

On Windows, if an application calls ShutdownBlockReasonCreate on shutdown , it can know whether the user has cancelled it or not, by checking if the ENDSESSION_CRITICAL bit is set in the lParam.

In my case I don't want to block any shutdown .



Is this possible ? Thanks for any suggestion.










share|improve this question

























  • Why the c++ tag?

    – abhishek_naik
    Nov 20 '18 at 23:26











  • A bit kludgy, but you could use Task Scheduler to create a task with a specific time trigger of (say) 20 seconds in the future.

    – Jonathan Potter
    Nov 20 '18 at 23:52











  • @abhishek_naik, I used C++ tag so as to not have solutions for C#, VB

    – perrahmaouy
    Nov 21 '18 at 0:12











  • @JonathanPotter, creating a task in Task Scheduler requires admin privileges, which I don't have, and don't want to ask for.

    – perrahmaouy
    Nov 21 '18 at 0:14













  • @perrahmaouy no, any user can schedule a task to run as themselves.

    – Jonathan Potter
    Nov 21 '18 at 0:29














0












0








0








I'm wondering if it's possible to programmatically restart an application that has been terminated in the beginning of a shutdown that will be canceled later.

On Windows, if an application calls ShutdownBlockReasonCreate on shutdown , it can know whether the user has cancelled it or not, by checking if the ENDSESSION_CRITICAL bit is set in the lParam.

In my case I don't want to block any shutdown .



Is this possible ? Thanks for any suggestion.










share|improve this question
















I'm wondering if it's possible to programmatically restart an application that has been terminated in the beginning of a shutdown that will be canceled later.

On Windows, if an application calls ShutdownBlockReasonCreate on shutdown , it can know whether the user has cancelled it or not, by checking if the ENDSESSION_CRITICAL bit is set in the lParam.

In my case I don't want to block any shutdown .



Is this possible ? Thanks for any suggestion.







winapi windows-vista shutdown






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 17:11







perrahmaouy

















asked Nov 20 '18 at 23:25









perrahmaouyperrahmaouy

84




84













  • Why the c++ tag?

    – abhishek_naik
    Nov 20 '18 at 23:26











  • A bit kludgy, but you could use Task Scheduler to create a task with a specific time trigger of (say) 20 seconds in the future.

    – Jonathan Potter
    Nov 20 '18 at 23:52











  • @abhishek_naik, I used C++ tag so as to not have solutions for C#, VB

    – perrahmaouy
    Nov 21 '18 at 0:12











  • @JonathanPotter, creating a task in Task Scheduler requires admin privileges, which I don't have, and don't want to ask for.

    – perrahmaouy
    Nov 21 '18 at 0:14













  • @perrahmaouy no, any user can schedule a task to run as themselves.

    – Jonathan Potter
    Nov 21 '18 at 0:29



















  • Why the c++ tag?

    – abhishek_naik
    Nov 20 '18 at 23:26











  • A bit kludgy, but you could use Task Scheduler to create a task with a specific time trigger of (say) 20 seconds in the future.

    – Jonathan Potter
    Nov 20 '18 at 23:52











  • @abhishek_naik, I used C++ tag so as to not have solutions for C#, VB

    – perrahmaouy
    Nov 21 '18 at 0:12











  • @JonathanPotter, creating a task in Task Scheduler requires admin privileges, which I don't have, and don't want to ask for.

    – perrahmaouy
    Nov 21 '18 at 0:14













  • @perrahmaouy no, any user can schedule a task to run as themselves.

    – Jonathan Potter
    Nov 21 '18 at 0:29

















Why the c++ tag?

– abhishek_naik
Nov 20 '18 at 23:26





Why the c++ tag?

– abhishek_naik
Nov 20 '18 at 23:26













A bit kludgy, but you could use Task Scheduler to create a task with a specific time trigger of (say) 20 seconds in the future.

– Jonathan Potter
Nov 20 '18 at 23:52





A bit kludgy, but you could use Task Scheduler to create a task with a specific time trigger of (say) 20 seconds in the future.

– Jonathan Potter
Nov 20 '18 at 23:52













@abhishek_naik, I used C++ tag so as to not have solutions for C#, VB

– perrahmaouy
Nov 21 '18 at 0:12





@abhishek_naik, I used C++ tag so as to not have solutions for C#, VB

– perrahmaouy
Nov 21 '18 at 0:12













@JonathanPotter, creating a task in Task Scheduler requires admin privileges, which I don't have, and don't want to ask for.

– perrahmaouy
Nov 21 '18 at 0:14







@JonathanPotter, creating a task in Task Scheduler requires admin privileges, which I don't have, and don't want to ask for.

– perrahmaouy
Nov 21 '18 at 0:14















@perrahmaouy no, any user can schedule a task to run as themselves.

– Jonathan Potter
Nov 21 '18 at 0:29





@perrahmaouy no, any user can schedule a task to run as themselves.

– Jonathan Potter
Nov 21 '18 at 0:29












1 Answer
1






active

oldest

votes


















0














I think I found a solution :



I created a hidden window using CreateWindowEx to handle the WM_QUERYENDSESSION and WM_ENDSESSION messages:



LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUERYENDSESSION:
{
//clean up ...

ShutdownBlockReasonCreate(hwnd, L" ");
// What ? You said you don't want to block the shutdown ?
// Yes I do. It seems to work different with a hidden window.

return 1;
}
case WM_ENDSESSION:
{
// GetSystemMetrics(SM_SHUTTINGDOWN) return Nonzero if the current session is shutting down
// This loop waits for the user to cancel the shutdown
while(GetSystemMetrics(SM_SHUTTINGDOWN) != 0)
Sleep(100);
// The user has cancelled the shutdown here
// I can run the process again using CreateProcess or what ever...
// Once this line is executed this instance will be terminated

return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}


The return values are very important (see this).

As I didn't expected, a process without a top level visible window, even if it calls ShutdownBlockReasonCreate (with a hidden window handle), it will not be shown in the list of applications blocking the shutdown. And if there's no other application blocking the shutdown, Windows will only wait for a few seconds, and then shutdown.



For the loop, I took the idea from here. I don't know if there's a better alternative.



That's it.



Please test it, and correct me if I'm wrong.
Thanks.



[EDIT]
Here's the program main for a ready test:



#include <string>
#include <Windows.h>

using namespace std;

string getExePath()
{
TCHAR wpath[MAX_PATH];
GetModuleFileName (NULL, wpath, MAX_PATH);
size_t i;
char path[MAX_PATH];
wcstombs_s(&i, path, MAX_PATH, wpath, MAX_PATH);

return path;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUERYENDSESSION:
{
// A shutdown has been initiated
ShutdownBlockReasonCreate(hwnd, L" ");

return 1;
}
case WM_ENDSESSION:
{
// GetSystemMetrics(SM_SHUTTINGDOWN) return Nonzero if the current session is shutting down
// This loop waits for the user to cancel the shutdown

while(GetSystemMetrics(SM_SHUTTINGDOWN) != 0)
Sleep(100);

// The user has cancelled the shutdown here
// I can run the process again

WinExec(string(""" + getExePath() + """).c_str(), SW_HIDE);

TerminateProcess(OpenProcess(PROCESS_TERMINATE, FALSE, GetCurrentProcessId()), 0);

return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
WNDCLASSEXA wc;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "TestShutDownAbortClass";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if( RegisterClassExA(&wc) == 0 )
return 1;

hwnd = CreateWindowExA(0,"TestShutDownAbortClass", "", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
return 1;

MSG msg;
while(GetMessageA(&msg, hwnd, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}


return 0;
}





share|improve this answer


























  • And if the user does not cancel shutdown, or force a shutdown? Your WM_ENDSESSION will loop indefinitely because of your call to ShutdownBlockReasonCreate(), as the system will not terminate the app automatically because it is waiting for it to call ShutdownBlockReasonDestroy() to signal terminate/shutdown is safe to continue, but you don't do that. "Applications should call [ShutdownBlockReasonCreate()] as they begin an operation that cannot be interrupted, such as burning a CD or DVD."

    – Remy Lebeau
    Nov 21 '18 at 19:38













  • I tested it, even if I don't call ShutdownBlockReasonDestroy, Windows shuts down the computer without forcing it , because the I'm not calling ShutdownBlockReasonCreate from a top level visible window. So Windows give it about 30 seconds I think or maybe less, and then it shuts down.

    – perrahmaouy
    Nov 21 '18 at 21:38











  • @RemyLebeau, did you tested the code above ?

    – perrahmaouy
    Nov 21 '18 at 21:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403136%2fis-it-possible-to-detect-that-a-log-off-has-been-aborted-on-windows%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I think I found a solution :



I created a hidden window using CreateWindowEx to handle the WM_QUERYENDSESSION and WM_ENDSESSION messages:



LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUERYENDSESSION:
{
//clean up ...

ShutdownBlockReasonCreate(hwnd, L" ");
// What ? You said you don't want to block the shutdown ?
// Yes I do. It seems to work different with a hidden window.

return 1;
}
case WM_ENDSESSION:
{
// GetSystemMetrics(SM_SHUTTINGDOWN) return Nonzero if the current session is shutting down
// This loop waits for the user to cancel the shutdown
while(GetSystemMetrics(SM_SHUTTINGDOWN) != 0)
Sleep(100);
// The user has cancelled the shutdown here
// I can run the process again using CreateProcess or what ever...
// Once this line is executed this instance will be terminated

return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}


The return values are very important (see this).

As I didn't expected, a process without a top level visible window, even if it calls ShutdownBlockReasonCreate (with a hidden window handle), it will not be shown in the list of applications blocking the shutdown. And if there's no other application blocking the shutdown, Windows will only wait for a few seconds, and then shutdown.



For the loop, I took the idea from here. I don't know if there's a better alternative.



That's it.



Please test it, and correct me if I'm wrong.
Thanks.



[EDIT]
Here's the program main for a ready test:



#include <string>
#include <Windows.h>

using namespace std;

string getExePath()
{
TCHAR wpath[MAX_PATH];
GetModuleFileName (NULL, wpath, MAX_PATH);
size_t i;
char path[MAX_PATH];
wcstombs_s(&i, path, MAX_PATH, wpath, MAX_PATH);

return path;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUERYENDSESSION:
{
// A shutdown has been initiated
ShutdownBlockReasonCreate(hwnd, L" ");

return 1;
}
case WM_ENDSESSION:
{
// GetSystemMetrics(SM_SHUTTINGDOWN) return Nonzero if the current session is shutting down
// This loop waits for the user to cancel the shutdown

while(GetSystemMetrics(SM_SHUTTINGDOWN) != 0)
Sleep(100);

// The user has cancelled the shutdown here
// I can run the process again

WinExec(string(""" + getExePath() + """).c_str(), SW_HIDE);

TerminateProcess(OpenProcess(PROCESS_TERMINATE, FALSE, GetCurrentProcessId()), 0);

return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
WNDCLASSEXA wc;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "TestShutDownAbortClass";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if( RegisterClassExA(&wc) == 0 )
return 1;

hwnd = CreateWindowExA(0,"TestShutDownAbortClass", "", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
return 1;

MSG msg;
while(GetMessageA(&msg, hwnd, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}


return 0;
}





share|improve this answer


























  • And if the user does not cancel shutdown, or force a shutdown? Your WM_ENDSESSION will loop indefinitely because of your call to ShutdownBlockReasonCreate(), as the system will not terminate the app automatically because it is waiting for it to call ShutdownBlockReasonDestroy() to signal terminate/shutdown is safe to continue, but you don't do that. "Applications should call [ShutdownBlockReasonCreate()] as they begin an operation that cannot be interrupted, such as burning a CD or DVD."

    – Remy Lebeau
    Nov 21 '18 at 19:38













  • I tested it, even if I don't call ShutdownBlockReasonDestroy, Windows shuts down the computer without forcing it , because the I'm not calling ShutdownBlockReasonCreate from a top level visible window. So Windows give it about 30 seconds I think or maybe less, and then it shuts down.

    – perrahmaouy
    Nov 21 '18 at 21:38











  • @RemyLebeau, did you tested the code above ?

    – perrahmaouy
    Nov 21 '18 at 21:40
















0














I think I found a solution :



I created a hidden window using CreateWindowEx to handle the WM_QUERYENDSESSION and WM_ENDSESSION messages:



LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUERYENDSESSION:
{
//clean up ...

ShutdownBlockReasonCreate(hwnd, L" ");
// What ? You said you don't want to block the shutdown ?
// Yes I do. It seems to work different with a hidden window.

return 1;
}
case WM_ENDSESSION:
{
// GetSystemMetrics(SM_SHUTTINGDOWN) return Nonzero if the current session is shutting down
// This loop waits for the user to cancel the shutdown
while(GetSystemMetrics(SM_SHUTTINGDOWN) != 0)
Sleep(100);
// The user has cancelled the shutdown here
// I can run the process again using CreateProcess or what ever...
// Once this line is executed this instance will be terminated

return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}


The return values are very important (see this).

As I didn't expected, a process without a top level visible window, even if it calls ShutdownBlockReasonCreate (with a hidden window handle), it will not be shown in the list of applications blocking the shutdown. And if there's no other application blocking the shutdown, Windows will only wait for a few seconds, and then shutdown.



For the loop, I took the idea from here. I don't know if there's a better alternative.



That's it.



Please test it, and correct me if I'm wrong.
Thanks.



[EDIT]
Here's the program main for a ready test:



#include <string>
#include <Windows.h>

using namespace std;

string getExePath()
{
TCHAR wpath[MAX_PATH];
GetModuleFileName (NULL, wpath, MAX_PATH);
size_t i;
char path[MAX_PATH];
wcstombs_s(&i, path, MAX_PATH, wpath, MAX_PATH);

return path;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUERYENDSESSION:
{
// A shutdown has been initiated
ShutdownBlockReasonCreate(hwnd, L" ");

return 1;
}
case WM_ENDSESSION:
{
// GetSystemMetrics(SM_SHUTTINGDOWN) return Nonzero if the current session is shutting down
// This loop waits for the user to cancel the shutdown

while(GetSystemMetrics(SM_SHUTTINGDOWN) != 0)
Sleep(100);

// The user has cancelled the shutdown here
// I can run the process again

WinExec(string(""" + getExePath() + """).c_str(), SW_HIDE);

TerminateProcess(OpenProcess(PROCESS_TERMINATE, FALSE, GetCurrentProcessId()), 0);

return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
WNDCLASSEXA wc;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "TestShutDownAbortClass";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if( RegisterClassExA(&wc) == 0 )
return 1;

hwnd = CreateWindowExA(0,"TestShutDownAbortClass", "", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
return 1;

MSG msg;
while(GetMessageA(&msg, hwnd, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}


return 0;
}





share|improve this answer


























  • And if the user does not cancel shutdown, or force a shutdown? Your WM_ENDSESSION will loop indefinitely because of your call to ShutdownBlockReasonCreate(), as the system will not terminate the app automatically because it is waiting for it to call ShutdownBlockReasonDestroy() to signal terminate/shutdown is safe to continue, but you don't do that. "Applications should call [ShutdownBlockReasonCreate()] as they begin an operation that cannot be interrupted, such as burning a CD or DVD."

    – Remy Lebeau
    Nov 21 '18 at 19:38













  • I tested it, even if I don't call ShutdownBlockReasonDestroy, Windows shuts down the computer without forcing it , because the I'm not calling ShutdownBlockReasonCreate from a top level visible window. So Windows give it about 30 seconds I think or maybe less, and then it shuts down.

    – perrahmaouy
    Nov 21 '18 at 21:38











  • @RemyLebeau, did you tested the code above ?

    – perrahmaouy
    Nov 21 '18 at 21:40














0












0








0







I think I found a solution :



I created a hidden window using CreateWindowEx to handle the WM_QUERYENDSESSION and WM_ENDSESSION messages:



LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUERYENDSESSION:
{
//clean up ...

ShutdownBlockReasonCreate(hwnd, L" ");
// What ? You said you don't want to block the shutdown ?
// Yes I do. It seems to work different with a hidden window.

return 1;
}
case WM_ENDSESSION:
{
// GetSystemMetrics(SM_SHUTTINGDOWN) return Nonzero if the current session is shutting down
// This loop waits for the user to cancel the shutdown
while(GetSystemMetrics(SM_SHUTTINGDOWN) != 0)
Sleep(100);
// The user has cancelled the shutdown here
// I can run the process again using CreateProcess or what ever...
// Once this line is executed this instance will be terminated

return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}


The return values are very important (see this).

As I didn't expected, a process without a top level visible window, even if it calls ShutdownBlockReasonCreate (with a hidden window handle), it will not be shown in the list of applications blocking the shutdown. And if there's no other application blocking the shutdown, Windows will only wait for a few seconds, and then shutdown.



For the loop, I took the idea from here. I don't know if there's a better alternative.



That's it.



Please test it, and correct me if I'm wrong.
Thanks.



[EDIT]
Here's the program main for a ready test:



#include <string>
#include <Windows.h>

using namespace std;

string getExePath()
{
TCHAR wpath[MAX_PATH];
GetModuleFileName (NULL, wpath, MAX_PATH);
size_t i;
char path[MAX_PATH];
wcstombs_s(&i, path, MAX_PATH, wpath, MAX_PATH);

return path;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUERYENDSESSION:
{
// A shutdown has been initiated
ShutdownBlockReasonCreate(hwnd, L" ");

return 1;
}
case WM_ENDSESSION:
{
// GetSystemMetrics(SM_SHUTTINGDOWN) return Nonzero if the current session is shutting down
// This loop waits for the user to cancel the shutdown

while(GetSystemMetrics(SM_SHUTTINGDOWN) != 0)
Sleep(100);

// The user has cancelled the shutdown here
// I can run the process again

WinExec(string(""" + getExePath() + """).c_str(), SW_HIDE);

TerminateProcess(OpenProcess(PROCESS_TERMINATE, FALSE, GetCurrentProcessId()), 0);

return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
WNDCLASSEXA wc;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "TestShutDownAbortClass";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if( RegisterClassExA(&wc) == 0 )
return 1;

hwnd = CreateWindowExA(0,"TestShutDownAbortClass", "", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
return 1;

MSG msg;
while(GetMessageA(&msg, hwnd, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}


return 0;
}





share|improve this answer















I think I found a solution :



I created a hidden window using CreateWindowEx to handle the WM_QUERYENDSESSION and WM_ENDSESSION messages:



LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUERYENDSESSION:
{
//clean up ...

ShutdownBlockReasonCreate(hwnd, L" ");
// What ? You said you don't want to block the shutdown ?
// Yes I do. It seems to work different with a hidden window.

return 1;
}
case WM_ENDSESSION:
{
// GetSystemMetrics(SM_SHUTTINGDOWN) return Nonzero if the current session is shutting down
// This loop waits for the user to cancel the shutdown
while(GetSystemMetrics(SM_SHUTTINGDOWN) != 0)
Sleep(100);
// The user has cancelled the shutdown here
// I can run the process again using CreateProcess or what ever...
// Once this line is executed this instance will be terminated

return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}


The return values are very important (see this).

As I didn't expected, a process without a top level visible window, even if it calls ShutdownBlockReasonCreate (with a hidden window handle), it will not be shown in the list of applications blocking the shutdown. And if there's no other application blocking the shutdown, Windows will only wait for a few seconds, and then shutdown.



For the loop, I took the idea from here. I don't know if there's a better alternative.



That's it.



Please test it, and correct me if I'm wrong.
Thanks.



[EDIT]
Here's the program main for a ready test:



#include <string>
#include <Windows.h>

using namespace std;

string getExePath()
{
TCHAR wpath[MAX_PATH];
GetModuleFileName (NULL, wpath, MAX_PATH);
size_t i;
char path[MAX_PATH];
wcstombs_s(&i, path, MAX_PATH, wpath, MAX_PATH);

return path;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUERYENDSESSION:
{
// A shutdown has been initiated
ShutdownBlockReasonCreate(hwnd, L" ");

return 1;
}
case WM_ENDSESSION:
{
// GetSystemMetrics(SM_SHUTTINGDOWN) return Nonzero if the current session is shutting down
// This loop waits for the user to cancel the shutdown

while(GetSystemMetrics(SM_SHUTTINGDOWN) != 0)
Sleep(100);

// The user has cancelled the shutdown here
// I can run the process again

WinExec(string(""" + getExePath() + """).c_str(), SW_HIDE);

TerminateProcess(OpenProcess(PROCESS_TERMINATE, FALSE, GetCurrentProcessId()), 0);

return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
WNDCLASSEXA wc;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "TestShutDownAbortClass";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if( RegisterClassExA(&wc) == 0 )
return 1;

hwnd = CreateWindowExA(0,"TestShutDownAbortClass", "", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
return 1;

MSG msg;
while(GetMessageA(&msg, hwnd, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}


return 0;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 22:49

























answered Nov 21 '18 at 1:52









perrahmaouyperrahmaouy

84




84













  • And if the user does not cancel shutdown, or force a shutdown? Your WM_ENDSESSION will loop indefinitely because of your call to ShutdownBlockReasonCreate(), as the system will not terminate the app automatically because it is waiting for it to call ShutdownBlockReasonDestroy() to signal terminate/shutdown is safe to continue, but you don't do that. "Applications should call [ShutdownBlockReasonCreate()] as they begin an operation that cannot be interrupted, such as burning a CD or DVD."

    – Remy Lebeau
    Nov 21 '18 at 19:38













  • I tested it, even if I don't call ShutdownBlockReasonDestroy, Windows shuts down the computer without forcing it , because the I'm not calling ShutdownBlockReasonCreate from a top level visible window. So Windows give it about 30 seconds I think or maybe less, and then it shuts down.

    – perrahmaouy
    Nov 21 '18 at 21:38











  • @RemyLebeau, did you tested the code above ?

    – perrahmaouy
    Nov 21 '18 at 21:40



















  • And if the user does not cancel shutdown, or force a shutdown? Your WM_ENDSESSION will loop indefinitely because of your call to ShutdownBlockReasonCreate(), as the system will not terminate the app automatically because it is waiting for it to call ShutdownBlockReasonDestroy() to signal terminate/shutdown is safe to continue, but you don't do that. "Applications should call [ShutdownBlockReasonCreate()] as they begin an operation that cannot be interrupted, such as burning a CD or DVD."

    – Remy Lebeau
    Nov 21 '18 at 19:38













  • I tested it, even if I don't call ShutdownBlockReasonDestroy, Windows shuts down the computer without forcing it , because the I'm not calling ShutdownBlockReasonCreate from a top level visible window. So Windows give it about 30 seconds I think or maybe less, and then it shuts down.

    – perrahmaouy
    Nov 21 '18 at 21:38











  • @RemyLebeau, did you tested the code above ?

    – perrahmaouy
    Nov 21 '18 at 21:40

















And if the user does not cancel shutdown, or force a shutdown? Your WM_ENDSESSION will loop indefinitely because of your call to ShutdownBlockReasonCreate(), as the system will not terminate the app automatically because it is waiting for it to call ShutdownBlockReasonDestroy() to signal terminate/shutdown is safe to continue, but you don't do that. "Applications should call [ShutdownBlockReasonCreate()] as they begin an operation that cannot be interrupted, such as burning a CD or DVD."

– Remy Lebeau
Nov 21 '18 at 19:38







And if the user does not cancel shutdown, or force a shutdown? Your WM_ENDSESSION will loop indefinitely because of your call to ShutdownBlockReasonCreate(), as the system will not terminate the app automatically because it is waiting for it to call ShutdownBlockReasonDestroy() to signal terminate/shutdown is safe to continue, but you don't do that. "Applications should call [ShutdownBlockReasonCreate()] as they begin an operation that cannot be interrupted, such as burning a CD or DVD."

– Remy Lebeau
Nov 21 '18 at 19:38















I tested it, even if I don't call ShutdownBlockReasonDestroy, Windows shuts down the computer without forcing it , because the I'm not calling ShutdownBlockReasonCreate from a top level visible window. So Windows give it about 30 seconds I think or maybe less, and then it shuts down.

– perrahmaouy
Nov 21 '18 at 21:38





I tested it, even if I don't call ShutdownBlockReasonDestroy, Windows shuts down the computer without forcing it , because the I'm not calling ShutdownBlockReasonCreate from a top level visible window. So Windows give it about 30 seconds I think or maybe less, and then it shuts down.

– perrahmaouy
Nov 21 '18 at 21:38













@RemyLebeau, did you tested the code above ?

– perrahmaouy
Nov 21 '18 at 21:40





@RemyLebeau, did you tested the code above ?

– perrahmaouy
Nov 21 '18 at 21:40




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403136%2fis-it-possible-to-detect-that-a-log-off-has-been-aborted-on-windows%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini