How to make a Delphi application run as an administrator?

6

I'm using Delphi XE4 and I have no idea how to make an application ask administrative permission to Windows 7 to run, I looked at websites and bolgs, but I did not find the answer! Help me please!

    
asked by Nustler Nustler 29.05.2015 в 21:28

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 as "Manifest.manifest" inside the folder of your project (I recommend that you create a subfolder called "Resources" to place these files).

Then create another text file with the following content:

1 24 "Manifest.manifest"

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

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

BRCC32.exe Manifest.rc

A file called Manifest.res will be automatically created

Then, finally, add the following line in your code:

{$R 'Manifest.res'}

In this way, when you open the executable, if Windows UAC is enabled, you will be required to elevate the privileges.

I hope I have helped! Good luck!

    
29.05.2015 в 23:26
3

To do this you will need to create a custom Manifest file.

  

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

YOU CAN USE THIS FILE HERE

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

  • Create a text file and paste the content below
  • Save in the folder of your project with the name you prefer, as long as you have the .manifest extension, the most common is that the file is named Manifest.manifest
  • Content of the manifest file

    <?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 MANIFEST ON YOUR PROJECT

    Follow the steps below:

  • Click the Project
  • Click the Options
  • In the window that will open select Application
  • GroupBox Runtime Themes Click the ComboBox and select the Use custom manifest
  • The Custom Manifest field below ComboBox will be enabled, click the button with the three dots to select the custom custom that you have prepared
  • Compile your project and the executable will already have the elevation request, you can check this by running the executable or even noticing that your application icon now has that shell in front.
  •   With Manifest you should notice that pressing F9 to run your application in debug mode Delphi will not be able to run your application, the solution is to close the Delphi IDE and open it   again with the Run as administrator

    IMAGES THAT ILLUSTRATE THE STEPS I INDICATED

    Steps 1 to 4

    Step5

    Step6

        
    11.05.2017 в 08:04
    -2

    I've added windows versions in XML if someone needs to:

    <?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>
    
        
    14.11.2018 в 12:29