Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Delay Windows shutdown

Delay Windows shutdown

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
17 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Royce Fickling 0

    I have a Windows service in which I need to detect the Windows shutdown event and, depending on what kind of processing I am doing at the time, delay the shutdown for a few seconds until my processing is complete. Can that be done? Any help appreciated.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #7

    AbortSystemShutdown() http://msdn2.microsoft.com/en-us/library/aa376630(VS.85).aspx[^] Sample Usage:

    BOOL AbortShutdown(LPTSTR lpMachineName)
    {
    	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)) 
        {
            TRACE("OpenProcessToken failed.\n"); 
    		return false;
        }
     
        // Get the LUID for shutdown privilege. 
         LookupPrivilegeValue(lpMachineName, 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) 
        {
            TRACE("AdjustTokenPrivileges(setting) enable failed.\n"); 
    		return false;
        }
     
        // Prevent the system from shutting down. 
        fResult = AbortSystemShutdown(lpMachineName); 
     
        if (!fResult) 
        { 
            TRACE("AbortSystemShutdown failed.\n"); 
    		return false;
        }
    	else
    		m_bIsShuttingDown = false; // Reset shut down flag
     
        // Disable shutdown privilege. 
        tkp.Privileges[0].Attributes = 0; 
        AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, 
            (PTOKEN_PRIVILEGES) NULL, 0); 
     
        if (GetLastError() != ERROR_SUCCESS) 
        {
            TRACE("AdjustTokenPrivileges(re-setting) disable failed.\n"); 
    		return false;
        }
    	return true;
    }
    
    1 Reply Last reply
    0
    • L led mike

      DavidCrow wrote:

      I usually open a dozen or so browser windows with various CP threads.

      How do you do that? Can you email the code?

      led mike

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #8

      Is it urgent?

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      L 1 Reply Last reply
      0
      • M Mark Salsbery

        Is it urgent?

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #9

        Mark Salsbery wrote:

        Is it urgent?

        If you have to ask you don't understand anything about the codes

        led mike

        M 1 Reply Last reply
        0
        • R Royce Fickling 0

          I have a Windows service in which I need to detect the Windows shutdown event and, depending on what kind of processing I am doing at the time, delay the shutdown for a few seconds until my processing is complete. Can that be done? Any help appreciated.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #10

          Read carefully:  Service Control Handler Function[^] Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          R 1 Reply Last reply
          0
          • L led mike

            Mark Salsbery wrote:

            Is it urgent?

            If you have to ask you don't understand anything about the codes

            led mike

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #11

            OMFG[^] Fishing season is OPEN!

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            R L 2 Replies Last reply
            0
            • M Mark Salsbery

              OMFG[^] Fishing season is OPEN!

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              R Offline
              R Offline
              Rajkumar R
              wrote on last edited by
              #12

              lol

              1 Reply Last reply
              0
              • M Mark Salsbery

                OMFG[^] Fishing season is OPEN!

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #13

                Mark Salsbery wrote:

                Fishing season is OPEN!

                Didn't you get the memo? The season opens on January 1st at 00:00:00 and closes on December 31st at 24:59:59 Here's another[^]

                led mike

                M 1 Reply Last reply
                0
                • L led mike

                  Mark Salsbery wrote:

                  Fishing season is OPEN!

                  Didn't you get the memo? The season opens on January 1st at 00:00:00 and closes on December 31st at 24:59:59 Here's another[^]

                  led mike

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #14

                  :doh:   Good one, thanks!

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Read carefully:  Service Control Handler Function[^] Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #15

                    Mark Salsbery wrote:

                    Service Control Handler Function[^]

                    Sounds more like a candidate for the Buzzword hall of shame forum. :doh:

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                    J 1 Reply Last reply
                    0
                    • R Rajesh R Subramanian

                      Mark Salsbery wrote:

                      Service Control Handler Function[^]

                      Sounds more like a candidate for the Buzzword hall of shame forum. :doh:

                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                      J Offline
                      J Offline
                      JudyL_MD
                      wrote on last edited by
                      #16

                      Rajesh R Subramanian wrote:

                      Mark Salsbery wrote: Service Control Handler Function[^] Sounds more like a candidate for the Buzzword hall of shame forum.

                      Nope :), it's actually a very concise description of the subject at hand - the specifics of the function which handles control messages for a service. MS surprisingly actually titled the page with meaningful text. Judy

                      R 1 Reply Last reply
                      0
                      • J JudyL_MD

                        Rajesh R Subramanian wrote:

                        Mark Salsbery wrote: Service Control Handler Function[^] Sounds more like a candidate for the Buzzword hall of shame forum.

                        Nope :), it's actually a very concise description of the subject at hand - the specifics of the function which handles control messages for a service. MS surprisingly actually titled the page with meaningful text. Judy

                        R Offline
                        R Offline
                        RoyceF
                        wrote on last edited by
                        #17

                        Thanks everyone for the help. The AbortShutdown() is what I was looking for. I know about the WM_QUERYENDSESSION message and how to handle it. Originally, I was looking for a way to get around the hidden window in my service, but because I have to interact with my user, I must use it.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups