Making an application restart with Windows
When you shut Windows with Windows Explorer still running, the next time Windows comes up, the Win Explorer restart as well. You may wish your application to exhibit the same behavior.
Below you'll find a code extract from a (main) form unit with the desired behavior:
~~~~~~~~~~~~~~~~~~~~~~~~~
type
TForm1 = class(TForm)
...
public
procedure WMEndSession(var Msg: TWMEndSession) ;
message wm_EndSession;
end;
...
uses
Registry;
...
procedure TForm1.WMEndSession(var Msg: TWMEndSession) ;
const
Restart =
'Software\Microsoft\Windows\CurrentVersion\RunOnce';
begin
if Msg.EndSession then
begin
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey(Restart, True) then
WriteString
(Application.Title, CmdLine)
finally
Free
end;
Msg.Result := 0
end;
inherited;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
Delphi tips navigator:
» Case statement that *accepts* string values
« Two (or more) line Hint box
Below you'll find a code extract from a (main) form unit with the desired behavior:
~~~~~~~~~~~~~~~~~~~~~~~~~
type
TForm1 = class(TForm)
...
public
procedure WMEndSession(var Msg: TWMEndSession) ;
message wm_EndSession;
end;
...
uses
Registry;
...
procedure TForm1.WMEndSession(var Msg: TWMEndSession) ;
const
Restart =
'Software\Microsoft\Windows\CurrentVersion\RunOnce';
begin
if Msg.EndSession then
begin
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey(Restart, True) then
WriteString
(Application.Title, CmdLine)
finally
Free
end;
Msg.Result := 0
end;
inherited;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
Delphi tips navigator:
» Case statement that *accepts* string values
« Two (or more) line Hint box
Source...