如何运行需要管理员权限的应用程序

时间:2023-04-14
本文介绍了如何运行需要管理员权限的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经坚持了几个小时,直到我终于设法做到了.已经有链接为我指明了正确的方向:

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++/Win32:如何等待挂起的删除完成

相关文章