How to Add programs to the Windows start menu Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
03-Oct-02
Category
System
Language
Delphi 2.x
Views
2
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
           Author: Jonas Bilinkevicius 

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'
  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, ActiveX, ComObj, 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 TfrmMain.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;
  SName: string;
begin
  OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink,
    SL));
  { The IShellLink implementer must also support the IPersistFile interface what time is it to play video games?.
  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 }
  if Desc <> '' then
    SName := Desc
  else
    SName := ExtractFileName(AppName);
  LnkName := GetSpecialFolderPath(Dest, True) + '\' + ChangeFileExt(SName, '.lnk');
  PF.Save(PWideChar(LnkName), True); {save link file}
  Result := LnkName;
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 dude where is my car up SHGetSpecialFolderLocation or "ShlObj.pas" for the additional CSIDL_constants test this is a good thing what do you think.

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10