How do I make a Delphi application run as an administrator?

Asked

Viewed 7,489 times

6

I’m using Delphi XE4 and have no idea how to get an application to ask administrative permission for Windows 7 to run, I searched websites and bolgs, but I couldn’t find the answer! Help me please!

3 answers

7

First you should create a text file with the following content:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity type="win32" name="Teste" version="3.1.0.0" processorArchitecture="*"/>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" publicKeyToken="6595b64144ccf1df" language="*" processorArchitecture="*"/>
    </dependentAssembly>
  </dependency>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
        </requestedPrivileges>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!--The ID below indicates application support for Windows Vista -->
      <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
      <!--The ID below indicates application support for Windows 7 -->
      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
      <!--Windows 8 Support -->
      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
    </application>
  </compatibility>
</assembly>

Save with the name "Manifest.manifest" inside the folder of your project (I recommend that you create a subfolder called "Resources", to place these files).

After that create another text file, with the following content:

1 24 "Manifest.manifest"

Save this other file with the name Manifest.rc, in the same folder where you saved the file "Manifest.manifest" above.

Then after that Compile this file using the following command line (or create a BAT for it):

BRCC32.exe Manifest.rc

A file called Manifest will be automatically created.res

Then, lastly, add the following line to your code:

{$R 'Manifest.res'}

This way, when opening the executable, if the Windows UAC is enabled, the privilege upgrade will be required.

I hope I helped! Good luck!

3

To do this you will need to create a Manifest customized.

Tip: To learn more about Manifest customized you can give a look at this link from Embarcadero: Customizing the Windows Application Manifest File


YOU CAN USE THIS FILE HERE

If you prefer you can use this ready aquivo, just do the following:

  1. Create a text file and paste the content below
  2. Save to the folder of your project with the name you prefer, as long as it has the extension .manifest, the most common is that the file has the name Manifest.

Contents of the manifest

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity type="win32" name="Teste" version="3.1.0.0" processorArchitecture="*"/>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" publicKeyToken="6595b64144ccf1df" language="*" processorArchitecture="*"/>
    </dependentAssembly>
  </dependency>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
        </requestedPrivileges>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!--The ID below indicates application support for Windows Vista -->
      <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
      <!--The ID below indicates application support for Windows 7 -->
      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
      <!--Windows 8 Support -->
      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
    </application>
  </compatibility>
</assembly>

USING THE MANIFEST IN YOUR PROJECT

Follow the steps below:

  1. Click on the menu Project
  2. Click on the option Options
  3. In the window that will open select Application
  4. In the Groupbox Runtime Themes click on Combobox and select the option Use custom manifest
  5. The field Custom Manifest below the Combobox will be enabled, click the button with the three dots to select the file manifest customized that you prepared
  6. Compile your project and the executable will already have the upgrade request, you can check this by running the executable or even noting that the icon of your application now has that shield in front.

IMPORTANT: With the Manifest you should realize that by pressing F9 to run your application in mode debug Delphi will not be able to run its application, the solution to this is to close the Delphi IDE and open it again with the option Run as administrator

IMAGES THAT ILLUSTRATE THE STEPS I INDICATED

Steps 1 to 4

inserir a descrição da imagem aqui

Step 5

inserir a descrição da imagem aqui

Step 6

inserir a descrição da imagem aqui

-2

I added versions of windows in XML, in case someone needs:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity type="win32" name="NOME DO APLICATIVO COM A EXTESÃO (.EXE)" version="3.1.0.0" processorArchitecture="*"/>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" publicKeyToken="6595b64144ccf1df" language="*" processorArchitecture="*"/>
    </dependentAssembly>
  </dependency>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
        </requestedPrivileges>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!--Windows Vista/6.0/Server 2008-->
      <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
      <!--Windows 7/6.1/Server 2008 R2-->
      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
      <!--Windows 8/6.2/Server 2012-->
      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
      <!--Windows 8.1/6.3/Server 2012 R2-->            
      <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
      <!--Windows 10/10.*-->            
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
    </application>
  </compatibility>   
</assembly>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.