Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create a shortcut on desktop?

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

Thanks.
0
Comment
Question by:yingkit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Accepted Solution

by:
fosco earned 30 total points
ID: 1386901
yingkit,

procedure DesktopLnkCreate(LinkName,ExePath,WorkDir,Arguments:string;ShowCmd,HotKey:integer);
var
  ULinkName:wideString;
  aPidl: PItemIDList;
  desktopDir: string;
  aISL: IShellLink;
  aIPF: IPersistFile;
begin
  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
  begin
    // 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
    begin
      // 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
      aISL.SetPath(PChar(ExePath));
      // Set the working directory ("Start in"), e.g., c:\windows
      aISL.SetWorkingDirectory(PChar(WorkDir));
      // Set the command-line params, e.g. test.txt
      aISL.SetArguments(PChar(Arguments));
      // 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
      aISL.SetHotKey(HotKey);
      // 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).
    end;
  end;
  CoUninitialize; // Symmetric uninitialize

end;

0
 

Expert Comment

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


0
 

Author Comment

by:yingkit
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]
etc........
many errors, should I include any units before compiling the codes?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 20

Expert Comment

by:Madshi
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:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10142923 

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

Regards, Madshi.
0
 
LVL 20

Expert Comment

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

Expert Comment

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

Thanks for the welcome, Madshi...

fosco
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

664 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