Windows will not be disabled programmatically when you log off

I have an application that runs from a service using a local administrator account. This application is accessible through a web browser and the host computer can be shut down through this interface.

If the user is logged into the host machine and I navigate to it and close it, the application exits and shuts down the PC as I expected - using ExitWindowsEx () (with shutdown privilege enabled).

If, however, the computer is disconnected, I look at it - the application is still running in the service and tries to shutdown with ExitWindowsEx (), it returns successful and it doesn't seem to be a problem, but the PC never shuts down.

I also tried InitiateSystemShutdown () which bizzarely fails and returns error 2! (The system cannot find the file specified.)

It doesn't seem to matter which account I use to run the app.

Any help would be greatly appreciated!

Thanks.

+1


a source to share


4 answers


Unfortunately not reproducible. I have a pre-existing service that provides a mailbox, so I added the code:

void RebootThisMachine ()
{
   if (GrabPrivilege (SE_SHUTDOWN_NAME))
   {
      if (!InitiateSystemShutdown (NULL,NULL,0,TRUE,TRUE))
      {
         wsprintf (g_szDebug, TEXT("RebootMachine - ISS failed, error %d"),
                   GetLastError()) ;
         DebugMessage (DEBUG_ERROR, g_szDebug) ;
      }
   }
   else
   {
      wsprintf (g_szDebug, TEXT("RebootMachine - cannot grab priv, error %d"),
                GetLastError()) ;
      DebugMessage (DEBUG_ERROR, g_szDebug) ;
   }
}

      



and called it when I got a Mailslot message from a little command line utility I wrote. InitiateSystemShutdown is the correct API for the service and it reboots the machine the service is running on, whether it is logged in or not. Shutting down takes some time if my machine (vista) is not logged in, but it eventually works (after 30-40s words "shutdown"). My service is running in LocalSystem. GrabPrivilege is the same code as before.

Thus, you may doubt that what you are trying to do is possible . I see that you are using an administrator account to start your service. Have you tried starting the service in LocalSystem to test completion? Perhaps your administrator privileges don't quite match LocalSystem requirements ...

+1


a source


If I am http://www.google.ca/search?hl=en&q=exitwindowsex+service then for example I will find http://www.eggheadcafe.com/software/aspnet/29901267/lockworkstation-and-exitw. aspx , which indicates a problem if your service has the "interact with the desktop" flag enabled (which is deprecated).

Then people suggest fixes in various posts that answer the above topic like http://www.eggheadcafe.com/conversation.aspx?messageid=29901274&threadid=29901267 ... something like this might help you.



An alternative, which is certainly a kludge, but which avoids the suggested magic, might be to have a second service running that doesn't interact with the desktop: the second service calls ExitWindowsEx ... and uses any IPC (or API specific for a service) to start the second service from the first service (or perhaps from the application).

0


a source


Try this code and let us know what it does:

GrabPrivilege (SE_SHUTDOWN_NAME);
ExitWindowsEx (EWX_REBOOT|EWX_FORCE, 0); // or whatever EWX flags you want

      

Helper function:

BOOL  GrabPrivilege (LPCTSTR lpctPrivName) 
{
   TOKEN_PRIVILEGES newtkp;
   HANDLE hToken;
   BOOL   bRetVal = FALSE;

   if (OpenProcessToken (GetCurrentProcess(), 
                         TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, 
                         &hToken)) 
   {
      LookupPrivilegeValue (NULL, 
                            lpctPrivName, 
                            &newtkp.Privileges[0].Luid); 
      newtkp.PrivilegeCount = 1;  // one privilege to set
      newtkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 

      if (AdjustTokenPrivileges (hToken, 
                                 FALSE, 
                                 &newtkp, 
                                 0, 
                                 (PTOKEN_PRIVILEGES) NULL, 
                                 0))
      {
         DWORD dwRet = GetLastError();
         if (dwRet == ERROR_SUCCESS) bRetVal = TRUE;
      }
   }
   CloseHandle (hToken);
   return bRetVal;
}

      

0


a source


Have you tried using shutdown.exe tool ? What error is he reporting?

I had a problem with Windows XP x64, but I was doing it remotely via WMI, but the shutdown tool gave me the same error (21). With this information I came across this fix:

http://support.microsoft.com/kb/834100

I haven't been able to test it as I don't have Windows 2003 Server at the moment, but maybe it helps.

0


a source







All Articles