Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 尝试在控制台应用程序中使用JclShell创建ShellLink快捷方式失败_Delphi_Delphi 10.3 Rio_Jedi - Fatal编程技术网

Delphi 尝试在控制台应用程序中使用JclShell创建ShellLink快捷方式失败

Delphi 尝试在控制台应用程序中使用JclShell创建ShellLink快捷方式失败,delphi,delphi-10.3-rio,jedi,Delphi,Delphi 10.3 Rio,Jedi,我尝试在Delphi Rio 10.3.3中的一个简单控制台应用程序中使用JclShell创建ShellLink快捷方式: program ShellLinkShortcutHashTest; {$APPTYPE CONSOLE} {$R *.res} uses JclShell, System.SysUtils; const ShortcutFile = 'R:\myshortcut.lnk'; ShortcutTarget = 'C:\Windows\System32

我尝试在Delphi Rio 10.3.3中的一个简单控制台应用程序中使用
JclShell
创建ShellLink快捷方式:

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.
但是,没有创建ShellLink快捷方式,结果如下:

-2147221008

我尝试了不同的路径常量(不写保护),但总是失败

我的操作系统:Windows 7 x64 SP1

在上述目录的Windows文件资源管理器中手动创建ShellLink快捷方式不会出现问题


JclShell
有什么问题吗?

在谷歌搜索了“2147221008”之后,我得到了很多结果,因为这是一个错误代码,意思是“没有调用CoInitialize”。这意味着什么?您需要在尝试之前调用CoInitialize。我找到了解决办法。现在它起作用了。
Winapi.ActiveX.OleInitialize(nil);
try
  Writeln(SaveShortcutShellLink(ShortcutFile));
finally
  Winapi.ActiveX.OleUninitialize;
end;
Readln;