如何创建清单文件以启动具有管理员权限的应用程序? [英] How do I create a manifest file to launch application with admin privileges?

查看:32
本文介绍了如何创建清单文件以启动具有管理员权限的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的 VB 6.0 程序创建一个清单文件,这样当我启动我的应用程序时,操作系统应该向用户询问管理员权限.

I want to create a manifest file for my VB 6.0 program, so that when I launch my application, the OS should ask the user for administrator privilege.

我也想知道如何将它嵌入到应用程序中?

I also want to know how it can be embedded in the application?

推荐答案

您实际上并没有在 VB 中创建清单文件.Windows 应用程序清单是标准文本文档,格式为 XML.您可以在记事本中创建它并使用适当的文件名将其保存在应用程序的目录中 (YourAppName.exe.manifest).

You don't actually create the manifest file in VB. A Windows application manifest is a standard text document, formatted as XML. You can create it in Notepad and save it with the appropriate file name in your application's directory (YourAppName.exe.manifest).

Microsoft 在此处提供了更多信息:应用程序清单.它甚至包含一个示例清单,您只需将其复制并粘贴到空白文本文件中即可开始使用.

Microsoft has more information available here: Application Manifests. It even includes a sample manifest that you can simply copy and paste into a blank text file to get started.

重要的是,如果您希望您的应用程序提示用户提升,请将 requestedExecutionLevel 设置为 requireAdministrator,而不是asInvoker.此处提供了有关使用 UAC 清单的具体信息.

The important thing, if you want your application to prompt the user for elevation, is to set the requestedExecutionLevel to requireAdministrator, rather than asInvoker. Specific information on using manifests with UAC is available here.

所以一个完整的样本可能看起来像这样:

So a full sample might look something like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 
  <assemblyIdentity
     version="1.0.0.0"
     processorArchitecture="*"
     name="MyMagicalApplication"
     type="win32"
  /> 
  <description>Sample manifest for your super cool application</description> 

  <!-- Request version 6 of the common controls. -->
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
        type="win32"
        name="Microsoft.Windows.Common-Controls"
        version="6.0.0.0"
        processorArchitecture="*"
        publicKeyToken="6595b64144ccf1df"
        language="*"
      />
    </dependentAssembly>
 </dependency>

  <!-- Identify the application security requirements. -->
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="false"
        />
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

将清单嵌入可执行文件的传统方法是使用 mt.exe 实用程序,作为 Windows SDK 的一部分提供.

The traditional way to embed a manifest into an executable is using the mt.exe utility, available as part of the Windows SDK.

VBAccelerator 站点也有一些有关在 VB 6 应用程序中嵌入清单的信息.具体来说,它说:

The VBAccelerator site also has some information about embedding manifests in a VB 6 application. Specifically, it says:

提供清单有两种方法:最简单(但最不优雅)的方法是在磁盘上为可执行文件提供清单.假设您的应用程序名为 TimeSlot.exe.那么如果你将上面的清单 XML 保存为

There are two ways to provide the manifest: the simplest (but least elegant) way is to provide the manifest on disk for an executable. Let's say your application is called TimeSlot.exe. Then if you save the manifest XML above as

TimeSlot.exe.manifest

在与可执行文件相同的目录下,TimeSlot.exe 会自动获取 XP 样式.提供了 VB5 和 VB6 示例.如果在运行应用程序之前重命名 .manifest 文件,则可以关闭 XP 样式.

in the same directory as the executable, TimeSlot.exe will automatically get the XP styles. VB5 and VB6 examples are provided. If you rename the .manifest file prior to running the app, you can switch off the XP styles.

更强大的方法是将清单编译为应用程序中的资源.为此,清单必须显示为资源类型 RT_MANIFEST (24),id 为 CREATEPROCESS_MANIFEST_RESOURCE_ID (1).出于某种奇怪的原因,您必须还确保生成的 XML 文件的长度是 4 个字节的偶数倍.因此,例如,如果您的文件实际上是 597 字节,则需要在编译前添加填充空间以将文件大小弥补为 600 字节.资源示例演示了如何使用资源编译器脚本(.rc 文件)和 RC.exe 创建此资源文件.

A more robust method is to compile the manifest as a resource in your application. To do this, the manifest must appear as resource type RT_MANIFEST (24) with id CREATEPROCESS_MANIFEST_RESOURCE_ID (1). For some bizarre reason, you must also ensure that the resulting XML file is an even multiple of 4 bytes long. So for example, if your file is actually 597 bytes you need to add padding spaces to make up the file size to 600 bytes before compiling. The Resource examples demonstrate how to create this resource file using a resource compiler script (.rc file) and RC.exe.

但是,如果您想在从 VB 6 IDE 构建应用程序时自动嵌入清单,那么您将面临更多困难.VB 6 IDE 不支持构建后步骤,因此您不能简单地在命令行上运行 mt.exe 来为您完成.我在网上看到了一些声称可以为您自动嵌入清单的实用程序,但我相信其中大多数是旧实用程序,它们只处理请求的 ComCtl32.dll v6.我不确定它们是否可以轻松扩展以包含 UAC 权限,但值得一试.以下是一些可以查看的链接:

But if you want to embed the manifest automatically when you build your application from the VB 6 IDE, you're in for a little more difficulty. The VB 6 IDE doesn't support post-build steps, so you can't simply run mt.exe on the command line to do it for you. There are a couple of utilities I've seen around the web that claim to automatically embed manifests for you, but I believe most of these are older utilities that only handle requesting v6 of ComCtl32.dll. I'm not sure if they're easily extensible to include the UAC permissions as well, but it's worth a shot. Here are some links to check out:

这篇关于如何创建清单文件以启动具有管理员权限的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆