-2

I am working on an hotspot maker application for which i am using c++ to execute netsh commands in cmd, but for that i require administrative privileges of cmd.I have used system(runas \user:) but that is giving an unknown error.

My admin user:the_annoying_

PC NAme: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;
}
πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
  • `command1` used in the `system("runas /user:the_annoying_ command1")` call won't expand to the contents of your `command1` variable. – πάντα ῥεῖ Apr 20 '19 at 09:44
  • Just prefix your setup for `command` with `"runas /user:the_annoying_ "` and call `system(command.c_str());` – πάντα ῥεῖ Apr 20 '19 at 09:54
  • Run-as yourself will have no effect. You need to run-as administrator. Also see [this](https://stackoverflow.com/questions/6418791/requesting-administrator-privileges-at-run-time). – rustyx Apr 20 '19 at 09:56

1 Answers1

-1

The easiest solution I think would be to just run your compiled Application as an Administrator this way alls subprocesses will be executed with Admin privileges as well (so the process of system()).

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

sudo ./test => root

./test => username

Should be the same on Windows.