开发者

How do I create a manifest file to launch application with admin privileges?

开发者 https://www.devze.com 2023-02-28 06:20 出处:网络
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 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?


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

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>

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

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

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.

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:

  • http://vb6zone.blogspot.com/2010/07/make-my-manifest.html
  • http://sourceforge.net/projects/ummm/
  • http://www.vbforums.com/showthread.php?t=606736
  • http://www.vbforums.com/showthread.php?t=430886


Here's a way to include the manifest at build time of your VB6 app:

  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.

I've been using this for the last few months without problems, on two old legacy applications that needed to run on Windows 7. You may need to experiment on how you save your manifest file though, for me only saving it as UTF8 without BOM worked properly.

To check what exactly gets embedded as manifest this way, you can also use mt.exe to extract the manifest from your compiled exe/dll. That was what helped me finding the BOM problem ...

0

精彩评论

暂无评论...
验证码 换一张
取 消