Reputation: 2594
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.
Upvotes: 3
Views: 380
Reputation: 33804
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.
Upvotes: 3