【问题标题】:How to excetute commands in cmd with adminstritive privelges using c++?如何使用 C++ 在具有管理权限的 cmd 中执行命令?
【发布时间】:2023-03-23 10:20:02
【问题描述】:

我正在开发一个 hotspot maker 应用程序,为此我使用 c++ 在 cmd 中执行 netsh 命令,但为此我需要 cmd 的管理权限。我使用过 system(runas \user :) 但这是一个未知错误。

我的管理员用户:the_annoying_

PC 名称:Unknown_God

using namespace std;

int main ()

{

    string name;

    cout << "Enter Name of Wifi Hotspot:" << endl;

    cin >> name;

    string pass1="0",pass2="1";

    while(pass1!=pass2)

    {
        cout << "Enter the password" << endl;
        cin >> pass1;
        cout << "Re-enter the password" << endl;
        cin >> pass2;
        if(pass1!=pass2)
        {
            cout << "Please enter same passwords" << endl;
        }
    }
    cout << "Working..." << endl;
    string command="netsh wlan set hostednetwork mode=allow ssid=" + name + "key=" + pass1;
    const char *command1=command.c_str();
    cout << "Creating Wifi Hotspot using given Credentials" << endl;
    system("runas /user:the_annoying_ command1");
    string comm="netsh wlan start hostednetwork";
    const char *command2=comm.c_str();
    system("runas /user:the_annoying_ command2");
    cout << "Hotspot Sucessfully Created" << endl;
}

【问题讨论】:

  • command1system("runas /user:the_annoying_ command1") 调用中使用不会扩展到您的command1 变量的内容。
  • 只需在command 的设置前加上"runas /user:the_annoying_ " 并致电system(command.c_str());
  • Run-as 你自己不会有任何效果。您需要以管理员身份运行。另请参阅this

标签: c++ cmd system netsh


【解决方案1】:

我认为最简单的解决方案是以管理员身份运行已编译的应用程序,这样所有子进程也将以管理员权限执行(因此 system() 的进程)。

#include <iostream>
{
  system("whoami");
  return 0;
}

sudo ./test => 根

./test => 用户名

在 Windows 上应该是一样的。

【讨论】:

猜你喜欢
  • 2017-09-24
  • 2016-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-20
  • 2019-07-14
  • 2011-11-29
相关资源
最近更新 更多