Desktop shortcuts

BlitzPlus Forums/BlitzPlus Beginners Area/Desktop shortcuts

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

This is the proper way to do it BUT the code is in Delphi 3. If anyone has the time to make it into a DLL then away you go.

procedure ccCreateDesktopShortcut(ObjectPath, Description: String; HotKey: Word); //JB
var
  MyObject  : IUnknown;
  MySLink   : IShellLink;
  MyPFile   : IPersistFile;
  Directory : String;
  WFileName : WideString;
  MyReg     : TRegIniFile;
begin
  //Hotkey ... Best to pass an Ord('Key') and let this function apply Ctrl+Alt to it
  //Open up a COM Object to create the shell link (shortcut)
  MyObject := CreateComObject(CLSID_ShellLink);
  MySLink := MyObject as IShellLink;
  MyPFile := MyObject as IPersistFile;
  with MySLink do begin
      //SetArguments(''); //not used
    If HotKey<>0 then //don't create a blank hotkey! JB 09/10/00
      SetHotKey(HotKey+HOTKEYF_ALT*256+HOTKEYF_CONTROL*256); //*256 to get high order byte
    SetPath(PChar(ObjectPath));
    SetWorkingDirectory(PChar(ExtractFilePath(ObjectPath))); //sensible
  end;

  //Open the Registry to find out the Desktop Path
  MyReg := TRegIniFile.Create(
    'Software\MicroSoft\Windows\CurrentVersion\Explorer');

  Try
    Directory := MyReg.ReadString('Shell Folders','Desktop','');

    //Create the Link !
    WFileName := Directory + '\' + Description + '.lnk';
    MyPFile.Save(PWChar(WFileName),False);
  Finally
    MyReg.Free;
  End;
end;

/////////////////////////////
procedure ccCreateStartMenuShortcut(ObjectPath, ShortcutSubPath, Description: String); //JB
var
  MyObject  : IUnknown;
  MySLink   : IShellLink;
  MyPFile   : IPersistFile;
  Directory : String;
  WFileName : WideString;
  MyReg     : TRegIniFile;
begin
  //Specify the ShortcutSubPath like this: Program Files\My Folder

  //Open up a COM Object to create the shell link (shortcut)
  MyObject := CreateComObject(CLSID_ShellLink);
  MySLink := MyObject as IShellLink;
  MyPFile := MyObject as IPersistFile;
  with MySLink do begin
      //SetArguments(''); //not used
    SetPath(PChar(ObjectPath));
    SetWorkingDirectory(PChar(ExtractFilePath(ObjectPath))); //sensible
  end;

  //Open the Registry to find out the Start Menu Path
  MyReg := TRegIniFile.Create(
    'Software\MicroSoft\Windows\CurrentVersion\Explorer');

  Try
    //Make Start Menu sub-folder
    Directory := MyReg.ReadString('Shell Folders','Start Menu','')
      + '\' + ShortcutSubPath;
    CreateDir(Directory);

    //Create the Link !
    WFileName := Directory + '\' + Description + '.lnk';
    MyPFile.Save(PWChar(WFileName),False);
  Finally
    MyReg.Free;
  end;
end;