3

I'm trying to find a way to run CMD commands as Administrator in my C++ code.

i.e:

system("taskkill /im mygame.exe");

This won't work and it will pop up an "Error: Access Denied" Message. If I run Windows CMD as Admin and execute the same command directly, it will work perfectly.

So what would be a way to use CMD as Administrator automatically in C++?

I've read some other threads on here but nothing I can directly relate/ get from them.

Examples are very appreciated.

Aaron
  • 113
  • 1
  • 8
  • 3
    Not quite sure in Windows, but I suppose you need to run your application in Admin rights, have a try? – dguan Feb 21 '16 at 00:33
  • 1
    Possible duplicate of [How can I run a child process that requires elevation and wait?](http://stackoverflow.com/questions/4893262/how-can-i-run-a-child-process-that-requires-elevation-and-wait) – MrEricSir Feb 21 '16 at 00:34
  • @MrEricSir I don't see how that thread relates to my question/ would solve my problem, might be because I'm new to C++ and need a simpler answer that relates more to my specific case or other insights on this topic. – Aaron Feb 21 '16 at 00:48
  • We may note that the other question is not a duplicate; he needs to elevate something that isn't manifested for elevation. – Joshua Feb 21 '16 at 04:37

2 Answers2

0

The correct answer is to launch via a helper EXE manifested with requireAdministrator.

Your helper program reads:

#include <process.h>
int main(int argc, char **argv, char **env) {
    return _spawnve(_P_WAIT, argv[1], argv + 1, env);
}

You will not be able to start your own child process with CreateProcess or system(). You must start the helper program with:

ShellExecute(hwnd, "helper.exe", "whatever you were going to pass to system()", NULL, NULL, 0);

Where hwnd is some window you have (pass NULL if you have no visible window).

Ref1: How do I force my .NET application to run as administrator?

Ref2: https://msdn.microsoft.com/en-us/library/bb756929.aspx

Ref3: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776886%28v=vs.85%29.aspx

Community
  • 1
  • 1
Joshua
  • 40,822
  • 8
  • 72
  • 132
  • I actually didn't get anything about the code you just wrote (as I said I'm basically new to this) but I did find an easier way (in case anyone goes through the same in the future). In Visual Studio C++, go to Project > Project Property > Linker > Manifest File > and set UAC Administration level to > Require Administration. This will ask for Admin rights every time you open the .exe file (and execute the System( ) commands as Admin as well. – Aaron Feb 21 '16 at 04:39
  • Which does work of course, but not the question asked. Nevertheless, go for it. – Joshua Feb 21 '16 at 16:29
-2

The usual way would be to use runas:

system("runas /user:Administrator@domain \"taskkill /im mygame.exe\"");
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 1
    @ChrisDood Hi, sadly this won't work. even though the program asks me for the Admin's password, it will still display the same error afterwards (Access Denied). – Aaron Feb 21 '16 at 01:09