17

I need to create an application which needs to create files/folders in "C:\Program Files","Users[username]" and Sys32. Also the application needs to make some registry entry.

This application needs to work on Vista and higher. Also, on Windows Server 2003 and higher.

The above Operating Systems have the concept of User Account Control (UAC), where to access Program Files and writing in registry requires admin privileges.

I looked into many forums and found that using Microsoft SDK we can check whether the current user have admin privileges or not . But the function "CheckTokenMembership" fails for Vista and higher version of OS.

I also found a solution where manifest file can be used to tell OS in advance that the current application requires admin privileges. This is done using "requestedExecutionLevel" tag.

I am using Visual Studio 2005 to create the application. When we create an application in Visual Studio a default manifest file is created. Can I change this manifest file to include "requestedExecutionLevel" tag, so that my application always runs with admin privileges?

Is there any other method through which my application runs with admin privileges without asking user (admin or standard) to run my application as "run as admin"??

Thanks!

Lipika
  • 315
  • 2
  • 5
  • 13
  • 2
    This may help to you http://stackoverflow.com/questions/8139480/c-console-application-prompt-run-as-admin-to-run-as-administrator and this http://msdn.microsoft.com/en-us/library/bb756929.aspx – Nayana Adassuriya Apr 12 '13 at 09:44
  • 1
    Not sure what your question is / was. But you need to elevate your token to admin under UAC. If you took the time to read through the Microsoft SDK and sample code you would have a perfect example of how to do this. – Dan Feb 11 '14 at 21:53

1 Answers1

37

You should find an option for this in project properties Linker -> Manifest File -> UAC Execution Level. Set this to requireAdminstrator.

This will cause the default generated manifest to include the requestedExecutionlevel that you need, so that your users will be prompted automatically to elevate their privileges if they are not already elevated.

screenshot of visual studio UAC options

snowcrash09
  • 4,392
  • 23
  • 42