Recent

Author Topic: How do create a Windows Shortcut?  (Read 13332 times)

jon.little

  • Guest
How do create a Windows Shortcut?
« on: July 29, 2009, 12:42:48 pm »
Hi

Does anyone know how to create a windows shortcut in Lazarus?

I found example Delphi code here: http://delphi.about.com/od/windowsshellapi/a/create_lnk.htm

But I don't know Lazarus libraries well enough to convert it.

Any help would be appreciated.

Jon

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How do create a Windows Shortcut?
« Reply #1 on: July 30, 2009, 07:56:52 am »
Haven't tried, but after a quick look I guess the code is compilable (perhaps with a {$mode delphi} switch) because at least all required units are present.

jon.little

  • Guest
Re: How do create a Windows Shortcut?
« Reply #2 on: July 30, 2009, 11:40:23 am »
The code works as is ny adding these units:

ShlObj, ActiveX, ComObj,  windows

Cheers

Jon

alejol0

  • Jr. Member
  • **
  • Posts: 60
  • Electronic technician
    • My Facebook page
Re: How do create a Windows Shortcut?
« Reply #3 on: August 24, 2010, 01:05:11 am »
Yes, this topic is old. But maybe this can help some new people.
In Windows, i have success creating shortcuts to files and folders using this code:

Code: [Select]
{In the Unit1 , we need to use Windows and ShlObj. This is not portable to Linux.
uses
 Windows, Classes, SysUtils, FileUtil, Forms, Controls, Graphics,
  Dialogs, StdCtrls, ShlObj, ActiveX, ComObj, ExtCtrls,
  ExtDlgs, Buttons, UnitGlobales; //maybe some of this units are not necessary
}

//Create a Windows shortcut to the calculator pressing the button Button1
Procedure TForm1.Button1
Var
   IObject : IUnknown;
   ISLink : IShellLink;
   IPFile : IPersistFile;
   PIDL : PItemIDList;
   InFolder : array[0..MAX_PATH] of Char;
   TargetName : String;
   LinkName : WideString;   

Begin

  TargetName :='C:\WINDOWS\System32\calc.exe'; //use complete path to desired target
   //Calc.exe is in other path in Windows 98, check.

    IObject := CreateComObject(CLSID_ShellLink) ;
    ISLink := IObject as IShellLink;
    IPFile := IObject as IPersistFile; 

   with ISLink do
    begin
      SetPath(pChar(TargetName)) ;
      SetWorkingDirectory(pChar(ExtractFilePath(TargetName))) ;
    end;

    // if we want to place a link on the Desktop , this function
    //returns the path of the desktop of the current user in Win XP
    SHGetSpecialFolderLocation(0, CSIDL_DESKTOPDIRECTORY, PIDL) ;
    SHGetPathFromIDList(PIDL, InFolder) ;
    //Of course, we can create the link in other path.


    LinkName := InFolder + '\My Calculator Link.lnk' ;//Absolute path and name of the new .lnk file
    //The user only sees "My Calculator Link" as the label of the shortcut.

    IPFile.Save(PWChar(LinkName), false) ; //here the new link is created: a new file
    //named "My Calculator Link.lnk" is created in the user's desktop directory.
   
End;

Hope this helps someone.
Contact me: lavarello1966 (at) gmail . com
or
alejandro.lavarello (at) gmail (dot) com

Francois_C

  • New member
  • *
  • Posts: 9
Re: How do create a Windows Shortcut?
« Reply #4 on: March 03, 2019, 11:51:26 am »
Thanks, alejol0. I'll try your code. Maybe it will help me:

As the native shortcut creator in my Mint LMDE Cinnamon is broken, I have written, for my own use, a Lazarus substitution shortcut creator, which I improved so that it became somewhat better, then a complete drag-drop installer (file copy, change permissions and owner, create shortcut) for apps without packages (especially my own Lazarus apps), and I was thinking of porting it to Windows, but Windows shortcuts are not text files as in Linux, but binaries. Everything is so different that I probably should write another program.

Finding the desktop on Windows is easier than on Linux, because it is an environment variable, and the localized Desktop name is only an alias: on Linux the directory name changes according to the language, and the only place I could read it from was /home/user/.config/user-dirs.dirs.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How do create a Windows Shortcut?
« Reply #5 on: March 03, 2019, 02:01:37 pm »
[...] I have written, for my own use, a Lazarus substitution shortcut creator, which I improved so that it became somewhat better, then a complete drag-drop installer (file copy, change permissions and owner, create shortcut) for apps without packages (especially my own Lazarus apps), [...]

Would you be willing to share your code? I'm working on something similar but it's still on the design phase and I'll be happier if I can avoid reinventing a wheel :)

I'm sure there are quite a lot of others that would love it too.


ETA: It would also make-up for resurrecting an almost 9 years old thread :D
« Last Edit: March 03, 2019, 02:04:14 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018