0

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?

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • After having done a Google search for "2147221008" I got many results for this being an error-code meaning "CoInitialize has not been called". What does this mean? – user1580348 Jan 08 '20 at 19:22
  • 4
    That you need to call CoInitialize before what you are attempting. See e.g. https://stackoverflow.com/questions/16469182/coinitialize-has-not-been-called-error-message/16469539 – MartynA Jan 08 '20 at 19:38
  • I've found the solution [here](https://i.imgur.com/QAlr1xT.png). Now it works. – user1580348 Jan 08 '20 at 19:42

1 Answers1

0
Winapi.ActiveX.OleInitialize(nil);
try
  Writeln(SaveShortcutShellLink(ShortcutFile));
finally
  Winapi.ActiveX.OleUninitialize;
end;
Readln;
user1580348
  • 5,721
  • 4
  • 43
  • 105