From a developer perspective Windows Vista's UAC can be problematic for some parts of your Delphi application, if the application is not being run by an administartor. One such operation is writing to the Registry database.
Learn how to "request admin rights" by creating an application manifest file....
Windows Vista - User Account ControlUser Account Control is a security component in Windows Vista. UAC enables users to perform common tasks as non-administrators, called standard users in Windows Vista, and as administrators without having to switch users, log off, or use Run As.
To help prevent malicious software from silently installing and causing computer-wide infection, Microsoft developed the UAC feature.
From a developer perspective the following UAC features are important:
- All processes are started as Standard User as default
- A Standard User can not:
- Change files in Program Files folders
- Change files in Windows or System32 folders
- Change registry under HKLM\Software
- Change the local machines date and time
- ...the list continues...
Programmatically Edit Registry to Run Your Delphi Application on Windows StartupBy programmatically editing the Windows Registry, using the TRegistry object, you can you can "automagically" start programs whenever Windows launches.
The procedure you can use to force "auto-run-on-Windows-startup" for your application might look like:
On Vista, if the user running the application does not have admin rights the above code would fail, due to UAC!
procedure RunOnStartup(const sCmdLine: string; bRunOnce: boolean = false; Remove: Boolean = false) ; var sKey: string; Section: string; const ApplicationTitle = ”Your Application TITLE”; begin if (bRunOnce) then sKey := 'Once' else sKey := ''; Section := 'Software\Microsoft\Windows\CurrentVersion\Run' + sKey + #0; with TRegIniFile.Create('') do try RootKey := HKEY_LOCAL_MACHINE; if Remove then DeleteKey(Section, ApplicationTitle) else WriteString(Section, ApplicationTitle, sCmdLine) ; finally Free; end; end;
Faking UAC Rights - How to Request Execution LevelEven if the user running the above code is not an admin, you can, as a developer arm your application with a special kind of embedded resource: application manifest file.
Having the manifest file will ensure Vista's UAC will allow your code to execute.
Here are the steps:
- Create XML file with following content:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0] <assemblyIdentity version="18.104.22.168" processorArchitecture="X86" name="YourApplicationExeName" type="win32"/> <description>elevate execution level</description> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2] <security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> </requestedPrivileges> </security> </trustInfo> </assembly>
- Name this XML file as YourApplicationName.manifest
- Create a text file with following content:
1 24 "YourApplicationName.manifest"
- Name this text file as YourApplicationName.RC
- using the command line execute following command:
brcc32 YourApplicationName.RC -foYourApplicationName.REC
This will create a new resource file called YourApplicationName.REC
- Copy this YourApplicationName.REC file in to the resource path of your application.
- Include this resource file into the DPR of you application, as like:
- Finally build your application - it is now ready to get admin rights on Windows Vista.
Note 2: Above steps create a resource file to be stored inside the EXE file of your application. More on Resources in Delphi applications.
How to Automate the Above "Create Manifest" ProcessTo help you automate the process I have written a small program that does all the dirty work for you.
Use the above DPR file to create build a project.
When you build the project, simple drag your application's DPR file, onto the "require-admin-vista-uac-manifest-builder" exe file and you’ll be up and running in 30 seconds.
Delphi tips navigator:
» MORE TIPS
« IsValidFileName - How to Check if a Given File Name is Valid Using Delphi