【发布时间】: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;
}
【问题讨论】:
-
command1在system("runas /user:the_annoying_ command1")调用中使用不会扩展到您的command1变量的内容。 -
只需在
command的设置前加上"runas /user:the_annoying_ "并致电system(command.c_str()); -
Run-as 你自己不会有任何效果。您需要以管理员身份运行。另请参阅this。