0

I want to make my program ask for admin rights when it starts. I already found out that you can do this by doing this :

Properties -> Linker -> Manifest File -> UAC Execution Level -> requireAdministrator (/level='requireAdministrator')

So here is my actual question:

Couldn't I change this setting with my code? Because I can do this for example:

#pragma comment (lib, winmm.lib);

and adding a lib is a linker setting too.

http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vcprojectengine.vclinkertool.uacexecutionlevel.aspx

I found this but it doesn't seem to work for me, or is there something I may forget by using this code, beacause the compiler tells me that property is undefined?

Hans Peter
  • 571
  • 1
  • 8
  • 14
  • 1
    Your link is for .NET. It won't work in plain C++. – chris Dec 12 '13 at 20:50
  • possible duplicate of [How can I ask the user for elevated permissions at runtime?](http://stackoverflow.com/questions/6108851/how-can-i-ask-the-user-for-elevated-permissions-at-runtime) – bcrist Dec 12 '13 at 20:53
  • but i don't want to write my own manifest file! – Hans Peter Dec 12 '13 at 20:55
  • As stated in the accepted answer on the other question, launching a process called `*_setup.exe` or `*_install.exe` through the shell triggers UAC. – bcrist Dec 12 '13 at 20:57
  • Why don't you do it the way that works? – David Heffernan Dec 12 '13 at 21:04
  • you mean with the properties? – Hans Peter Dec 12 '13 at 21:05
  • @bcrist, I think it's more like `*(setup|install)*.exe` – chris Dec 12 '13 at 21:05
  • @chris yes, I know, technically I think it's `*(setup|install|update)*.exe` – bcrist Dec 12 '13 at 21:23
  • @bcrist, Just saw that appended comment :p – chris Dec 12 '13 at 21:25
  • @chris it also appears the program needs to be linked using `/SUBSYSTEM:WINDOWS`. With `/SUBSYSTEM:CONSOLE` UAC is not triggered automatically, but `*(setup|install)*.exe` triggers a "Did this program install correctly" on exit if a non-zero status code is returned. `*(update).exe` does not exhibit this behavior. – bcrist Dec 12 '13 at 21:33
  • @bcrist, Interesting, although my test of "setup.exe" that was linked with a console subsystem still brought up the dialog. Considering all it did was print something, it did bring up the troubleshooting dialog after, though. – chris Dec 12 '13 at 21:44
  • @chris Hmm. The console program I used was an OpenGL program using the console for debugging. I assumed it was the `/SUBSYSTEM:CONSOLE` that suppressed it but maybe there's something else going on. – bcrist Dec 12 '13 at 21:52
  • https://social.msdn.microsoft.com/Forums/vstudio/en-US/95677c5a-b331-4b1d-96e7-14fdbea4d65f/how-to-use-vs2010-pragma-manifestuac-on-console-without-ide?forum=vcgeneral you can add manifest using mt.exe after compiling – se_pavel May 29 '15 at 11:27

1 Answers1

2

No you can't do that from code. The #pragma comment directive can pass some information to the linker but it only supports a limited subset of linker commands:

Only the following (comment-type) linker options are available to be passed to the linker identifier:

/DEFAULTLIB

/EXPORT

/INCLUDE

/MANIFESTDEPENDENCY

/MERGE

/SECTION

shf301
  • 31,086
  • 2
  • 52
  • 86