1

I am developing software on Delphi XE for Windows 10 and I must run my software with administrator's privileges on 10. If I right click on the exe file and run it as an administrator, it doesn't raise any error, but if I run it in any other way the program raises error message Access Denied. So, it needs to have admin status. Initially I thought you could use task scheduler to accomplish this but it has issues as well. Now, I am learning I need to have custom manifest to do this on Windows 10. So after going through some stackoveflow questions, blogs and websites online, I created a manifest for my program as follows, but STILL when I run my program it raises Access Denied error message.

This is what I did:

Created the manifest file (GeoMonitor.manifest) using NOTEPAD:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestversion="1.0">
<assemblyIdentity version="1.1.1.1">
processorArchitecture="*"
name="GeoMonitor"
type="win32" />
<description>elevate execution level</description>
<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.v2"><security>
<requestedPrivileges>
<requestedExecutionlevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustinfo>
</assembly>

Created RC file GeoMonitor.rc using NOTEPAD

1 24 GeoMonitor.manifest

Linked both of these files to my project file after I unchecked the option Enable Run Time Theme in the project options.

enter image description here

Finally I complied my project files with no problem and then tested the complied exe file on Windows 10. It is still running as a user not as administrator. What am I doing wrong?

UPDATE: Is there anything I have to set in my project option - resource compiler section?

enter image description here

ThN
  • 3,235
  • 3
  • 57
  • 115
  • How did you "link both of these files"? Did you look at the [second answer to this question](http://stackoverflow.com/a/6227554/62576), including the comments? – Ken White Jun 22 '15 at 20:19
  • @KenWhite I actually added these files to my program project files by right clicking on the project name and clicking the add options in the popup menu. Also, I noticed that RC file was linked to my DPR file like so `{.$R 'GeoMonitor.res' 'GeoMonitor.rc'}'` I read somewhere online that you don't need to compile RC file separately that Delphi compiler will do that for you. – ThN Jun 23 '15 at 12:52
  • With the dot in there (`{.$R}`), it's a do-nothing statement (actually a comment). In order for it to actually be a compiler directive, you need to remove the `.` to leave `{$R 'GeoMonitor.res' 'GeoMonitor.rc'}`. That would explain why it didn't work - the manifest was never included. – Ken White Jun 23 '15 at 12:54

2 Answers2

7

I think you have two problems:

  1. You are failing to compile the .rc resource script file to a compiled resource with .res extension, and linking that. You should not link the .rc file, or the .xml file. You need to link the compiled .res file. Compile the resource script with either brcc32 or rc.
  2. I can tell that you are not linking the .res file because your manifest is invalid. If you had compiled and linked that, the loader would reject your executable due to an invalid manifest.

Here's the minimum manifest that you need:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
            <requestedPrivileges>
                <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
            </requestedPrivileges>
        </security>
    </trustInfo>
</assembly>

Clearly you'll want to add more, but start here, with a manifest file that is known to be good. Prove that it works as you expect. Then add more functionality.

One final point to make is that I suspect that you have been in the habit of disabling UAC. That is a really bad idea, even more so for a developer. Had you spent the past 10 years with UAC enabled then you would have come across all the UAC/standard user issues a long time ago.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • not arguing, just memory-failure & curious: Didn't Delphi used to include a cut-down version of InstallShield or similar? – MartynA Jun 22 '15 at 20:49
  • @MartynA I don't see how that relates to the question or this answer. But I do recall some cut down IS in some ancient Delphi. – David Heffernan Jun 22 '15 at 20:50
  • All I meant was, that on-going inclusion of a professional installer and its docs might have encouraged Delphi devs to get to grips with UAC, etc, rather than leave them to trip over it or try and fudge their way around it. – MartynA Jun 22 '15 at 20:58
  • 2
    +1 for the comment about UAC, developers, please stop making software that requires administrator privileges. In 9 out of 10 times it really isn't needed and is just laziness. You're making windows less secure. – Pieter B Jun 23 '15 at 06:42
  • @DavidHeffernan My RC file is getting compiled and I do see RES file in my project folder after I compile my project. If I remove RC file from my project, `{.$R 'GeoMonitor.res' 'GeoMonitor.rc'}` disappears from my DPR file. Is that okay? I just learned from my IT guy that he always turns OFF UAC when he setups computers because he hates it. That explains why I never had to deal with this.... – ThN Jun 23 '15 at 13:05
  • You need to link the .res file. `{$R manifest.res}` or whatever it is called. If you link the xml in the question, the exe won't even start. Because that manifest xml is not valid. – David Heffernan Jun 23 '15 at 13:09
  • Or if you are having the compiler compile the res file `{$R manifest.res manifest.rc}` – David Heffernan Jun 23 '15 at 14:41
  • @DavidHeffernan It seems my compiler compiles RC file using brcc32.exe as long as you have one in the project folder by default. I made the changes according to your answer and compiled. It did produce RES file. I did verify what's in it is what I have in my manifest file in binary by opening it in Microsoft Visual Studio. Plus, in my profile (DPR) file, RES file is linked as follows {$R *.res}. Then, I took my program's exe file, drop it on Windows 10 system and double clicked on the exe file. Still acting the same as it did before. Only with `Run as an administrator` option it runs no error – ThN Jun 23 '15 at 15:03
  • All works fine here. I don't know what you did wrong. You enabled UAC though I presume? Why don't you start with a plain vanilla project, and see if you can make the UAC dialog appear when launching that? – David Heffernan Jun 23 '15 at 15:19
  • I finally got it to work without the error message as admin except at startup. At boot it kept starting as background process. So, I had to cheat and wrote a mini program, which is placed in the Startup folder and starts my program as admin at boot... – ThN Jun 23 '15 at 19:37
  • I don't know why this question is marked down. David's answer did help me solve my access denied popup window. Now, I can start my program without issues. It is only at boot my program runs as background process. It could be because something else not necessarily related this question. – ThN Jun 23 '15 at 20:52
-1
function CreateRunAsAdmin(const AppNamePath: string): boolean; //AppNamePath=CreateRunAsAdmin(Application.ExeName);
var
   RegisterTemp: TRegistry;
   openResult: Boolean;
begin
   RegisterTemp := TRegistry.Create;
   with RegisterTemp do
   begin
      RootKey := HKEY_CURRENT_USER;
      //openResult:=OpenKey('\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers', True);
      OpenKey('\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers', True);
      WriteString(AppNamePath, 'RUNASADMIN');
      Result := True;
      Free;
   end;
end;
DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44