如何从具有“以管理员身份运行”的基于 WiX 的安装程序安装桌面快捷方式(到批处理文件)启用?

发布于 2024-08-28 23:05:48 字数 1247 浏览 1 评论 0原文

我正在从基于 WiX 的安装程序安装桌面快捷方式(到批处理文件) - 如何在启用“以管理员身份运行”设置的情况下自动配置此快捷方式?目标操作系统是Windows Server 2008 R2,并且安装程序正在以提升的权限运行。

更新:
感谢@Anders 提供的链接,我得以完成此工作。我需要在 C# CustomAction 中执行此操作,因此这里是代码的 C# 版本:

namespace CustomAction1
{
 public class CustomAction1
 {
  public bool MakeShortcutElevated(string file_)
  {
   if (!System.IO.File.Exists(file_)) { return false; }

   IPersistFile pf = new ShellLink() as IPersistFile;
   if (pf == null) { return false; }

   pf.Load(file_, 2 /* STGM_READWRITE */);
   IShellLinkDataList sldl = pf as IShellLinkDataList;
   if (sldl == null) { return false; }

   uint dwFlags;
   sldl.GetFlags(out dwFlags);
   sldl.SetFlags(dwFlags | 0x00002000 /* SLDF_RUNAS_USER */);
   pf.Save(null, true);
   return true;
  }
 }

 [ComImport(), Guid("00021401-0000-0000-C000-000000000046")]
 public class ShellLink { }

 [ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("45e2b4ae-b1c3-11d0-b92f-00a0c90312e1")]
 interface IShellLinkDataList
 {
  void AddDataBlock(IntPtr pDataBlock);
  void CopyDataBlock(uint dwSig, out IntPtr ppDataBlock);
  void RemoveDataBlock(uint dwSig);
  void GetFlags(out uint pdwFlags);
  void SetFlags(uint dwFlags);
 }
}

I'm installing a desktop shortcut (to a batch file) from a WiX-based installer -- how do I automatically configure this shortcut with the "Run as Administrator" setting enabled? The target OS is Windows Server 2008 R2, and the installer is running with elevated priveleges.

Update:
Thanks to the link provided by @Anders, I was able to get this working. I needed to do this in a C# CustomAction, so here is the C# version of the code:

namespace CustomAction1
{
 public class CustomAction1
 {
  public bool MakeShortcutElevated(string file_)
  {
   if (!System.IO.File.Exists(file_)) { return false; }

   IPersistFile pf = new ShellLink() as IPersistFile;
   if (pf == null) { return false; }

   pf.Load(file_, 2 /* STGM_READWRITE */);
   IShellLinkDataList sldl = pf as IShellLinkDataList;
   if (sldl == null) { return false; }

   uint dwFlags;
   sldl.GetFlags(out dwFlags);
   sldl.SetFlags(dwFlags | 0x00002000 /* SLDF_RUNAS_USER */);
   pf.Save(null, true);
   return true;
  }
 }

 [ComImport(), Guid("00021401-0000-0000-C000-000000000046")]
 public class ShellLink { }

 [ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("45e2b4ae-b1c3-11d0-b92f-00a0c90312e1")]
 interface IShellLinkDataList
 {
  void AddDataBlock(IntPtr pDataBlock);
  void CopyDataBlock(uint dwSig, out IntPtr ppDataBlock);
  void RemoveDataBlock(uint dwSig);
  void GetFlags(out uint pdwFlags);
  void SetFlags(uint dwFlags);
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

給妳壹絲溫柔 2024-09-04 23:05:48

我猜您需要自定义操作并自行调用 COM 接口。查询 IShellLink(或 IPersistFile?)以获取 IShellLinkDataList,然后: IShellLinkDataList->SetFlags(orgFlagsFromGetFlags|SLDF_RUNAS_USER);

编辑:Raymond 在他的 博客

I'm guessing you would need a custom action and call the COM interfaces on your own. Query IShellLink (or IPersistFile?) for IShellLinkDataList, then: IShellLinkDataList->SetFlags(orgFlagsFromGetFlags|SLDF_RUNAS_USER);

Edit: Raymond has full sample code on his blog

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文