How to create a shortcut on desktop?

Posted on 1999-07-07
Medium Priority
Last Modified: 2010-04-06
Is it possible for me to create a Windows shortcut on the desktop without using other custom components?

Question by:yingkit
  • 3
  • 2

Accepted Solution

fosco earned 30 total points
ID: 1386901

procedure DesktopLnkCreate(LinkName,ExePath,WorkDir,Arguments:string;ShowCmd,HotKey:integer);
  aPidl: PItemIDList;
  desktopDir: string;
  aISL: IShellLink;
  aIPF: IPersistFile;
  CoInitialize(nil); // Must initialize COM or CoCreateInstance won't work
  // Get the desktop folder location (as a PItemIDList)
  if SUCCEEDED(SHGetSpecialFolderLocation(Application.Handle, CSIDL_DESKTOPDIRECTORY, aPidl)) then
    // Get the actual path of the desktop directory from the PItemIDList
    SetLength(desktopDir, MAX_PATH); // SHGetPathFromIDList assumes MAX_PATH buffer
    SHGetPathFromIDList(aPidl, PChar(desktopDir)); // Do it
    SetLength(desktopDir, StrLen(PChar(desktopDir)));

    // Our link is on the desktop.  We need a WideString here to pass to
    // IPersistFile.Save below
    // "My Home-made Shortcut" will be the caption under the shortcut's icon
    UlinkName := desktopDir+'\'+LinkName+'.lnk';

    // Get an IShellLink interface to make the shortcut
    if SUCCEEDED(CoCreateInstance(CLSID_ShellLink, nil,
              CLSCTX_INPROC_SERVER, IID_IShellLinkA, aISL)) then
      // Now we have an IShellLink interface, so we can set it up as we like.
      // Set the path (the program to run) e.g., notepad
      // Set the working directory ("Start in"), e.g., c:\windows
      // Set the command-line params, e.g. test.txt
      // Set the show command, e.g., normal
      aISL.SetShowCmd(ShowCmd); // or SW_SHOWMINIMIZED or SHOW_MAXIMIZED
      // Set the hot-key e.g., Ctrl+Alt+A
      // Set the icon location e.g., the fourth icon in shell32.dll
      // Uncomment to see the effect.  Otherwise, the "normal" icon for the
      // linked program will be shown
      // aISL.SetIconLocation('shell32.dll', 3);

      // The shortcut IShellLink is now all set up.
      // We get an IPersistFile interface from it, and use it to save the link
      aIPF := (aISL as IPersistFile);
      if assigned(aIPF) then
        aIPF.Save(PWideChar(UlinkName), true);

      // Both aISL and aIPF will be released automatically when they go
      // out of scope (i.e., when this routine ends).
  CoUninitialize; // Symmetric uninitialize



Expert Comment

ID: 1386902
if uppercase(s) = '/LNK' then DesktopLnkCreate('Mak',application.exename,'','', SW_SHOW,((HOTKEYF_ALT or HOTKEYF_CONTROL) shl 8) or ord('B'));


Author Comment

ID: 1386903
Hi Fosco,
The compiler gave me the following errors: (I am using Delphi 2)
1) ULinkName:wideString [Undeclared identifier]
2) aPidl: PItemIDList  [Undeclared identifier]
3) aISL: IShellLink; [Undeclared identifier]
4) aIPF: IPersistFile; [Undeclared identifier]
5) SHGetSpecialFolderLocation [Undeclared identifier]
6) CSIDL_DESKTOPDIRECTORY [Undeclared identifier]
7) SHGetPathFromIDList [Undeclared identifier]
8) CoCreateInstance [Undeclared identifier]
9) CLSID_ShellLink [Undeclared identifier]
10) CLSCTX_INPROC_SERVER [Undeclared identifier]
11) IID_IShellLinkA [Undeclared identifier]
12) aISL.SetPath(PChar(ExePath)); [Missing operator or semicolon]
13) aISL.SetWorkingDirectory(PChar(WorkDir)); [Missing operator or semicolon]
many errors, should I include any units before compiling the codes?
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

LVL 20

Expert Comment

ID: 1386904
Hi yingkit, fosco's code is correct, but works only for D3/4. Borland introduced a much enhanced COM implementation in D3. If you have questions here, you should better say, you're working with D2, because it's a quite old version.

This question might help you:


But perhaps fosco has a full solution for D2, too.

Regards, Madshi.
LVL 20

Expert Comment

ID: 1386905
Hey, fosco, welcome on EE. Good experts are always welcome...   :-)

Expert Comment

ID: 1386906
Sorry Yingkit, I dont have Delphi 2 code for that. =(..

Thanks for the welcome, Madshi...


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question