如何创建一个清单文件以使用管理员权限启动应用程序?

14

我想为我的VB 6.0程序创建一个清单文件,这样当我启动应用程序时,操作系统将要求用户提供管理员权限。

我还想知道如何将其嵌入应用程序中?

2个回答

27

在VB中,您实际上不需要创建清单文件。Windows应用程序清单是一个标准文本文档,格式为XML。您可以在记事本中创建它,并将其保存在应用程序目录中,文件名为YourAppName.exe.manifest

Microsoft在应用程序清单中提供了更多信息。它甚至包括一个示例清单,您可以将其简单地复制并粘贴到空白文本文件中以开始使用。

重要的是,如果您希望应用程序提示用户进行升级,则需要将requestedExecutionLevel设置为requireAdministrator,而不是asInvoker。有关使用UAC与清单的具体信息,请参阅此处

因此,完整的示例可能如下所示:

<?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>

将清单嵌入可执行文件的传统方法是使用Windows SDK中的mt.exe实用程序

VBAccelerator网站还提供了一些关于在VB 6应用程序中嵌入清单的信息。具体来说,它说:

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

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.

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来完成这项工作。我看到过一些声称可以自动嵌入清单文件的实用工具,但我认为其中大多数是旧的实用工具,只能处理请求v6的ComCtl32.dll。我不确定它们是否容易扩展以包括UAC权限,但值得一试。以下是一些要查看的链接:


MMM可以创建超越CC6和执行级别选择的清单,以创建用于无需注册的COM和DLL重定向的条目。UMMM试图克隆其操作,并在很大程度上成功。MMM肯定可以嵌入清单。还有第三方商业工具。 - Bob77
2
@Bob:如果你对使用这些工具嵌入清单的细节了解更多,请随时发布答案。你不会抢我的风头。我从未在VB 6中构建需要提升的应用程序,因此我从未让这些工具经过测试。我模糊地记得在XP时代使用类似MMM的东西使我的测试周期更容易,但我总是在部署之前回到mt.exe - Cody Gray
1
@Cody Gray:您说得完全正确。通过使用mt.exe,我们可以轻松地嵌入清单。 - nightfire001
@Cody:不,我认为你已经很好地涵盖了目前的目的。还有许多其他问题,但它们与进程提升以外的特定情况有关。例如,选择CC6意味着您需要在第一次加载表单之前先加载shell32.dll和comctl32.dll,以避免致命冲突。但那是另一个话题了。 - Bob77
@Bob:你的意思是说你需要明确地加载shell32.dll吗?我不知道这一点。在我没有这样做的项目中,我从来没有遇到过任何问题。我本以为DLL会自动放置在VB 6应用程序的导入表中,因为它被后台运行时使用。 - Cody Gray
显示剩余2条评论

7
以下是关于如何在VB6应用程序的构建时包含清单的方法:

以下是一种在VB6应用程序的构建时包含清单的方法:

  1. Save your manifest file, maybe call it "my.manifest".

  2. Make sure the file size is a multiple of four, as Cody already mentioned. If necessary, pad it with blanks at the end.
    See also: Embedding an application manifest into a VB6 exe

  3. Generate a resource-file "my.rc" with the following content:

     #define CREATEPROCESS_MANIFEST_RESOURCE_ID 1
     #define RT_MANIFEST 24
     CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "my.manifest"
    

    You can find more information on the constants used at the msdn blogs. Seems you'd need to use

     #define ISOLATIONAWARE_MANIFEST_RESOURCE_ID 2
    

    for a dll project.

  4. Use rc.exe to compile this .rc file to a .res file.

  5. Add the resulting .res file to your VB6 project.

在过去的几个月中,我一直在使用它,并没有遇到任何问题,这是为了运行在Windows 7上的两个旧的遗留应用程序。不过你可能需要尝试一下如何保存你的清单文件,因为只有将其保存为UTF8而不带BOM才能正常工作。

为了检查以这种方式嵌入的清单文件的确切内容,您还可以使用mt.exe从已编译的exe / dll中提取清单。这就是帮助我找到BOM问题的方法...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接