BY CRAIG CHAPMAN · PUBLISHED 2015-06-08 · UPDATED 2015-06-08

 

One of my customers recently asked the question “How can I force my application to run as administrator, without the user having to select this option?” Good question…

In this post, I’ll be using RAD Studio to create a Delphi application, however, the same technique should work equally well for a C++ Builder project.

Around the launch of Windows Vista, I’d heard about UAC (User Access Control) and manifest files, but I’ve never really had cause to work with them. The applications that I’ve worked on have not required administrative privileges, nor the theming benefits of using manifest files. So I’m inexperienced with them.

I began searching around for information, and found lots of older solutions which didn’t exactly work, either because they’re for older versions of Delphi / Rad Studio, or because they’re for older windows versions than I’m using. Eventually, I was able to work out a method that does work, and I’d like to share that with you…

Start by creating a new VCL application, and save the project. You can name the project as you wish, but you’ll need it’s name later. For this example I’ve named my project “ManifestTesting.” Also, be sure it’s a VCL application, this technique should work equally well for FMX applications running on windows, however, it does apply to windows only, so FMX is not necessary here.

Now, you’ll need to create a new file containing the XML manifest code required to request elevated permissions.

Select File / New / Other..

Under “Other Files” select “Text File”

Type in “.manifest” as the extension for your file, in the dialog…

When you click “Ok”, a new file named ‘New1.manifest’ will appear under your project. Right click on it, and select “Save As”

Save the file with the same name as your project, but a .manifest extension. So, in this example, save it as “ManifestTesting.manifest”

Now, we need some content for this file. Open the file in the IDE, and paste the following…

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity type="win32" name="ManifestTesting" version="1.0.0.0" processorArchitecture="x86"/>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
publicKeyToken="6595b64144ccf1df"
language="*"
processorArchitecture="*"/>
</dependentAssembly>
</dependency>
<!-- Windows Vista application security requirements. -->
<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 7-->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
<!--Windows Vista-->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
</application>
</compatibility>
</assembly>

Notice the tag name=”ManifestTesting” on line 3. You need to alter this to the name of your own application.

Save this file, and now right click on your project file and select “Options”

Now under the ‘Application’ category in the tree, select the option to “Use custom manifest”

The box named “Custom Manifest” (beneath the drop-down) has an associated button, click it to browse for and locate your “ManifestTesting.manifest” file…

After you click “Ok” a few times to dismiss the dialogs, build your project and attempt to run it.

You’ll see an error message:

This is because your application now requires Administrative rights to run, but the IDE is not running as Administrator, and is therefore unable to launch the process.

If you attempt to run the executable outside the IDE, you’ll be prompted to permit it to run as Administrator, which is the desired result, but how can we resolve this issue using the IDE?

You might have noticed that when we elected to use a custom manifest file in the project options, we did so under the debug profile…

Go and restore this option to “Enable runtime themes”, then change the “Target” dialog to a release profile and set “Use custom manifest” for that profile. Again, select the custom manifest file. You’ll then have a debug profile which does not request Administrative privileges, and a release profile which does.

Another alternative (if you require Administrative rights during debug), is to run your IDE as administrator, which you could do permanently by appropriately altering it’s short-cut. Note, this is not advised, you should only use administrative rights for debugging those features which specifically require elevated rights.

http://chapmanworld.com/2015/06/08/elevated-privileges-for-delphi-applications/

Elevated privileges for Delphi applications的更多相关文章

  1. Real time profiler for Delphi applications

    xalion提供的资源,这么强,还是免费的,快去试用!   ✓  Detailed debug information (internal, TDS, MAP) ✓  Display informat ...

  2. [Windows Azure] Windows Azure Web Sites, Cloud Services, and VMs: When to use which?

    This document provides guidance on how to make an informed decision in choosing between Windows Azur ...

  3. Delphi WebBrowser控件的使用(大全 good)

    Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application      如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...

  4. Delphi TWebBrowser

    Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application      如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...

  5. Delphi资源大全

    A curated list of awesome Delphi frameworks, libraries, resources, and shiny things. Inspired by awe ...

  6. Awesome Delphi

    Awesome Delphi  A curated list of awesome Delphi frameworks, libraries, resources, and shiny things. ...

  7. 开源的Delphi性能调试工具

    官网:http://dbg-spider.net/源码:https://github.com/yavfast/dbg-spider Real time profiler for Delphi appl ...

  8. Taking a screen shot of a window using Delphi code is rather easy.

    Taking a screen shot of a window using Delphi code is rather easy. A screen shot (screen capture) is ...

  9. From MSI to WiX, Part 1 - Required properties, by Alex Shevchuk

    Following content is directly reprinted from From MSI to WiX, Part 1 - Required properties Author: A ...

随机推荐

  1. 【BZOJ 1026】 [SCOI2009]windy数

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1026 [题意] [题解] 数位Dp 设f[i][j]表示长度为i,第一位(也就是最高位 ...

  2. TensorFlow 实战(三)—— 实现常见公式

    tf.reduce_mean (求向量的均值)等价于 1N∑i=1Nxi 1. 对权值矩阵进行 l2 正则 def variable_with_weight_loss(shape, stddev, w ...

  3. Java中的集合Map、HashMap、Hashtable、Properties、SortedMap、TreeMap、WeakHashMap、IdentityHashMap、EnumMap(五)

    Map Map用于保存具有映射关系的数据,因此Map集合里保存着两组值,一组值用于保存Map里的key,另一组值用于保存Map里的value,key和value都可以是任何引用类型的数据.Map的ke ...

  4. 数据批量插入MSSQL

    MSSQL数据批量插入优化详细   序言 现在有一个需求是将10w条数据插入到MSSQL数据库中,表结构如下,你会怎么做,你感觉插入10W条数据插入到MSSQL如下的表中需要多久呢? 或者你的批量数据 ...

  5. numpy 辨异(三)—— hstack/column_stack,linalg.eig/linalg.eigh

    1. np.hstack np.column_stack >>> np.hstack([np.array([1, 2, 3]), np.array([4, 5, 6])]) arra ...

  6. Codeforces 449 B. Jzzhu and Cities

    堆优化dijkstra,假设哪条铁路能够被更新,就把相应铁路删除. B. Jzzhu and Cities time limit per test 2 seconds memory limit per ...

  7. Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博

    原文:Android 调用系统分享文字.图片.文件,可直达微信.朋友圈.QQ.QQ空间.微博 兼容SDK 18以上的系统,直接调用系统分享功能,分享文本.图片.文件到第三方APP,如:微信.QQ.微博 ...

  8. AdaBoost算法原理及OpenCV实例

    备注:OpenCV版本 2.4.10 在数据的挖掘和分析中,最基本和首要的任务是对数据进行分类,解决这个问题的常用方法是机器学习技术.通过使用已知实例集合中所有样本的属性值作为机器学习算法的训练集,导 ...

  9. XenServer重置存储管理SR至Ext3

    Xenserver默认安装到本地磁盘,它是基于本地磁盘上Linux的LVM所管理,在xenserver创建为LVM Typer的本地SR,xenserver在LVM上不支持Thin Provision ...

  10. 经典c开源项目

    1. Webbench Webbench是一个在Linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连 ...