Author: Jonas Bilinkevicius
How to add programs to the Windows start menu
Answer:
Solve 1:
1
2 procedure CreateStartmenuLink(ExeFile, WorkPath, Args, Descr: string);
3 var
4 MyObject: IUnknown;
5 MyLink: IShellLink;
6 MyFile: IPersistFile;
7 ds: WideString;
8 StartMenuDir: string;
9 reg_info: TRegIniFile;
10 reg: TRegistry;
11 s: string;
12 begin
13 reg_Info :=
14 TRegIniFile.Create('Software\Microsoft\Windows\CurrentVersion\Explorer');
15 StartMenuDir := reg_Info.ReadString('Shell Folders', 'Start Menu', '');
16 reg_Info.Free;
17 s := ExtractFilePath(StartMenuDir + '\' + Descr + '.lnk');
18 ForceDirectories(s);
19 if FileExists(StartMenuDir + '\' + Descr + '.lnk') then
20 DeleteFile(StartMenuDir + '\' + Descr + '.lnk');
21 MyObject := CreateComObject(CLSID_ShellLink);
22 MyLink := MyObject as IShellLink;
23 MyFile := MyObject as IPersistFile;
24 MyLink.SetArguments(PChar(Args));
25 MyLink.SetPath(PChar(ExeFile));
26 MyLink.SetWorkingDirectory(PChar(WorkPath));
27 s := ExtractFileName(StartMenuDir + '\' + Descr + '.lnk');
28 s := copy(s, 1, length(s) - 4);
29 MyLink.SetDescription(PChar(s));
30 ds := StartMenuDir + '\' + Descr + '.lnk';
31 MyFile.Save(PWChar(ds), false);
32 reg := TRegistry.Create;
33 reg.RootKey := HKEY_USERS;
34 reg.openkey('.Default\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
35 Folders'
36 StartMenuDir := reg.ReadString('Start Menu');
37 reg.closekey;
38 reg.free;
39 s := ExtractFilePath(StartMenuDir + '\' + Descr + '.lnk');
40 ForceDirectories(s);
41 if FileExists(StartMenuDir + '\' + Descr + '.lnk') then
42 DeleteFile(StartMenuDir + '\' + Descr + '.lnk');
43 ds := StartMenuDir + '\' + Descr + '.lnk';
44 MyFile.Save(PWChar(ds), false);
45 end;
Solve 2:
46
47 {uses Windows, ShlObj, ActiveX, ComObj, SysUtils, ...}
48
49 type
50 TShellLinkInfo = record
51 PathName: string;
52 Arguments: string;
53 Description: string;
54 WorkingDirectory: string;
55 IconLocation: string;
56 IconIndex: integer;
57 ShowCmd: integer;
58 HotKey: word;
59 end;
60
61 function GetSpecialFolderPath(Folder: Integer; CanCreate: Boolean):
62 string;
63 var
64 FilePath: array[0..MAX_PATH] of char;
65 begin
66 { Get path of selected location }
67 SHGetSpecialFolderPath(0, FilePath, Folder, CanCreate);
68 Result := FilePath;
69 end;
70
71 function TfrmMain.CreateShellLink(const AppName, Desc: string; Dest: Integer):
72 string;
73 { Creates a shell link for application or document specified in AppName with
74 description Desc.
75 Link will be located in folder specified by Dest. Returns the full path name of the
76 link file }
77 var
78 SL: IShellLink;
79 PF: IPersistFile;
80 LnkName: WideString;
81 SName: string;
82 begin
83 OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink,
84 SL));
85 { The IShellLink implementer must also support the IPersistFile interface.
86 Get an interface pointer to it. }
87 PF := SL as IPersistFile;
88 OleCheck(SL.SetPath(PChar(AppName))); {set link path to proper file}
89 if Desc <> '' then
90 OleCheck(SL.SetDescription(PChar(Desc))); {set description}
91 { create a path location and filename for link file }
92 if Desc <> '' then
93 SName := Desc
94 else
95 SName := ExtractFileName(AppName);
96 LnkName := GetSpecialFolderPath(Dest, True) + '\' + ChangeFileExt(SName, '.lnk');
97 PF.Save(PWideChar(LnkName), True); {save link file}
98 Result := LnkName;
99 end;
Usage:
CreateShellLink('c:\MyProgram.exe', 'GKB Server', CSIDL_STARTUP);
The CSIDL_constants:
CSIDL_BITBUCKET RecycleBin
CSIDL_CONTROLS ControlPanel
CSIDL_DESKTOP Desktop
CSIDL_DRIVES My Computer
CSIDL_FONTS Fonts
CSIDL_NETHOOD Network Neighborhood
CSIDL_NETWORK The virtual version of the above
CSIDL_PERSONAL 'Personal'
CSIDL_PRINTERS printers
CSIDL_PROGRAMS Programs in the Start Menu
CSIDL_RECENT Recent Documents
CSIDL_SENDTO Folder SendTo
CSIDL_STARTMENU The whole Start menu
CSIDL_STARTUP The Autostart Group
CSIDL_TEMPLATES Document templates
Look up SHGetSpecialFolderLocation or "ShlObj.pas" for the additional CSIDL_constants.
|