4

I am writing a c++ application in Windows that runs as administrator. However, while calling the system() command, it seems that the command doesn't have admin privileges (can't create file in the C:\Program Files (x86)\ directory).

How can I avoid using CreateProcess ?

Community
  • 1
  • 1
0x90
  • 34,073
  • 33
  • 137
  • 218
  • Do you absolutely have to use `system()`? if its just create a file you can use file IO perfectly fine. What are you actually using this command for? – Serdalis Mar 20 '13 at 12:43
  • 1
    I am using adb pull, and it is only for self use. – 0x90 Mar 20 '13 at 12:44
  • Saw this http://stackoverflow.com/questions/6418791/requesting-administrator-privileges-at-run-time ? Might help you. And also consider this: http://www.cplusplus.com/forum/articles/11153/ – Kangkan Mar 20 '13 at 12:44
  • Why do you need to avoid using the proper API call? – Lightness Races in Orbit Mar 20 '13 at 12:59
  • 1
    I find it very hard to believe that an elevated process will start another process not elevated. How would the implementation of `system` actually achieve that? – David Heffernan Mar 20 '13 at 13:11
  • @LightnessRacesinOrbit `CreateProcess` doesn't actually allow you to request elevation. You can do it with the `runas` verb to `ShellExecute`, or with a manifest. But not through `CreateProcess`. – David Heffernan Mar 20 '13 at 13:12
  • I think your problem is elsewhere. Please show us the code that is calling `system()`. – Harry Johnston Mar 22 '13 at 02:17

1 Answers1

4

If you use system you can use:

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

Or ShellExecute:

ShellExecute(hwnd, "runas", "program.exe", 0, 0, SW_SHOWNORMAL);

This Stackoverflow Question
details the CreateProcess method pretty well.

Community
  • 1
  • 1
Serdalis
  • 9,469
  • 2
  • 33
  • 54