UAC need for console application

Created : 7/11/2022

Question

I have a console application that require to use some code that need administrator level. I have read that I need to add a Manifest file myprogram.exe.manifest that look like that :

<?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">
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>

But it still doesn't raise the UAC (in the console or in debugging in VS). How can I solve this issue?

Update

I am able to make it work if I run the solution in Administrator or when I run the /bin/*.exe in Administrator. I am still wondering if it's possible to have something that will pop when the application start instead of explicitly right click>Run as Administrator?

Questioned by : Patrick Desjardins

Answer

You need to embed the UAC manifest as an embedded Win32 resource. See Adding a UAC Manifest to Managed Code.

In short, you use a Windows SDK command line tool to embed it into your executable.

You can automate this as a post-build step by placing the following line as a post build task in your VS project's properties:

mt.exe -manifest "$(ProjectDir)$(TargetName).exe.manifest" -updateresource:"$(TargetDir)$(TargetName).exe;#1"
Answered by : Judah Gabriel Himango