-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.AutoStart.pas
More file actions
152 lines (124 loc) · 3.44 KB
/
Copy pathMaxLogic.AutoStart.pas
File metadata and controls
152 lines (124 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
unit MaxLogic.AutoStart;
interface
uses
windows, classes, sysUtils;
// adds application.exeName to autostart
procedure AddToAutoStart(const aParams: string = ''); Overload;
procedure AddToAutoStart(const aTitle, aFilename: string;
const aParams: string = ''); Overload;
procedure RemoveFromAutoStart; Overload;
procedure RemoveFromAutoStart(const aFilename: string); Overload;
function isInAutoStart: boolean; Overload;
function isInAutoStart(aFilename: string): boolean; Overload;
implementation
uses
registry, forms, strUtils;
procedure AddToAutoStart(const aParams: string = '');
var
Title, Filename: string;
begin
if Application.Title <> '' then
Title := Application.Title
else
Title := ChangeFileExt(ExtractFilename(Application.ExeName), '');
Filename := Application.ExeName;
AddToAutoStart(Title, Filename, aParams);
end;
procedure AddToAutoStart(const aTitle, aFilename: string;
const aParams: string = '');
var
KeyName, Value: string;
reg: TRegistry;
begin
if aFilename = '' then
Exit;
if aTitle = '' then
KeyName := ExtractFilename(aFilename)
else
KeyName := aTitle;
if aFilename[1] <> '"' then
Value := '"' + aFilename
else
Value := aFilename;
if Value[Length(Value)] <> '"' then
Value := Value + '"';
if aParams <> '' then
begin
if aParams[1] <> ' ' then
Value := Value + ' ' + aParams
else
Value := Value + aParams;
end;
reg := TRegistry.Create;
// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
reg.rootkey := HKEY_CURRENT_USER;
reg.openkey('\Software\Microsoft\Windows\CurrentVersion\Run', true);
if (not reg.ValueExists(KeyName)) or (reg.readstring(KeyName) <> Value) then
reg.WriteString(KeyName, Value);
reg.Free;
end;
procedure RemoveFromAutoStart;
begin
RemoveFromAutoStart(Application.ExeName);
end;
procedure RemoveFromAutoStart(const aFilename: string);
var
reg: TRegistry;
s: string;
l: TStringList;
x: Integer;
begin
reg := TRegistry.Create;
// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
reg.rootkey := HKEY_CURRENT_USER;
reg.openkey('\Software\Microsoft\Windows\CurrentVersion\Run', true);
l := TStringList.Create;
reg.GetValueNames(l);
for x := 0 to l.Count - 1 do
begin
s := Trim(reg.readstring(l[x]));
if s = '' then
continue;
if s[1] = '"' then
s := AnsiDequotedStr(s, '"');
if startsText(aFilename, s) then // remember, there might be some command line params attached, so just test the start of this string
reg.DeleteValue(l[x]);
end;
l.Free;
reg.Free;
end;
function isInAutoStart: boolean;
begin
result := isInAutoStart(Application.ExeName);
end;
function isInAutoStart(aFilename: string): boolean;
var
reg: TRegistry;
s: string;
l: TStringList;
x: Integer;
begin
result := False;
reg := TRegistry.Create;
// HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
reg.rootkey := HKEY_CURRENT_USER;
reg.openkey('\Software\Microsoft\Windows\CurrentVersion\Run', true);
l := TStringList.Create;
reg.GetValueNames(l);
for x := 0 to l.Count - 1 do
begin
s := Trim(reg.readstring(l[x]));
if s = '' then
continue;
if s[1] = '"' then
s := AnsiDequotedStr(s, '"');
if startsText(aFilename, s) then // remember, there might be some command line params attached, so just test the start of this string
begin
result := true;
Break;
end;
end;
l.Free;
reg.Free;
end;
end.