The following example shows how to tell Windows to relaunch your application when Windows starts up if it was running when the system shut down. When Windows starts up, it launches each application listed in the RunOnce key and then deletes the entry for that application. Therefore, you do not need to remove the entry written here.
procedure TForm1.WMEndSession(var Message: TWMEndSession);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/RunOnce',
True)
then Reg.WriteString('MyApp','"' + ParamStr(0) + '"');
finally
Reg.CloseKey;
Reg.Free;
inherited;
end;
end;
In order for this method to be called, it must be declared in the interface section of your unit as follows:
private
procedure WMEndSession(var Msg:TWMEndSession); message
WM_ENDSESSION;
13. 读写注册表的二进制值
uses .., registry;
procedure TForm1.Button2Click(Sender: TObject);
var
Reg: TRegistry;
b,b2:array[0..10] of char;
i,n:integer;
begin
Reg := TRegistry.Create;
b[0]:=#10; //10
b[1]:='1'; //49
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('/Software/Microsoft/Windows/CurrentVersion/RunOnce', True)
then
begin
Reg.WriteBinaryData('MyApp',b,2);
n:=Reg.ReadBinaryData('MyApp',b2,10);
showmessage(inttostr
);
for i:=0 to n-1 do showmessage(inttostr(integer(b2
)));
end;
finally
Reg.CloseKey;
Reg.Free;
inherited;
end;
end;