Well I noticed that on Windows 7, sometimes even when you are an administrator, you can’t do a lot of things, probably it’s some sort of bug, my application I check if an user is administrator before start the program because my program creates file in folders that are protected default like the root folder ( C: ), and if you aren’t an administrator on Windows 7, you can only create folders there.
So if I right click in my application and go “Run as Administrator”, it just works fine.
Is there a way to make my application run as administrator automatically? I would like to be able to make a line of code like: ActivateAdministrator(); and be available for the code completely, because I change attributes, create files with ifstream.
You could add a manifest to your executable – http://msdn.microsoft.com/en-us/library/bb756929.aspx
If the user is running on a system with the UAC switched on, and are not an administrator, a manifest which contains
requestedExecutionLevelwill produce a prompt for the Administrator password before your application can run with administrative privileges. (requiring administrator privileges means that an incorrect password or no password will stop it from running altogether)level="requireAdministrator"
If they are an administrator with the UAC switched on, then that same manifest will cause a Yes/No prompt to ask whether your application should be granted administrative privileges.
Of course, the real issue is that whatever your application is doing which requires administrative privileges needs to be examined.
Most of the time the privilege is simply not required for normal user-level applications. This is an application design issue really – what is your application doing which requires admin privileges? is it really necessary? e.g. If you’re modifying files, then why are those files in a protected area on the file system instead of in the user’s profile space?