delphidelphi-10.3-riojedi

Trying to create a ShellLink Shortcut with JclShell in a console app fails


I try to create a ShellLink Shortcut with JclShell in a simple console app in Delphi Rio 10.3.3:

program ShellLinkShortcutHashTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  JclShell,
  System.SysUtils;

const
  ShortcutFile = 'R:\myshortcut.lnk';
  ShortcutTarget = 'C:\Windows\System32\notepad.exe';

function SaveShortcutShellLink(const AFile: string): string;
var
  SL: JclShell.TShellLink;
  HR: Integer;
begin
  Result := 'error';

  SL.Target := ShortcutTarget;
  SL.Description := 'My description';
  HR := JclShell.ShellLinkCreate(SL, AFile);

  Result := IntToStr(HR);
end;

begin
  try
    Writeln(SaveShortcutShellLink(ShortcutFile));
    Readln;
  except
    on E: Exception do
    begin
      Writeln(E.ClassName, ': ', E.Message);
      Readln;
    end;
  end;
end.

However, no ShellLink Shortcut is being created and this is the result:

-2147221008

enter image description here

I've tried different path constants (not write-protected), but it always fails.

My OS: Windows 7 x64 SP1

Creating a ShellLink Shortcut manually in Windows File Explorer in the above directory works without problems.

Is there something wrong with JclShell?


Solution

  • Winapi.ActiveX.OleInitialize(nil);
    try
      Writeln(SaveShortcutShellLink(ShortcutFile));
    finally
      Winapi.ActiveX.OleUninitialize;
    end;
    Readln;