如何运行需要管理员权限的应用程序从没有他们的 [英] How to run application which requires admin rights from one that doesn't have them

查看:2254
本文介绍了如何运行需要管理员权限的应用程序从没有他们的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直呆在这里几个小时,直到我终于设法做到了。已有指向我正确方向的链接:

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

推荐答案

真正的问题:(来自维基百科: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。

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.

(BTW,ERROR_ELEVATION_REQUIRED错误== 740)

(BTW, ERROR_ELEVATION_REQUIRED error == 740)

解决方案:(同一网站)

Solution: (same site)


在本机Win32应用程序中,添加到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


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;

}

最后,这是我做的: / p>

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);
}

为了完整起见,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

这篇关于如何运行需要管理员权限的应用程序从没有他们的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆