Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: Where is the shortcut that started the application? [duplicate]

Tags:

delphi

I'm aware that the directory where the current executable is located can be found using Application.Exename.

But when the application was started using a shortcut in another directory? Can I find the address of THAT directory, where the shortcut is, as I'd like to create some files there?

Using XE2. Many thanks.

like image 720
Giomach Avatar asked Dec 28 '25 13:12

Giomach


1 Answers

You can do that using GetStartupInfo with the STARTF_TITLEISLINKNAME flag:

const
  STARTF_TITLEISLINKNAME = $800;

function GetShortcutName(out LinkName: string): Boolean;
var
  si: TStartupInfo;
begin
  Result := False;
  FillChar(si, SizeOf(TStartupInfo), 0);
  GetStartupInfo(si);
  if (si.dwFlags and STARTF_TITLEISLINKNAME) <> 0 then
  begin
    Result := True;
    LinkName := si.lpTitle;
  end;
end;

Test code (tested on Win7 64 with XE8 and Delphi 10 Seattle - not tested on Win8 or 10):

program GetShortCutTest;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  Windows;

var
  sLink: string;

begin
  if GetShortcutName(sLink) then
    WriteLn('Shortcut: ' + sLink)
  else
    WriteLn('Not run from shortcut.');

  ReadLn;
end.

You can test it by running the test app (which will show 'Not run from shortcut.'), and then creating a shortcut to the test app and executing that shortcut (which then shows 'Shortcut: ' and the name of the .lnk file).

like image 86
Ken White Avatar answered Dec 31 '25 11:12

Ken White