使用Delphi创建快捷方式并指定其权限级别

时间:2012-09-29 12:30:37

标签: windows delphi winapi

我使用以下代码在Delphi中创建快捷方式。我想在快捷方式的权限级别属性中选中“以管理员身份运行此程序”框中的快捷方式。有没有办法做到这一点?

function MakeShortcut(Dst, Src: String; Desc: String = ''; Arg: String = ''; WorkDir: String = ''; Icon: String = ''; IconI: Integer = 0; Show: Integer = SW_SHOWNORMAL; HotKey: Word = 0): Boolean;
var
  u: IUnknown;
  s: IShellLink;
  f: IPersistFile;
  p: WideString;
begin
try
  u := CreateComObject(CLSID_SHELLLINK);
  s := u as IShellLink;
  f := u as IPersistFile;
  s.SetPath(PChar(Src));
  if (WorkDir = '') then WorkDir := ExtractFileDir(Src);
  s.SetWorkingDirectory(PChar(WorkDir));
  if (Icon = '') then Icon := Src;
  s.SetIconLocation(PChar(Icon), IconI);
  s.SetDescription(PChar(Desc));
  s.SetArguments(PChar(Arg));
  s.SetShowCmd(Show);
  s.SetHotkey(HotKey);
  p := Dst;
  Result := Succeeded(f.Save(PWChar(p), False));
except
  Result := False;
end;
end;

1 个答案:

答案 0 :(得分:4)

您需要使用IShellLinkDataList::SetFlags()传递SLDF_RUNAS_USER

我手边没有任何代码示例。但基本方法是这样的:

sldl := s as IShellLinkDataList;
flags := sldl.GetFlags();
sldl.SetFlags(flags or SLDF_RUNAS_USER);