Activating Administrator via C++ when users are already administrator [Run as administrator]

馋奶兔 提交于 2019-12-13 08:05:48

问题


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.


回答1:


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 requestedExecutionLevel level="requireAdministrator" will 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)

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?




回答2:


You might find the Windows Dev Center article on Priviliges helpful, specifically Enabling and Disabling Privileges in C++ .




回答3:


Although this is in C#, it might be easier for you I don't know. What I did was to Detect if running as Administrator with or without elevated privileges?, and if not rerun the current process while requesting administrative access (which if the UAC is enabled, would do a popup to the current user and ask if it is ok for the program to run with administrative privleges).

Then some simple (but C# code) looks like:

// UAC is a class from the previous link on SO
if (UAC.IsCurrentProcessElevated())
{
  string currentProcess = Assembly.GetEntryAssembly().Location;
  string arguments = string.Join(" ", this._Args.ToArray());
  ProcessStartInfo startInfo = new ProcessStartInfo(currentProcess, arguments); 
  startInfo.UseShellExecute = true;
  startInfo.Verb = "runas";
  Process.Start(startInfo);
}

The un-elevated process would quite, with a new one started that requested administrative privileges.



来源:https://stackoverflow.com/questions/10254969/activating-administrator-via-c-when-users-are-already-administrator-run-as-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!