Add programs to the Windows start menu (Views: 711)
Problem/Question/Abstract: How to add programs to the Windows start menu Answer: Solve 1: procedure CreateStartmenuLink(ExeFile, WorkPath, Args, Descr: string); var MyObject: IUnknown; MyLink: IShellLink; MyFile: IPersistFile; ds: WideString; StartMenuDir: string; reg_info: TRegIniFile; reg: TRegistry; s: string; begin reg_Info := TRegIniFile.Create('Software\Microsoft\Windows\CurrentVersion\Explorer'); StartMenuDir := reg_Info.ReadString('Shell Folders', 'Start Menu', ''); reg_Info.Free; s := ExtractFilePath(StartMenuDir + '\' + Descr + '.lnk'); ForceDirectories(s); if FileExists(StartMenuDir + '\' + Descr + '.lnk') then DeleteFile(StartMenuDir + '\' + Descr + '.lnk'); MyObject := CreateComObject(CLSID_ShellLink); MyLink := MyObject as IShellLink; MyFile := MyObject as IPersistFile; MyLink.SetArguments(PChar(Args)); MyLink.SetPath(PChar(ExeFile)); MyLink.SetWorkingDirectory(PChar(WorkPath)); s := ExtractFileName(StartMenuDir + '\' + Descr + '.lnk'); s := copy(s, 1, length(s) - 4); MyLink.SetDescription(PChar(s)); ds := StartMenuDir + '\' + Descr + '.lnk'; MyFile.Save(PWChar(ds), false); reg := TRegistry.Create; reg.RootKey := HKEY_USERS; reg.openkey('.Default\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', true); StartMenuDir := reg.ReadString('Start Menu'); reg.closekey; reg.free; s := ExtractFilePath(StartMenuDir + '\' + Descr + '.lnk'); ForceDirectories(s); if FileExists(StartMenuDir + '\' + Descr + '.lnk') then DeleteFile(StartMenuDir + '\' + Descr + '.lnk'); ds := StartMenuDir + '\' + Descr + '.lnk'; MyFile.Save(PWChar(ds), false); end; Solve 2: {uses Windows, ShlObj, SysUtils, ...} type TShellLinkInfo = record PathName: string; Arguments: string; Description: string; WorkingDirectory: string; IconLocation: string; IconIndex: integer; ShowCmd: integer; HotKey: word; end; function GetSpecialFolderPath(Folder: Integer; CanCreate: Boolean): string; var FilePath: array[0..MAX_PATH] of char; begin { Get path of selected location } SHGetSpecialFolderPath(0, FilePath, Folder, CanCreate); Result := FilePath; end; function CreateShellLink(const AppName, Desc: string; Dest: Integer): string; { Creates a shell link for application or document specified in AppName with description Desc. Link will be located in folder specified by Dest. Returns the full path name of the link file } var SL: IShellLink; PF: IPersistFile; LnkName: WideString; begin OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink, SL)); { The IShellLink implementer must also support the IPersistFile interface. Get an interface pointer to it. } PF := SL as IPersistFile; OleCheck(SL.SetPath(PChar(AppName))); {set link path to proper file} if Desc <> '' then OleCheck(SL.SetDescription(PChar(Desc))); {set description} { create a path location and filename for link file } LnkName := GetSpecialFolderPath(Dest, True) + '\' + ChangeFileExt(AppName, '.lnk'); PF.Save(PWideChar(LnkName), True); {save link file} Result := LnkName; end; Usage: CreateShellLink('c:\programfiles\mycompany\myapp.exe', '', CSIDL_PROGRAMS); Look up SHGetSpecialFolderLocation or "ShlObj.pas" for the CSIDL_constants. |