0

I want my program to simply request elevation privileges when it launches, and to do this I'm adding a Manifest file to the Delphi source code as a resource. This is the XML files content:

<?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="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>

I need to modify the file where it says "YourApplicationExeName" and add the current .exe name as the user can rename the .exe file anytime.

What I want to know is: when a user opens the executable file, how do I add the new filename of the executable to the manifest file stored as a resource? I know how to get the new filename, I just want to know how do I update the resource file with the new filename?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ddvniek
  • 27
  • 1
  • 1
  • 9
  • Not a duplicate. My question is how do I modify the XML file - add the current application name - and add it as a resource file to be used again? – ddvniek Jul 06 '14 at 09:15
  • You modify a file in the same way as you modify any file. It is a basic pre-requisite of programming that you know how to edit a text file. If you don't know how to edit a text file, you need to go back to the beginning. If you want to know how to get from where you are now to where you want to be then you would need to explain in detail what you have currently done, i.e. where you are now. We cannot read your mind. Please edit the question and put a little more effort into explaining. – David Heffernan Jul 06 '14 at 09:16
  • All you have to do is start from any tutorial on how to compile and add a manifest resource. They are all the same. You can find serviceable ones at that question. 1. Make the XML file. 2. Make the .rc file. 3. Compile the .rc file to a .res file. 4. Link the .res file. Steps 3/4 can be combined into one if you wish with the advanced `$R` option. We simply do not need yet another "how do I compile and link a resource" question here. There are literally thousands already. – David Heffernan Jul 06 '14 at 09:21
  • Hi David. I know how to edit a text file and I know how to compile a .rc file and add it to the project source. Not the problem. The problem I have is updating the resource file - Editing the XML file with new values and then adding itself as a resource again. What I want is when a user opens the executable file, it must determine the new filename(not a problem), and add the new filename to the manifest file(stored as a resource) – ddvniek Jul 06 '14 at 11:02
  • What do you mean? Open the executable file? You mean create a new process and execute it? Please explain precisely what you mean in the question. Click the edit button and make it clear. – David Heffernan Jul 06 '14 at 11:05
  • Edited. I hope this clears up what I'm trying to ask. All I want is when the .exe file is opened, it must determine its own filename, and add it to the manifest(XML file) which is stored in the project as a resource. – ddvniek Jul 06 '14 at 11:12

2 Answers2

1

What you are asking for is not possible. A process is started from an executable file. That file is locked while the process is executing and cannot be modified.

If you wish the modify a resource in a compiled executable file then you use the UpdateResource API. But the file must not be locked in order to do so. You'd need to do this from a different process.

I think you are over-analysing this anyway. The field you wish to modify is not expected to hold the file name of the executable. After all, the file name can be changed. This field has the name of the application, an value independent from the file name of the executable.

The documentation describes this field like this:

Uniquely names the application or assembly. Use the following format for the name: Organization.Division.Name. For example Microsoft.Windows.mysampleApp.

In summary, I think you have been misled by whoever wrote YourApplicationExeName. You simply should not attempt to modify this resource after the program has been compiled. You should set the field correctly when compiling your app, and that's it.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thank you for the answer. So in other words, if I wanted to update the resource of the executable, another executable would have to do it as the first executable can not be running. Also, the field "YourApplicationExeName" does not need the filename of the executable, but the name of the application? – ddvniek Jul 06 '14 at 11:25
  • Correct on both counts. You absolutely don't want to modify the executable file like this. No program I have ever encountered does so. – David Heffernan Jul 06 '14 at 11:29
0

Tell Delphi not to include as assembly manifest in your executable, and instead have an external manifest.

For example:

Contoso.exe
Contoso.exe.manifest

A .manifest file is simply an XML file. When the user renames Contoso.exe to Frobber.exe, you can either:

  • tell them to also rename Contoso.exe.manifest to Frobber.exe.manifest
  • vomit out a Frobber.exe.manifest file onto the hard drive for them before you launch Frobber.exe
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219