C++ 如何运行需要管理员权限的应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11586139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run application which requires admin rights from one that doesn't have them
提问by Samuel
I've been stuck on this for a few hours until I've finally managed to do it. There are already links which pointed me the right direction:
我已经坚持了几个小时,直到我终于设法做到了。已经有链接为我指明了正确的方向:
- Is it possible for the executable to ask for Administrator rights? (Windows 7)
- CreateProcess error=740, The requested operation requires elevation
But I've thought that simple overview of the problem could help someone :).
但我认为对问题的简单概述可以帮助某人:)。
回答by Samuel
Real problem: (from Wikipedia: http://en.wikipedia.org/wiki/User_Account_Control)
真正的问题:(来自维基百科:http: //en.wikipedia.org/wiki/User_Account_Control)
An executable that is marked as "requireAdministrator" in its manifest cannot be started from a non-elevated process using CreateProcess(). Instead, ERROR_ELEVATION_REQUIRED will be returned. ShellExecute() or ShellExecuteEx() must be used instead.
无法使用 CreateProcess() 从非提升进程启动在其清单中标记为“requireAdministrator”的可执行文件。相反,将返回 ERROR_ELEVATION_REQUIRED。必须改用 ShellExecute() 或 ShellExecuteEx()。
(BTW, ERROR_ELEVATION_REQUIRED error == 740)
(顺便说一句,ERROR_ELEVATION_REQUIRED 错误 == 740)
Solution: (same site)
解决方案:(同一站点)
In a native Win32 application the same "runas" verb can be added to a ShellExecute() or ShellExecuteEx() call.
在本机 Win32 应用程序中,可以将相同的“runas”动词添加到 ShellExecute() 或 ShellExecuteEx() 调用中。
ShellExecute(hwnd, "runas", "C:\\Windows\\Notepad.exe", 0, 0, SW_SHOWNORMAL);
ShellExecute(hwnd, "runas", "C:\\Windows\\Notepad.exe", 0, 0, SW_SHOWNORMAL);
This may be also helpful: (source: http://mark.koli.ch/2009/12/uac-prompt-from-java-createprocess-error740-the-requested-operation-requires-elevation.html)
这也可能有帮助:(来源:http: //mark.koli.ch/2009/12/uac-prompt-from-java-createprocess-error740-the-requested-operation-requires-elevation.html)
2 - Basic UAC Flow
Ok, so before you dig into it, I thought it might be helpful to explain the basic flow of a UAC aware application and how everything fits together. Normally, your application runs as an unprivileged user. But, sometimes it needs to be an Administrator (to do whatever). So, here's the basic idea, in pseudo code:
2 - 基本的 UAC 流程
好的,所以在您深入研究之前,我认为解释 UAC 感知应用程序的基本流程以及所有内容如何组合在一起可能会有所帮助。通常,您的应用程序以非特权用户身份运行。但是,有时它需要是管理员(做任何事情)。所以,这是伪代码中的基本思想:
int main (int argc, char **argv) {
HRESULT operation = tryToDoSomethingPrivileged();
if (operation == ACCESS_DENIED && !alreadyElevated) {
// Spawn a copy of ourselves, via ShellExecuteEx().
// The "runas" verb is important because that's what
// internally triggers Windows to open up a UAC prompt.
HANDLE child = ShellExecuteEx(argc, argv, "runas");
if (child) {
// User accepted UAC prompt (gave permission).
// The unprivileged parent should wait for
// the privileged child to finish.
WaitForSingleObject(child, INFINITE);
CloseHandle(pid);
}
else {
// User rejected UAC prompt.
return FAILURE;
}
return SUCCESS;
}
return SUCCESS;
}
Finally, this is how I've done it:
最后,这就是我的做法:
if(0 == CreateProcess(argv[2], params, NULL, NULL, false, 0, NULL, NULL, &si, &pi)) {
//runas word is a hack to require UAC elevation
ShellExecute(NULL, "runas", argv[2], params, NULL, SW_SHOWNORMAL);
}
And just for completness's sake - MSDN links to ShellExecute and CreateProcess:
为了完整起见 - MSDN 链接到 ShellExecute 和 CreateProcess:
http://msdn.microsoft.com/en-us/library/bb762153%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/bb762153%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx