Note that there are some explanatory texts on larger screens.

plurals
  1. POUse ShutdownBlockRequestCreate in Win32 Console application
    primarykey
    data
    text
    <p><strong>What is the proper method of blocking premature termination of a Win32 Console Application running on Windows 7?</strong></p> <p>When Vista was introduced, there were changes regarding how <a href="http://msdn.microsoft.com/en-us/library/ms700677%28v=vs.85%29.aspx" rel="nofollow">Application Shutdown</a> happened. Contrary to the behavior in XP, which was to open up a UI requesting whether the user wants to force close or not, Windows Vista (and 7) terminates the process if nothing is done programmatically to prevent it. Console apps and applications without a top level window visible must also use the new function <a href="http://msdn.microsoft.com/en-us/library/aa376877%28v=VS.85%29.aspx" rel="nofollow">ShutdownBlockRequestCreate</a> to provide a reason for Vista to show in the UI that pops up or it will terminate the program after 5 seconds anyway.</p> <p>Below is my attempt at using the ShutdownBlockRequestCreate function in a Win32 Console application; the precompiled header option was removed from the project after creation by the wizard. I get the error code of 5, corresponding to ERROR_ACCESS_DENIED, whenever I use the function. This is apparently (according to the <a href="http://msdn.microsoft.com/en-us/library/ms700677%28v=vs.85%29.aspx" rel="nofollow">Application Shutdown link</a>) because I am not calling the function from the same thread as was used to create the window (the console window).</p> <pre><code>#include &lt;iostream&gt; #include &lt;tchar.h&gt; #include &lt;conio.h&gt; #include &lt;windows.h&gt; typedef BOOL (WINAPI *SBRCREATEFUNC)(HWND,LPCWSTR); void RegisterShutdownBlockReason() { SBRCREATEFUNC ShutdownBlockReasonCreate; HWND hWnd = GetForegroundWindow(); HINSTANCE hinstLib = LoadLibrary(TEXT("user32.dll")); if (hinstLib != NULL) { ShutdownBlockReasonCreate = (SBRCREATEFUNC) GetProcAddress(hinstLib,"ShutdownBlockReasonCreate"); if(ShutdownBlockReasonCreate != NULL) { if(!(ShutdownBlockReasonCreate) (hWnd, L"Terminating Communication Sessions")) { printf("\nfailed To Register Reason, failure code: %d\n", GetLastError()); } else { printf("\nRegistered Reason\n"); } } else { printf("\nCouldn't load ShutdownBlockReasonCreate procedure\n"); } } else { printf("\nFailed to LoadLibrary(\"user32.dll\")\n"); } } int _tmain(int argc, _TCHAR* argv[]) { RegisterShutdownBlockReason(); printf("Type to terminate program.\n"); getch(); return 0; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload