3

When Windows automatically installs updates and automatically restarts, some programs (like Microsoft Word) come back up automatically and restore their original state (Word opens the same documents that were open previously). How can my program do this? Is there an API?

If it is a regular user-initiated restart I do not want my program to come back automatically - that would look weird to the user. But if the user leaves my program open when they go to bed and Windows reboots for updates in the middle of the night, I want the user to come back to their computer in the morning and still see my program running.

I care about Window 7 and above. I tagged the question as C#, since this is the language I am using, but if there is a solution in C/C++, I'll write appropriate glue code and post it here for others to use, too.

George
  • 2,436
  • 4
  • 15
  • 30
  • 5
    The principle is that you [detect a session logoff](http://stackoverflow.com/q/6799955/11683), and if the [reason](https://msdn.microsoft.com/en-us/library/microsoft.win32.sessionendreasons.aspx) is the one you want, you [register](http://stackoverflow.com/q/7483230/11683) your program for restart. – GSerg Dec 14 '16 at 21:06
  • Override the `onClosing` method and save the settings to a file while closing. You can save the reason of closing. Take a look here to figure out how to get the closing reason: http://stackoverflow.com/questions/1623756/detect-reason-for-form-closing When your program restarts, it checks the reason and decides whether to restore state or not – Everyone Dec 14 '16 at 21:07
  • 2
    @RbMm This feature is for installers, and *For this to work, the installer of the application must call the `ExitWindowsEx` function with the `EWX_RESTARTAPPS` flag set or the `InitiateShutdown` function with the `SHUTDOWN_RESTARTAPPS` flag set*. – GSerg Dec 14 '16 at 21:11
  • Always wondered, how that works myself. Quick search suggests, that this is [Application Recovery and Restart](https://msdn.microsoft.com/en-us/library/cc948909.aspx) (introduced in Windows Vista) at work. Canonical sample is available from [Registering for Application Restart](https://msdn.microsoft.com/en-us/library/bb525423.aspx). – IInspectable Dec 14 '16 at 21:30
  • 3
    Sample code, that better matches the question: [Registering for Application Recovery](https://msdn.microsoft.com/en-us/library/bb525422.aspx). – IInspectable Dec 14 '16 at 21:40
  • C != C#, and you can't possibly be using both at once. If the question is language agnostic, leave off the language tags altogether and stick with WinAPI. Otherwise, pick the *specific* language tag for the language you're actually using. – Ken White Dec 15 '16 at 00:50

1 Answers1

3

begin from vista we can use for this single call

RegisterApplicationRestart(L"some cmd line", RESTART_NO_CRASH|RESTART_NO_HANG);

if we run under XP, we can (as @GSerg propose) listen for WM_ENDSESSION message with

wParam != 0 && (lParam & ENDSESSION_CLOSEAPP) != 0

and register self restart under HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce (but not under Run !)

explorer.exe on any next start handle this by delete value key and exec application.

RbMm
  • 31,280
  • 3
  • 35
  • 56
  • 1
    The reverse engineered implemention details aren't relevant. – David Heffernan Dec 15 '16 at 05:20
  • @DavidHeffernan - probably and not relevant, but i guessed that for somebody can be interesting how this implemented internally – RbMm Dec 15 '16 at 09:52
  • 2
    I would expect that it mostly just clouds the message. I'd read this answer and glaze over. I know that you love reverse engineering, but it's not necessary to program on Windows, and implementation specific knowledge can often be counter productive. – David Heffernan Dec 15 '16 at 09:57
  • @DavidHeffernan - sorry. I remove implementation specific ("you love reverse engineering" - not more than coding, this is only seems) – RbMm Dec 15 '16 at 10:45
  • @RbMm - thank you. On Windows 7 and above, do I just call RegisterApplicationRestart() whenever my program starts? Do I need to delete this registration when my program exits? For example, if user closes my program and later on Windows installs updates and reboots, what will preven my program from starting up automatically? – George Dec 15 '16 at 18:22
  • @George - when you call `RegisterApplicationRestart` - on `ExitWindowsEx` with `EWX_RESTARTAPPS` flag you app will be registered under `HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce` - so when explorer start next time in your account - this entry will be auto deleted and your app started. this worked and for manifested apps (requireAdmin) – RbMm Dec 15 '16 at 18:26
  • @George - RegisterApplicationRestart - nothing write to registry just - it only save info in memory block in your process. so you not need nothing delete on normal exit – RbMm Dec 15 '16 at 18:27
  • @George - also can read `Remarks` under https://msdn.microsoft.com/en-us/library/windows/desktop/aa373349(v=vs.85).aspx – RbMm Dec 15 '16 at 18:33