Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 798
nightfire001 Avatar asked Apr 22 '11 12:04

nightfire001


People also ask

How do I run an application with elevated privileges?

Open Start. Right-click the app (anywhere on the menu). Select the More submenu, and click the Run as administrator option.

How do you create a manifest file?

You can tell Visual Studio to generate a manifest file for a particular project in the project's Property Pages dialog. Under Configuration Properties, select Linker > Manifest File > Generate Manifest. By default, the project properties of new projects are set to generate a manifest file.


1 Answers

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
like image 177
Cody Gray Avatar answered Oct 27 '22 21:10

Cody Gray