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. Delaying Shutdown through windows service and executing scripts ?

Delaying Shutdown through windows service and executing scripts ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
25 Posts 4 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.
  • C Covean

    Does your service get the shutdown notification SERVICE_CONTROL_SHUTDOWN or does AbortSystemShutdown not abort the shutdown? I would say its easier to test the version with SetConsoleCtrl..., after that you should try th service version. Edit: In my first answer I said, that you have to say the system that your service supports SHUTDOWN events. Now I found what i meant. You have to use SetServiceStatus with a SERVICE_STATUS where dwControlsAccepted is a least SERVICE_ACCEPT_SHUTDOWN.

    modified on Thursday, October 22, 2009 12:02 PM

    K Offline
    K Offline
    Kushagra Tiwari
    wrote on last edited by
    #16

    Covean wrote:

    . You have to use SetServiceStatus with a SERVICE_STATUS where dwControlsAccepted is a least SERVICE_ACCEPT_SHUTDOWN.

    As per ur suggestion I tried to do the same but seems the control never reaches my code , I am quite confused actually as I think I am doing every thing which I think is right .Here is what I am doing after calling RegisterserverEx: ::SetServiceStatus(m_hServiceStatus, &m_status); Where m_status is of SERVICE_STATUS type and initialized by : m_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS; m_status.dwCurrentState = SERVICE_STOPPED; m_status.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN; m_status.dwWin32ExitCode = 0; m_status.dwServiceSpecificExitCode = 0; m_status.dwCheckPoint = 0; m_status.dwWaitHint = 0; and in Handler function of my Service handle SERVICE_CONTROL_SHUTDOWN which has following code char chComputerName[512]; DWORD dwBuff = 512; GetComputerName(chComputerName,&dwBuff); switch (dwOpcode) { case SERVICE_CONTROL_STOP: SetServiceStatus(SERVICE_STOP_PENDING); Alive = true; iscmcontrol = 3; PostThreadMessage(dwThreadID, WM_QUIT, 0, 0); break; case SERVICE_CONTROL_PAUSE: Alive = false; iscmcontrol = 1; break; case SERVICE_CONTROL_CONTINUE: Alive = false; iscmcontrol = 0; break; case SERVICE_CONTROL_INTERROGATE: break; case SERVICE_CONTROL_SHUTDOWN: CoInitialize( NULL ); AbortShutdown(chComputerName); CoUninitialize(); Alive = true; iscmcontrol = 2; break; default: LogEvent(_T("Bad service request")); } If you want the sample code and personally see what is the problem please let me know your email address I will send it to you . Regards, Kushagra

    1 Reply Last reply
    0
    • C Covean

      Does your service get the shutdown notification SERVICE_CONTROL_SHUTDOWN or does AbortSystemShutdown not abort the shutdown? I would say its easier to test the version with SetConsoleCtrl..., after that you should try th service version. Edit: In my first answer I said, that you have to say the system that your service supports SHUTDOWN events. Now I found what i meant. You have to use SetServiceStatus with a SERVICE_STATUS where dwControlsAccepted is a least SERVICE_ACCEPT_SHUTDOWN.

      modified on Thursday, October 22, 2009 12:02 PM

      K Offline
      K Offline
      Kushagra Tiwari
      wrote on last edited by
      #17

      Hello Covean, Did you recieve my mail having the sample code?? Kushagra

      C 1 Reply Last reply
      0
      • K Kushagra Tiwari

        Hello Covean, Did you recieve my mail having the sample code?? Kushagra

        C Offline
        C Offline
        Covean
        wrote on last edited by
        #18

        Yes I received your code and after some changes (for VS2008 and Vista) I has installed it. Now I'm try to get it to work.

        K 2 Replies Last reply
        0
        • C Covean

          Yes I received your code and after some changes (for VS2008 and Vista) I has installed it. Now I'm try to get it to work.

          K Offline
          K Offline
          Kushagra Tiwari
          wrote on last edited by
          #19

          Ok . I hope it works :) Kushagra

          1 Reply Last reply
          0
          • C Covean

            Yes I received your code and after some changes (for VS2008 and Vista) I has installed it. Now I'm try to get it to work.

            K Offline
            K Offline
            Kushagra Tiwari
            wrote on last edited by
            #20

            Any luck with the code? Regards, Kushagra

            C 1 Reply Last reply
            0
            • K Kushagra Tiwari

              Any luck with the code? Regards, Kushagra

              C Offline
              C Offline
              Covean
              wrote on last edited by
              #21

              What OS to you use? Cause on Vista I do not find any way to block the shutdown (ShutdownBlockReasonCreate needs a window...). But the service itself works fine. Here some eventlog-output: SetServiceStatus Succeed Service started SetServiceStatus Succeed SERVICE_CONTROL_SHUTDOWN (<- shutdown event caught) AbortSystemShutdown failed! Error=1115 (<- means shutdown already in progress, very funny errorcode :) ) The only thing left is to stop the shutdown. On WinXP your code should work. How do you test your service? You can just catch this event if your app really runs as service in debug mode you will never see this event. Here some code-changes I made (but nothing very special): 1. The "problem" that your SetServiceStatus always "fails":

              void ...SetServiceStatus(DWORD dwState)
              {
              if (::SetServiceStatus(m_hServiceStatus, &m_status) == TRUE)
              {
              // ...
              LogEvent(TEXT("SetServiceStatus Failed"));
              }
              else
              {
              // ...
              LogEvent(TEXT("SetServiceStatus Failed"));
              }
              }

              ---> copy & paste is not always your friend. :laugh: 2. In "...::Handler(...)" I made some changes to get more eventlog entries:

              switch (dwOpcode)
              {
              case SERVICE_CONTROL_STOP:
              LogEvent(_T("SERVICE_CONTROL_STOP"));
              //...
              break;
              case SERVICE_CONTROL_PAUSE:
              LogEvent(_T("SERVICE_CONTROL_PAUSE"));
              break;
              case SERVICE_CONTROL_CONTINUE:
              LogEvent(_T("SERVICE_CONTROL_CONTINUE"));
              break;
              case SERVICE_CONTROL_INTERROGATE:
              LogEvent(_T("SERVICE_CONTROL_INTERROGATE"));
              break;
              case SERVICE_CONTROL_SHUTDOWN:
              LogEvent(_T("SERVICE_CONTROL_SHUTDOWN"));

                  AbortShutdown(chComputerName);
                  break;
              //...
              

              }

              3. AbortShutdown changes: (AbortShutdown is now part of your Module class, just for LogEvent)

              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");
              LogEvent(TEXT("OpenProcessToken failed"));

              	return false;
              }
              
              
              // Get the LUID for shutdown privilege.
              
              K 2 Replies Last reply
              0
              • C Covean

                What OS to you use? Cause on Vista I do not find any way to block the shutdown (ShutdownBlockReasonCreate needs a window...). But the service itself works fine. Here some eventlog-output: SetServiceStatus Succeed Service started SetServiceStatus Succeed SERVICE_CONTROL_SHUTDOWN (<- shutdown event caught) AbortSystemShutdown failed! Error=1115 (<- means shutdown already in progress, very funny errorcode :) ) The only thing left is to stop the shutdown. On WinXP your code should work. How do you test your service? You can just catch this event if your app really runs as service in debug mode you will never see this event. Here some code-changes I made (but nothing very special): 1. The "problem" that your SetServiceStatus always "fails":

                void ...SetServiceStatus(DWORD dwState)
                {
                if (::SetServiceStatus(m_hServiceStatus, &m_status) == TRUE)
                {
                // ...
                LogEvent(TEXT("SetServiceStatus Failed"));
                }
                else
                {
                // ...
                LogEvent(TEXT("SetServiceStatus Failed"));
                }
                }

                ---> copy & paste is not always your friend. :laugh: 2. In "...::Handler(...)" I made some changes to get more eventlog entries:

                switch (dwOpcode)
                {
                case SERVICE_CONTROL_STOP:
                LogEvent(_T("SERVICE_CONTROL_STOP"));
                //...
                break;
                case SERVICE_CONTROL_PAUSE:
                LogEvent(_T("SERVICE_CONTROL_PAUSE"));
                break;
                case SERVICE_CONTROL_CONTINUE:
                LogEvent(_T("SERVICE_CONTROL_CONTINUE"));
                break;
                case SERVICE_CONTROL_INTERROGATE:
                LogEvent(_T("SERVICE_CONTROL_INTERROGATE"));
                break;
                case SERVICE_CONTROL_SHUTDOWN:
                LogEvent(_T("SERVICE_CONTROL_SHUTDOWN"));

                    AbortShutdown(chComputerName);
                    break;
                //...
                

                }

                3. AbortShutdown changes: (AbortShutdown is now part of your Module class, just for LogEvent)

                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");
                LogEvent(TEXT("OpenProcessToken failed"));

                	return false;
                }
                
                
                // Get the LUID for shutdown privilege.
                
                K Offline
                K Offline
                Kushagra Tiwari
                wrote on last edited by
                #22

                Yup you are right ...some time a minute error can take the hell out of ..hehehe :laugh: I am working on the code presently will let you know on the progress . Cheers, Kushagra :thumbsup:

                1 Reply Last reply
                0
                • C Covean

                  What OS to you use? Cause on Vista I do not find any way to block the shutdown (ShutdownBlockReasonCreate needs a window...). But the service itself works fine. Here some eventlog-output: SetServiceStatus Succeed Service started SetServiceStatus Succeed SERVICE_CONTROL_SHUTDOWN (<- shutdown event caught) AbortSystemShutdown failed! Error=1115 (<- means shutdown already in progress, very funny errorcode :) ) The only thing left is to stop the shutdown. On WinXP your code should work. How do you test your service? You can just catch this event if your app really runs as service in debug mode you will never see this event. Here some code-changes I made (but nothing very special): 1. The "problem" that your SetServiceStatus always "fails":

                  void ...SetServiceStatus(DWORD dwState)
                  {
                  if (::SetServiceStatus(m_hServiceStatus, &m_status) == TRUE)
                  {
                  // ...
                  LogEvent(TEXT("SetServiceStatus Failed"));
                  }
                  else
                  {
                  // ...
                  LogEvent(TEXT("SetServiceStatus Failed"));
                  }
                  }

                  ---> copy & paste is not always your friend. :laugh: 2. In "...::Handler(...)" I made some changes to get more eventlog entries:

                  switch (dwOpcode)
                  {
                  case SERVICE_CONTROL_STOP:
                  LogEvent(_T("SERVICE_CONTROL_STOP"));
                  //...
                  break;
                  case SERVICE_CONTROL_PAUSE:
                  LogEvent(_T("SERVICE_CONTROL_PAUSE"));
                  break;
                  case SERVICE_CONTROL_CONTINUE:
                  LogEvent(_T("SERVICE_CONTROL_CONTINUE"));
                  break;
                  case SERVICE_CONTROL_INTERROGATE:
                  LogEvent(_T("SERVICE_CONTROL_INTERROGATE"));
                  break;
                  case SERVICE_CONTROL_SHUTDOWN:
                  LogEvent(_T("SERVICE_CONTROL_SHUTDOWN"));

                      AbortShutdown(chComputerName);
                      break;
                  //...
                  

                  }

                  3. AbortShutdown changes: (AbortShutdown is now part of your Module class, just for LogEvent)

                  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");
                  LogEvent(TEXT("OpenProcessToken failed"));

                  	return false;
                  }
                  
                  
                  // Get the LUID for shutdown privilege.
                  
                  K Offline
                  K Offline
                  Kushagra Tiwari
                  wrote on last edited by
                  #23

                  I was able to abort shutdown and execute my scripts when the user was logged on to the system by making the service as an interactive service but in case when user was logged off and the shutdown was scheduled , the shutdown was aborted but no scripts were executed.Please suggest what to do in this case when no user is logged on to the system and shutdown was made a scheduled task. Regards, Kushagra

                  C 1 Reply Last reply
                  0
                  • K Kushagra Tiwari

                    I was able to abort shutdown and execute my scripts when the user was logged on to the system by making the service as an interactive service but in case when user was logged off and the shutdown was scheduled , the shutdown was aborted but no scripts were executed.Please suggest what to do in this case when no user is logged on to the system and shutdown was made a scheduled task. Regards, Kushagra

                    C Offline
                    C Offline
                    Covean
                    wrote on last edited by
                    #24

                    Sorry but after I read some information on this, I don't believe, that you will be able to abort this kind of shutdown. But maybe someone else has an idea.

                    K 1 Reply Last reply
                    0
                    • C Covean

                      Sorry but after I read some information on this, I don't believe, that you will be able to abort this kind of shutdown. But maybe someone else has an idea.

                      K Offline
                      K Offline
                      Kushagra Tiwari
                      wrote on last edited by
                      #25

                      Even I couldn't find any way to do the same . Windows can be harsh to us some times :( . Still I am looking for some tweaks to achieve it . Will let you know if I found some . Regards, Kushagra

                      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