-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTProgramRegistrationClass.pas
More file actions
348 lines (266 loc) · 11.1 KB
/
TProgramRegistrationClass.pas
File metadata and controls
348 lines (266 loc) · 11.1 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
unit TProgramRegistrationClass;
{ TProgRegistrationClass
Copyright (C) 2024 Baz Cuda
https://github.com/BazzaCuda/TProgramRegistrationClass
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
}
interface
uses
system.win.registry, winApi.windows, system.sysUtils, winApi.ShlObj;
type
TProgramRegistration = class(TObject)
strict private
FReg: TRegistry;
{property fields}
FAppArgs: string;
FClientType: string;
FProgIDPrefix: string;
FFriendlyAppName: string;
FFullPathToExe: string;
FFullPathToIco: string;
FRegRoot: HKEY;
FSysFileType: string;
private
function getSysFileAssocsKey: string;
public
constructor Create;
destructor Destroy; override;
function exeFileName: string;
function getApplicationsKey: string;
function getCapabilitiesKey: string;
function getFileAssociationsKey: string;
function getProgID(const aExtension: string): string;
function getRegisteredApplicationsKey: string;
function getSupportedTypesKey: string;
function registerAppPath: boolean;
function registerAppName: boolean;
function registerAppVerbs(const aKey: string): boolean;
function registerClientCapabilities: boolean; // e.g. media
function registerFileAssociation(const aExtension: string): boolean;
function registerRegisteredApplication: boolean;
function registerSupportedType(const aExtension: string): boolean;
function registerSysFileType: boolean; // e.g. audio, video
function registerExtension(const aExtension: string; const aFriendlyName: string; const aMimeType: string = ''; const aPerceivedType: string = ''): boolean;
function refreshDesktop: boolean;
property appArgs: string read FAppArgs write FAppArgs;
property clientType: string read FClientType write FClientType; // e.g. media
property friendlyAppName: string read FFriendlyAppName write FFriendlyAppName;
property fullPathToExe: string read FFullPathToExe write FFullPathToExe;
property fullPathToIco: string read FFullPathToIco write FFullPathToIco; // optional separate icon file
property progIDPrefix: string read FProgIDPrefix write FProgIDPrefix; // a unique identifier
property regRoot: HKEY read FRegRoot write FRegRoot;
property sysFileType: string read FSysFileType write FSysFileType;
end;
implementation
const
KEY_APP_PATHS = 'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\';
KEY_SOFTWARE_CLASSES = 'SOFTWARE\Classes\';
KEY_SOFTWARE_CLIENTS = 'SOFTWARE\Clients\';
KEY_APPLICATIONS = 'Applications\';
KEY_SYSFILE_ASSOCS = 'SystemFileAssociations\';
KEY_REGISTERED_APPS = 'SOFTWARE\RegisteredApplications\';
{ TProgramRegistration }
constructor TProgramRegistration.Create;
begin
inherited;
FReg := TRegistry.create(KEY_READ OR KEY_WRITE);
FRegRoot := HKEY_LOCAL_MACHINE;
FAppArgs := '"%1"';
end;
destructor TProgramRegistration.Destroy;
begin
case FReg <> NIL of TRUE: FReg.free; end;
inherited;
end;
function TProgramRegistration.exeFileName: string;
begin
result := extractFileName(FFullPathToExe);
end;
function TProgramRegistration.getApplicationsKey: string;
begin
result := KEY_SOFTWARE_CLASSES + KEY_APPLICATIONS + exeFileName + '\';
end;
function TProgramRegistration.getCapabilitiesKey: string;
begin
result := KEY_SOFTWARE_CLIENTS + FClientType + '\' + FFriendlyAppName + '\' + 'Capabilities\';
end;
function TProgramRegistration.getFileAssociationsKey: string;
begin
result := getCapabilitiesKey + 'FileAssociations\';
end;
function TProgramRegistration.getProgID(const aExtension: string): string;
begin
result := FProgIDPrefix + aExtension;
end;
function TProgramRegistration.getRegisteredApplicationsKey: string;
begin
result := KEY_REGISTERED_APPS;
end;
function TProgramRegistration.getSupportedTypesKey: string;
begin
result := KEY_SOFTWARE_CLASSES + KEY_APPLICATIONS + exeFileName + '\' + 'SupportedTypes\';
end;
function TProgramRegistration.getSysFileAssocsKey: string;
begin
result := KEY_SOFTWARE_CLASSES + KEY_SYSFILE_ASSOCS + FSysFileType + '\' + 'OpenWithList\' + exeFileName + '\';
end;
function TProgramRegistration.refreshDesktop: boolean;
begin
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
end;
function TProgramRegistration.registerAppName: boolean;
begin
result := FALSE;
FReg.rootKey := FRegRoot;
// HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Applications\your.exe\
case FReg.openKey(KEY_SOFTWARE_CLASSES + KEY_APPLICATIONS + exeFileName, TRUE) of FALSE: EXIT; end;
FReg.writeString('FriendlyAppName', FFriendlyAppName);
FReg.closeKey;
result := registerAppVerbs(KEY_SOFTWARE_CLASSES + KEY_APPLICATIONS + exeFileName + '\');
end;
function TProgramRegistration.registerAppPath: boolean;
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\your.exe
begin
result := FALSE;
FReg.rootKey := FRegRoot;
case FReg.openKey(KEY_APP_PATHS + exeFileName, TRUE) of FALSE: EXIT; end;
FReg.writeString('', FFullPathToExe);
FReg.writeInteger('UseUrl', 1);
FReg.closeKey;
result := TRUE;
end;
function TProgramRegistration.registerAppVerbs(const aKey: string): boolean;
// registerAppVerbs(getApplicationsKey)
begin
result := FALSE;
FReg.rootKey := FRegRoot;
case FReg.openKey(aKey + 'shell\', TRUE) of FALSE: EXIT;end;
// set the default verb to Play
FReg.writeString('', 'Play');
FReg.closeKey;
FReg.rootKey := FRegRoot;
// add "open" verb
case FReg.openKey(aKey + 'shell\open\', TRUE) of FALSE: EXIT; end;
// Hide the "open" verb from the context menu, since it's the same as "play"
FReg.writeString('LegacyDisable', '');
FReg.closeKey;
FReg.rootKey := FRegRoot;
case FReg.openKey(aKey + 'shell\open\command\', TRUE) of FALSE: EXIT; end;
// set open command = "fullpathtoexe" "%1" or "fullpathtoexe" followed by the caller-supplied args
FReg.writeString('', '"' + FFullPathToExe + '"' + ' ' + FAppArgs);
FReg.closeKey;
FReg.rootKey := FRegRoot;
// add "play" verb
case FReg.openKey(aKey + 'shell\play\', TRUE) of FALSE: EXIT; end;
FReg.writeString('', '&Play');
FReg.closeKey;
FReg.rootKey := FRegRoot;
case FReg.openKey(aKey + 'shell\play\command\', TRUE) of FALSE: EXIT; end;
// set play command = "fullpathtoexe" "%1" or "fullpathtoexe" followed by the caller-supplied args
FReg.writeString('', '"' + FFullPathToExe + '"' + ' ' + FAppArgs);
FReg.closeKey;
result := TRUE;
end;
function TProgramRegistration.registerClientCapabilities: boolean;
// HKEY_LOCAL_MACHINE\SOFTWARE\Clients\<ClientType>\<your app>\Capabilities\
begin
result := FALSE;
FReg.rootKey := FRegRoot;
case FReg.openKey(getCapabilitiesKey, TRUE) of FALSE: EXIT; end;
FReg.writeString('ApplicationName', FFriendlyAppName);
FReg.writeString('ApplicationDescription', FFriendlyAppName);
FReg.rootKey := FRegRoot;
FReg.closeKey;
registerRegisteredApplication;
result := TRUE;
end;
function TProgramRegistration.registerExtension(const aExtension: string; const aFriendlyName: string; const aMimeType: string = ''; const aPerceivedType: string = ''): boolean;
function getExtKey: string;
begin
result := KEY_SOFTWARE_CLASSES + aExtension + '\';
end;
function getProgIDExtKey: string;
begin
result := KEY_SOFTWARE_CLASSES + getProgID(aExtension) + '\';
end;
begin
result := FALSE;
FReg.rootKey := FRegRoot;
// create ProgID - HKEY_CLASSES_ROOT\<yourPrefix>.ext
case FReg.openKey(getProgIDExtKey, TRUE) of FALSE: EXIT; end;
FReg.writeString('', aFriendlyName);
FReg.writeInteger('EditFlags', 65536); // FTA_OpenIsSafe
FReg.writeString('FriendlyTypeName', aFriendlyName);
FReg.closeKey;
FReg.rootKey := FRegRoot;
case FReg.openKey(getProgIDExtKey + 'DefaultIcon\', TRUE) of FALSE: EXIT; end;
case FFullPathToIco = '' of TRUE: FReg.writeString('', FFullPathToExe + ',0');
FALSE: FReg.writeString('', FFullPathToIco); end;
FReg.closeKey;
FReg.rootKey := FRegRoot;
registerAppVerbs(getProgIDExtKey);
case FReg.openKey(getExtKey, TRUE) of FALSE: EXIT; end;
FReg.writeString('', getProgID(aExtension));
case aMimeType <> '' of TRUE: FReg.writeString('Content Type', aMimeType); end;
case aPerceivedType <> '' of TRUE: FReg.writeString('PerceivedType', aPerceivedType); end;
FReg.closeKey;
FReg.rootKey := FRegRoot;
// HKEY_CLASSES_ROOT\.ext\OpenWithProgIds
case FReg.openKey(getExtKey + 'OpenWithProgIDs\', TRUE) of FALSE: EXIT; end;
FReg.writeString(getProgID(aExtension), '');
FReg.closeKey;
FReg.rootKey := FRegRoot;
registerSupportedType(aExtension);
registerFileAssociation(aExtension);
result := TRUE;
end;
function TProgramRegistration.registerFileAssociation(const aExtension: string): boolean;
// see HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Capabilities\FileAssociations\
begin
result := FALSE;
FReg.rootKey := FRegRoot;
// add extension to the Default Programs control panel
case FReg.openKey(getFileAssociationsKey, TRUE) of FALSE: EXIT; end;
FReg.writeString(aExtension, getProgID(aExtension));
FReg.closeKey;
result := TRUE;
end;
function TProgramRegistration.registerRegisteredApplication: boolean;
// see HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications\
begin
result := FALSE;
FReg.rootKey := FRegRoot;
case FReg.openKey(getRegisteredApplicationsKey, TRUE) of FALSE: EXIT; end;
FReg.writeString(FFriendlyAppName, getCapabilitiesKey);
FReg.closeKey;
result := TRUE;
end;
function TProgramRegistration.registerSupportedType(const aExtension: string): boolean;
// see HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Applications\your.exe\SupportedTypes\
begin
result := FALSE;
FReg.rootKey := FRegRoot;
case FReg.openKey(getSupportedTypesKey, TRUE) of FALSE: EXIT; end;
FReg.writeString(aExtension, '');
FReg.closeKey;
result := TRUE;
end;
function TProgramRegistration.registerSysFileType: boolean;
begin
result := FALSE;
FReg.rootKey := FRegRoot;
case FReg.openKey(getSysFileAssocsKey, TRUE) of FALSE: EXIT; end;
FReg.closeKey;
result := TRUE;
end;
end.