How to run application which requires admin rights from one that doesn#39;t have them(如何运行需要管理员权限的应用程序)
问题描述
我已经坚持了几个小时,直到我终于设法做到了.已经有链接为我指明了正确的方向:
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:
- 有可能吗对于可执行文件要求管理员权限?(Windows 7)
- CreateProcess error=740,请求的操作需要提升
但我认为对问题的简单概述可以帮助某人:)
But I've thought that simple overview of the problem could help someone :).
推荐答案
真正的问题:(来自维基百科:http://en.wikipedia.org/wiki/User_Account_Control)
Real problem: (from Wikipedia: http://en.wikipedia.org/wiki/User_Account_Control)
标记为requireAdministrator"的可执行文件;在其清单中不能使用 CreateProcess() 从非提升进程启动.相反,将返回 ERROR_ELEVATION_REQUIRED.必须改用 ShellExecute() 或 ShellExecuteEx().
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.
(顺便说一句,ERROR_ELEVATION_REQUIRED 错误 == 740)
(BTW, ERROR_ELEVATION_REQUIRED error == 740)
解决方案:(同一站点)
Solution: (same site)
在本机 Win32 应用程序中,相同的runas"动词可以添加到 ShellExecute() 或 ShellExecuteEx() 调用中.
In a native Win32 application the same "runas" verb can be added to a ShellExecute() or ShellExecuteEx() call.
ShellExecute(hwnd, "runas", "C:\Windows\Notepad.exe", 0, 0, SW_SHOWNORMAL);
这可能也有帮助:(来源:http://mark.koli.ch/2009/12/uac-prompt-from-java-createprocess-error740-the-requested-operation-requires-elevation.html一>)
This may be also helpful: (source: http://mark.koli.ch/2009/12/uac-prompt-from-java-createprocess-error740-the-requested-operation-requires-elevation.html)
2 - 基本的 UAC 流程
2 - Basic UAC Flow
好的,所以在您深入研究之前,我认为解释 UAC 感知应用程序的基本流程以及所有内容如何组合在一起可能会有所帮助.通常,您的应用程序以非特权用户身份运行.但是,有时它需要是管理员(做任何事情).所以,这是伪代码中的基本思想:
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:
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;
}
最后,我是这样做的:
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);
}
为了完整起见 - MSDN 链接到 ShellExecute 和 CreateProcess:
And just for completness's sake - MSDN links to ShellExecute and CreateProcess:
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
这篇关于如何运行需要管理员权限的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何运行需要管理员权限的应用程序
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- CString 到 char* 2021-01-01
