Execute actions before reboot

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • TrifonovS
    Senior Member
    • Apr 2009
    • 192

    Execute actions before reboot

    Hi,
    I have a strange issue and I cannot find a solution. My installer calls an external installer (included in it), but sometimes this external installer reboots the PC. I want my installer to execute some actions before the reboot. How it can handle this?
  • Anders
    Moderator
    • Jun 2002
    • 5643

    #2
    I don't see how you can, ExecWait does not return until the process has completed. Is this a MSI based external installer?
    IntOp $PostCount $PostCount + 1

    Comment

    • TrifonovS
      Senior Member
      • Apr 2009
      • 192

      #3
      Yes, it is MSI based installer. I know that I cannot do anything inside my installer, but I'm thinking that I can register somewhere another small application (before to call this external installer) that will be called when Windows tries to reboot and it can do the job.But I don't really know how to do it...

      Comment

      • Nutzzz
        Member
        • May 2007
        • 73

        #4
        MSI installers take the /norestart command line parameter, and return 3010 if install was successful but requires a reboot.

        If the installer didn't take a command line parameter, I could think of some wacky hacks like adding a logoff script and then removing it once the script runs, or just removing it without running it if the installer didn't reboot.

        Comment

        • TrifonovS
          Senior Member
          • Apr 2009
          • 192

          #5
          Thank you Nutzzz, but ca you explain a little bit the idea of the logoff script. I didn't get it...

          Comment

          • Nutzzz
            Member
            • May 2007
            • 73

            #6
            Well, since the former method should work for you, that's by far the better solution.

            The hacky method I mention is to replicate the method that the group policy editor uses to assign a shutdown script to the machine (or a logoff script for the current user) by using a .reg file with "regedit.exe /s regfile.reg" (and/or a group of WriteRegStr/WriteRegDWORD NSIS commands). See this page for an example .reg file for a startup script.

            Your script would be a .bat/.cmd or .vbs file that runs your actions and then applies another .reg file that deletes the registry entries so the shutdown script doesn't run next time around. If you want/need to use NSIS for your actions, you could create another installer or even leverage your uninstaller by calling it with a command line parameter that uses an otherwise unused routine and then immediately quits before the regular uninstall routines begin.

            If a reboot isn't performed and execution returns to your installer, then delete the registry entries so that they're never used.

            FYI, to delete registry values using a .reg file, assign it an unquoted hyphen (-), e.g.:
            code:
            [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown\0\0]
            "Script"=-
            "Parameters"=-
            "ExecTime"=-

            Comment

            • TrifonovS
              Senior Member
              • Apr 2009
              • 192

              #7
              Hi again Nutzzz,
              Thanks a lot. This is exactly what I'm searching for.

              Comment

              • TrifonovS
                Senior Member
                • Apr 2009
                • 192

                #8
                Hi again!
                This doesn't work. When the PC is connected to the network, the group policy is changed by the network server. The problem still occurs. Isn't there another way to catch the reboot without hacks?

                Comment

                • TrifonovS
                  Senior Member
                  • Apr 2009
                  • 192

                  #9
                  One additional question. I found somewhere in Internet that NSIS handles WM_QUERYENDSESSION message and returns FALSE, so it must prevent rebooting from another application. Is there any possibility to execute a function from the script when thos message is received?

                  Comment

                  • Anders
                    Moderator
                    • Jun 2002
                    • 5643

                    #10
                    Originally Posted by TrifonovS View Post
                    One additional question. I found somewhere in Internet that NSIS handles WM_QUERYENDSESSION message and returns FALSE, so it must prevent rebooting from another application. Is there any possibility to execute a function from the script when thos message is received?
                    Only if you write a custom plug-in. You don't know if Windows is really restarting at that point though.

                    See also:
                    IntOp $PostCount $PostCount + 1

                    Comment

                    • TrifonovS
                      Senior Member
                      • Apr 2009
                      • 192

                      #11
                      Thank you Anders. I want to avoid writing a plug-in, because my knowledge is not enough. However, is it possible to use code like this:
                      PHP Code:
                        !include SysFunc.nsh
                        
                      !include WinMessages.nsh

                        
                      ...
                        
                      StrCpy $5 $hwndParent
                        
                      Create MSG struct
                        System
                      ::Call "*${stMSG} (_) p.R9"
                        
                      Get message
                        System
                      ::Call "${sysGetMessage} (R9, r5,_) .s"
                        
                      !insertmacro SINGLE_CALLBACK 1 $R8 1 ShutDownRequest

                        
                      ...
                        Function 
                      ShutDownRequest
                          MessageBox MB_OK 
                      "NSIS: Shutdown request detected!!!"
                        
                      FunctionEnd 
                      I tried this code, but the installer hangs-up somewhere in these lines. My problem is that this is ready code and I don't understand it completely. Maybe there is an error that I cannot see...

                      Comment

                      • Anders
                        Moderator
                        • Jun 2002
                        • 5643

                        #12
                        You can't use the system plug-in to do this. I wrote a WndSubClass plug-in a long time ago, it might work.
                        IntOp $PostCount $PostCount + 1

                        Comment

                        • TrifonovS
                          Senior Member
                          • Apr 2009
                          • 192

                          #13
                          Hi Anders,
                          I'm following you advice. I installed the WndSubclass plug-in and also executed the example that comes with it. It works fine, but I still can't understand how to use it for my needs. Sorry for that, but can you give me a next hint?

                          Comment

                          • TrifonovS
                            Senior Member
                            • Apr 2009
                            • 192

                            #14
                            One more question. I cannot understand from the example why the message is in $2:
                            PHP Code:
                              ${If} $= ${WM_NCHITTEST}
                                ; ...
                              ${ElseIf} $
                            = ${WM_TIMER}
                                ; ...
                              ${ElseIf} $
                            = ${WM_LBUTTONUP}
                                ; ...
                              ${EndIf} 
                            Where it comes the value in $2?

                            Comment

                            • Anders
                              Moderator
                              • Jun 2002
                              • 5643

                              #15
                              The plug-in uses $1, $2, $3 and $4 so you don't have to pop IIRC.
                              IntOp $PostCount $PostCount + 1

                              Comment

                              Working...
                              X