-2

I want to make a program that runs a command in cmd, but as an administrator, But i'm having hard time understanding how to do it.

I've tried changing my program startup properties to start as an administrator but it still didn't work, when i called system() it wasn't administrator.

I tried this this solution which I saw here

system("runas /user:<admin-user> \"program.exe\"");

But i dont undertand how am i suppose to run commands with it like:

system("runas /user:<admin-user> \" COMMAND HERE? \"");

when I tried running the solution in cmd i got error saying the file specific as not found

In addition i am trying to do this project avoiding CreateProcess. Thank you

drescherjm
  • 8,907
  • 5
  • 42
  • 60
yarin Cohen
  • 612
  • 5
  • 16
  • what have you tried on `\" COMMAND HERE? \"` ? – Coursal Jan 12 '19 at 18:42
  • @Coursal this was an example of what i dont understand, where to put the command – yarin Cohen Jan 12 '19 at 18:42
  • Put the command in place the ` COMMAND HERE? ` part of the string. – KamilCuk Jan 12 '19 at 18:44
  • do you mean at what line in the source code? do you mean what to put instead of `\" COMMAND HERE? \"`? – Coursal Jan 12 '19 at 18:44
  • @Coursal I mean how to use it to run commands, look at my first code example – yarin Cohen Jan 12 '19 at 18:45
  • What commands are you wanting to run? Any command you type in `cmd.exe` can be passed to `system()` as it just runs `cmd.exe` with the command. But why are you using `system()` to run commands? Chances are, there is usually a better direct API for most commands – Remy Lebeau Jan 12 '19 at 18:47
  • @KamilCuk when i do it i get an error "The system cannot find the file specified." – yarin Cohen Jan 12 '19 at 18:48
  • What you do what? When you do `system("runas /user: \"program.exe\"");` ? What if you open a comand line and put the exact same command in your command line? Try `printf("runas /user: \"program.exe\"");` and copy the exact output to the command line window and try it. – KamilCuk Jan 12 '19 at 18:49
  • That means the executable you are trying to run is not a valid file name or the folder for it is not in the PATH environment variable. Also I believe run-as will inherit the environment (and drives mapped) as the user you run-as not the current user's environment. One way to check this is to run a cmd.exe window as the user and see if the command works in the cmd.exe window. – drescherjm Jan 12 '19 at 18:50
  • 1
    C++ has no notion of an admin user. – Ron Jan 12 '19 at 19:36

2 Answers2

3

You'll have to make your own system. Something like this may suffice:

BOOL RunAsAdmin( LPCTSTR lpFile, LPCTSTR lpParameters, HWND hWnd ) {
    BOOL                retval;
    SHELLEXECUTEINFO    sei;
    ZeroMemory ( &sei, sizeof(sei) );

    sei.cbSize          = sizeof(SHELLEXECUTEINFO);
    sei.hwnd            = hWnd;
    sei.fMask           = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI;
    sei.lpVerb          = TEXT("runas");
    sei.lpFile          = lpFile;
    sei.lpParameters    = lpParameters;
    sei.nShow           = SW_SHOWNORMAL;
    retval = ShellExecuteEx( &sei );
    // or try as the normal user ... remove if that's not an option
    if( !retval ) {
        sei.lpVerb = TEXT("open");
        retval = ShellExecuteEx( &sei );
    }

    return retval;
}
Anders
  • 83,372
  • 11
  • 96
  • 148
Ted Lyngmo
  • 37,764
  • 5
  • 23
  • 50
  • Why are you mixing LPTSTR with plain strings? Use `TEXT()` on those strings or force Unicode. – Anders Jan 12 '19 at 19:36
  • @Anders Fair point. I think i got them all. – Ted Lyngmo Jan 12 '19 at 20:08
  • No, you messed up again. LPCTSTR+TEXT or LPCWSTR+L+ShellExecuteExW – Anders Jan 12 '19 at 20:13
  • @Anders In context, what I had would work, but I agree on the sloppyness. New try. I left the `W` out on the `ShellExecuteEx` on pupose. If that fails - problems. – Ted Lyngmo Jan 12 '19 at 20:19
  • You can make it work everywhere regardless of the UNICODE setting. Failing to compile is a distant 2nd I guess. – Anders Jan 12 '19 at 20:25
  • @Anders I don't know if what it'd take to make it unicode proof is required to answer questions - but isn't this an answer to OP:s question as it stands? – Ted Lyngmo Jan 12 '19 at 20:30
  • Yes, anyone with a little bit of win32 experience can make this code work regardless of their UNICODE setting. But SO answers should aim for perfection (IMHO) which is why I made a comment. – Anders Jan 13 '19 at 01:12
  • @Anders You are welcome to make any edits you'd like to my answer or to provide an answer yourself. Please do, because I'm doubting myself at this point. – Ted Lyngmo Jan 13 '19 at 01:16
  • There is nothing to doubt. Pasting this into a Unicode project will work without changes. Ansi projects will require minor modifications. Your initial answer was so close but you commented out the `_TEXT` macro for some reason. – Anders Jan 13 '19 at 02:23
  • Thank you very much. That's literally the only answer I could find online on this matter. How ShellExecuteEx manages to execute the passed cmd as admin? The params here are quite not self evident. – golosovsky Jul 30 '20 at 20:36
0

You probably should be using CreateProcessAsUser, but based on your wish to avoid CreateProcess, you probably don't want to use that either.

To do it with system, you want to create a string that contains the complete command to execute, then pass that to system, something on this general order:

void run_program(std::string const &admin_name, std::string const &cmd) { 
    std::strings buffer = "runas /user:" + admin_name + " " + cmd;

    system(buffer.c_str());
}
Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035