-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.Win.Shortcuts.pas
128 lines (103 loc) · 2.7 KB
/
JPL.Win.Shortcuts.pas
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
unit JPL.Win.Shortcuts;
interface
{$IFDEF MSWINDOWS}
{$I .\..\jp.inc}
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
uses
Windows, SysUtils, Variants, Classes, Registry,
ShlObj, ActiveX, ComObj;
//JPL.Strings;
procedure DeleteShortcut(const Folder, LnkShortName: string);
function ShortcutExists(const Folder, LnkShortName: string): Boolean;
procedure CreateShortcut(const {%H-}Name, FileName, Folder, LnkShortName: string; Description: string = '');
{$ENDIF} // MSWINDOWS
implementation
{$IFDEF MSWINDOWS}
procedure DeleteShortcut(const Folder, LnkShortName: string);
var
Reg: TRegistry;
dir: string;
begin
Reg := TRegistry.Create;
try
with Reg do
begin
RootKey := HKEY_CURRENT_USER;
if OpenKey('Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', False) then
begin
dir := ReadString(Folder);
CloseKey;
if FileExists(dir + '\' + LnkShortName) then DeleteFile(dir + '\' + LnkShortName);
end;
end;
finally
Reg.Free;
end;
end;
function ShortcutExists(const Folder, LnkShortName: string): Boolean;
var
Reg: TRegistry;
dir: string;
begin
Result := False;
Reg := TRegistry.Create;
try
with reg do
begin
RootKey := HKEY_CURRENT_USER;
if OpenKey(
'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',
false) then
begin
dir := ReadString(Folder);
CloseKey;
Result := FileExists(dir + '\' + LnkShortName);
//ShowMessage(dir + '\' + LnkShortName);
end;
end;
finally
Reg.Free;
end;
end;
procedure CreateShortcut(const Name, FileName, Folder, LnkShortName: string; Description: string = '');
var
obj: IUnknown;
link: IShellLink;
pf: IPersistFile;
fName: WideString;
Reg: TRegistry;
dir: string;
begin
try
CoInitialize(nil);
obj := CreateComObject(CLSID_ShellLink);
link := obj as IShellLink;
pf := obj as IPersistFile;
with link do
begin
SetPath(PChar(FileName));
SetArguments('');
SetWorkingDirectory(PChar(ExtractFilePath(FileName)));
if Description <> '' then SetDescription(PChar(Description)); //link.SetDescription(PChar(Name));
end;
Reg := TRegistry.Create;
try
with Reg do
begin
RootKey := HKEY_CURRENT_USER;
OpenKey('Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', false);
dir := ReadString(Folder);
if not DirectoryExists(dir) then CreateDir(dir);
CloseKey;
end;
finally
Reg.Free;
end;
//fName := dir + '\' + Name + '.lnk';
fName := WideString(dir) + '\' + WideString(LnkShortName);
pf.Save(PWChar(fName), false);
except
end;
end;
{$ENDIF} // MSWINDOWS
end.