Adding manifest for admin rights request

前端 未结 3 572
孤独总比滥情好
孤独总比滥情好 2020-12-13 17:02

I have an application that writes program settings to the windows registry that is absolutely necessary so.storing to a file is not an option.

To simplify this, I wo

相关标签:
3条回答
  • 2020-12-13 17:26

    In newer versions of Delphi this is no longer necessary.
    Under Project Options, then Application, there is a section for the manifest:


    Screenshot from version 10.2.1 Tokyo

    This automatically takes care of the XP manifest mentioned as a warning at the botom of Kobik's answer.

    I do not know from which Delphi version this was available.
    (Free free to edit this answer if you know). (Edit Ian Murphy Feb 2019: It is not available in XE2)

    0 讨论(0)
  • 2020-12-13 17:32

    I already commented that "Writing program settings to the registry is not a proper cause to give your application admin privileges". However it is a good idea to include a UAC manifest in any case. the common requestedExecutionLevel should be level="asInvoker". see the docs

    "Q: Adding manifest for admin rights request for delphi 7"
    

    Create the below 4 files (2 sets):


    (UAC Only)

    uac.manifest

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
      <assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="MyApp" type="win32"/>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
          <requestedPrivileges>
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
          </requestedPrivileges>
        </security>
      </trustInfo>
    </assembly>
    

    uac.rc

    1 24 "uac.manifest"
    

    (UAC + XP Themes)

    uac_xp.manifest

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
      <assemblyIdentity type="win32" name="MyApp" 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>
    

    uac_xp.rc

    1 24 "uac_xp.manifest"
    

    Add the desired rc file (uac.rc or uac_xp.rc) to your project via the "Project > Add to project" menu item. This will create the {$R} directive in your project file:

    program Project1;
    
    {.$R 'uac.res' 'uac.rc'} // UAC only
    // OR
    {$R 'uac_xp.res' 'uac_xp.rc'} // UAC + XP Themes
    
    uses
      Forms,
      Unit1 in 'Unit1.pas' {Form1};
    
    {$R *.RES}
    
    begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
    

    Note the {$R 'uac_xp.res' 'uac_xp.rc'}. Delphi will auto compile the rc to res file.

    Alternatively you can compile the rc file via brcc32 uac.rc outside Delphi IDE. and then add {$R 'uac_xp.res'} manually to your project.


    Make sure you don't use any other XP manifest.

    0 讨论(0)
  • 2020-12-13 17:35

    How to remove the "internal" manifest from Delphi 7 project ?

    That internal manifest was in Delphi 7 times represented by the TXPManifest component, whose only purpose was to add the XPMan.pas unit to the uses clause of a form's unit where it was dropped. This XPMan.pas unit then included the resource file with the manifest itself into a project output.

    So, to remove this internal manifest, you need to remove all TXPManifest components from all of the forms of your project (if you used them), as well as all the XPMan uses clause references from all units of your project.

    In short, do the following two steps for all units of your project (the first step is optional if you didn't used the TXPManifest components, but the second):

    enter image description here

    0 讨论(0)
提交回复
热议问题