5
votes

How can I add custom text to shutdown screen, like those messages that show when Windows is installing updates before shutting down? For example, you have a backup script that is executed on shutdown, and you want to inform about the progress of the backup just like Windows does when installing updates. Is there any command line tool for that, or some code library, or even something in Windows API?

Note that this is not about how to shutdown a computer, and it is not about whatever way to display a message there in shutdown screen, such as console applications or message boxes. This is not about customizing existing messages either, and it is not about any shutdown dialog that shows before shutdown screen and allows the user to cancel the shutdown or proceed without waiting for the programs to terminate.

This is about understanding how Windows implements the displaying of those messages the way they are displayed there in shutdown, and how to add new messages to be displayed, preferably with progress information. To be clear, below is a screenshot.

Shutdown screen

2
ShutdownBlockReasonCreate lets you add a custom message.Raymond Chen
ShutdownBlockReasonCreate has nothing to do with what was asked, see its documentation and read the question again.user1726010
That function displays the message on the shutdown screen itself (as opposed to in a console application or a message box), which seems to be what was requested. I guess you could encode a progress counter in the message string. What you cannot do is force shutdown to wait until your background process is complete. If the user says "Shut down anyway" then the system will shut down anyway.Raymond Chen
By the time that screen appears applications have been terminated. Even if you could display a message there it wouldn't help because your app is already deadRaymond Chen
You cannot do it the way updates does. That is not available for applications. People noted other options but you don't seem to like them.Raymond Chen

2 Answers

0
votes

There is a function WmsgPostNotifyMessage in wmsgapi.dll which is displaying this message. Undocumented though, but shouldn't be a problem to use.

-1
votes

Here is a C++ code that can shutdown the computer with message.

#include <windows.h>

#pragma comment( lib, "advapi32.lib" )

BOOL MySystemShutdown( LPTSTR lpMsg )
{
   HANDLE hToken;              // handle to process token 
   TOKEN_PRIVILEGES tkp;       // pointer to token structure 

   BOOL fResult;               // system shutdown flag 

   // Get the current process token handle so we can get shutdown 
   // privilege. 

   if (!OpenProcessToken(GetCurrentProcess(), 
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
          return FALSE; 

   // Get the LUID for shutdown privilege. 

   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, 
        &tkp.Privileges[0].Luid); 

   tkp.PrivilegeCount = 1;  // one privilege to set    
   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 

   // Get shutdown privilege for this process. 

   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
  (PTOKEN_PRIVILEGES) NULL, 0); 

   // Cannot test the return value of AdjustTokenPrivileges. 

   if (GetLastError() != ERROR_SUCCESS) 
      return FALSE; 

   // Display the shutdown dialog box and start the countdown. 

   fResult = InitiateSystemShutdown( 
      NULL,    // shut down local computer 
      lpMsg,   // message for user
      30,      // time-out period, in seconds 
      FALSE,   // ask user to close apps 
      TRUE);   // reboot after shutdown 

   if (!fResult) 
      return FALSE; 

   // Disable shutdown privilege. 

   tkp.Privileges[0].Attributes = 0; 
   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
    (PTOKEN_PRIVILEGES) NULL, 0); 

   return TRUE; 
}