如何使用 c++ 在 cmd 中使用管理员权限执行命令

How to excetute commands in cmd with adminstritive privelges using c++?

本文关键字:管理员 权限 执行 命令 何使用 c++ cmd      更新时间:2023-10-16

我正在开发一个热点制作器应用程序,我正在使用c ++在cmd中执行netsh命令,但为此我需要cmd的管理权限。但这给出了一个未知的错误。

我的管理员用户:the_annoying_

电脑号码: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;
}
我认为

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

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

sudo ./test =>根

./test =>用户名

在Windows上应该相同。