By: Fredrik Haglund
Abstract: User Account Control is one of the major features in Windows Vista that will have a large impact on your Delphi application because everyone will run your applications as Standard User.
Format: FlashTime: 33:18
Watch the presentation I prepared for Oslo Delphi Clubs meeting in December for learning what UAC is and why it will affect you.
I have only extracted a part of the presentation into this article. It is the content on how you fix it in Delphi.
You can also download the video, example application and slides.
You application should work out of the box in Windows Vista if you have followed the guidelines about how to write a well behaving Windows application that has been in the Windows logo program for years.
The only thing that has really changed is that things that earlier where recommendations is now enforced. If you break the rules Vista has some bold compatibility features that probably will make administrating your application a nightmare.
Requested Execution Level in Delphi
Include information about execution level in your executable to turn of the compatibility mode and signal to Vista if this application should run as a standard user or must be an administrator.
First add a resource with a manifest to your application. Make sure to remove all references to XPMan unit from project!!!
The rc file is compiled to a res file and only contains the resource type (24 for Manifests) and the filename of the resource file to be embedded.
The Manifest file
First section with dependency information is the same as you include today when the XPMan unit to get the new look and feel of the user interface. This part is needed.
The second section with trust info is new.
Windows XP Warning!
Incorrect formatting of Manifest can blue screen Windows XP. Read KB921337.
Many locations are read only for standard user! Do not open files or registry keys with Write flag.
It is important to use a correct path. A lot has moved around in Vista so no hard coded strings in your application. Save data, log files, etc. in the right location using SHGetFolderPath.
Using ShellExecute to start process running as administrator. The Application.Handle delays elevation if the application is minimized preventing the secure desktop to steel focus for the consent dialog when the user is doing something else. Use Application.Handle (or MainForm.Handle if you have applied other patches that make it the application windows)
If you do not use a handle UAC will always give direct foreground elevation.
Using COM class for Admin tasks
Instead of launching a separate application to perform an admin task it is possible to call a COM object that runs elevated from a process that only run as standard user.
The COM Server must be an EXE and the EXE should have requireAdministrator to install COM objects correctly. You have to add two things to the registration:
Creating the elevated COM object
Use a Moniker from the User Process to create an elevated CoClass running in process with admin token. Call it’s methods as usual.
Adding the Shield to your buttons- SetElevationRequiredState
It is very simple to get the Shield icon onto your buttons, links and menu items to indicate that clicking them will require admin rights and that a consent dialog will appear.
Could not retrieve comments. Please try again later.
Download Delphi 10 now!
Webinars on demand!
More social media choices:
Delphi on Google+
@RADTools on Twitter
Server Response from: ETNASC01