【问题标题】:how start process with admin rights in hide mode如何在隐藏模式下以管理员权限启动进程
【发布时间】:2012-11-30 02:37:53
【问题描述】:

我正在制作应该以管理员权限启动进程并且应该隐藏的程序 我试过了,但它不起作用

 ProcessStartInfo startInfo = new ProcessStartInfo(Application.StartupPath + "\\launcher.exe");
           startInfo.WindowStyle = ProcessWindowStyle.Hidden;

           startInfo.CreateNoWindow = true;  
           Process.Start(startInfo);

【问题讨论】:

  • 有什么用? “管理员权限”和“隐藏”引发了一些警钟。
  • 此外,绕过 UAC 也很困难。不是你应该的。

标签: c#


【解决方案1】:

隐藏窗口? 那么你应该试试这个:

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

private const int c_SwHide = 0;
private const int c_SwShow = 5;

通过使用方法:

static void Main()
{
    ShowWindow(GetConsoleWindow(), c_SwHide);
}

希望这会有所帮助。 :) 如果要再次显示,只需将第二个参数“c_SwHide”替换为“c_SwShow”即可。

【讨论】:

  • return new WindowsPrincipal( WindowsIdentity.GetCurrent() ).IsInRole( WindowsBuiltInRole.Administrator ); - 当你拥有管理员权限时,这应该返回 true。否则它会返回 false。
【解决方案2】:
SecureString pass = new SecureString ();
foreach (char c in "yourpassword".ToCharArray())
{
    pass.AppendChar(c);
}    
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Application.StartupPath + "\\cmd.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.Domain = "yourdomain"
startInfo.UserName = "yourusername"
startInfo.Password = pass;
Process.Start(Info);

根据需要填写即可。

要提升,您必须提示输入 UAC,为此您可以使用

startInfo.Verb = "runas";

或创建一个清单文件,右键单击您的项目 -> 添加项目 -> 清单文件

找到上面写着的那一行

<requestedExecutionLevel level="asInvoker" uiAccess="false"/>

并将其更改为

<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>

【讨论】:

  • 感谢您的快速回复,但我收到此错误请求的操作需要提升
  • 在这种情况下,您需要创建一个清单文件来提示 UAC 提升。我将编辑我的答案。
  • 现在它不再确认管理员权限,但是当我启动它向我希望隐藏的用户显示的过程时它仍然没有隐藏
  • UAC 或需要管理员帐户的问题 将程序从屏幕中隐藏并在后台运行的问题 它与除了 launcher.exe 之外的所有程序一起工作 我不知道为什么我认为它是因为它需要管理员权限
  • 解决方案不准确。您不能将 runas 与用户名和密码结合使用。要使用 Verb="runas",您必须将 UseShellExecute 设置为 true,而要使用用户名和密码,您必须将 UseShellExecute 设置为 false。
猜你喜欢
  • 1970-01-01
  • 2016-09-29
  • 2011-06-06
  • 2011-05-05
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多