0

I have a simple program that reads in a basic config file which contains a user provided path. I'm trying to use that path to call system to execute a .bat that resides at the location provided. I'm able to pass the stored path directly into system and it attempts to run the .bat fine, but it needs to try and run it as an admin. I came across the following post: How to call system() in an opened administrator program and gives it the same privileges?

I'm building the string as indicated in the above post, but when I try and pass this new string into system, it tells me "The system cannot find the file specified". Here's the (most likely wrong) way I'm building the string that I pass into system.

std::string adminFilePath = "runas /user:<admin-user> \"";
adminFilePath.append(configFileSettings.path.c_str()); //Append the path of the file that we got from the config file.
adminFilePath.append("\"");
system(adminFilePath.c_str());

My assumption is that I'm should be trying to build a basic string representing what I'd type right into a cmd window to execute the .bat, but obviously I'm wrong somewhere.

Community
  • 1
  • 1
JSicking84
  • 55
  • 6
  • If you print out `adminFilePath` does it look valid? What happens if you try to run it manually? – NathanOliver Nov 28 '16 at 16:15
  • When I print `adminFilePath` it looks like i'm expecting it to... `runas /user: "C:\Windows\batFile.bat"` The path looks identical to the print i'm getting off the `configFileSettings.path.c_str()` If I call`system(configFileSettings.path.c_str());` it executes the .bat just fine. trying to call `system(adminFilePath.c_str());` results in the "System cannot find the file specified" – JSicking84 Nov 28 '16 at 17:46
  • What happens if you just hard code it and use `system(R"(runas /user: "C:\Windows\batFile.bat")");`? Do note that is a C++11 feature. – NathanOliver Nov 28 '16 at 17:53
  • Compiler wasn't a fan of that. Guess i'm not currently utilizing C++11 features? Sorry that's not more helpful. This was an old project created in VS2010 that I've ported over to 2015. – JSicking84 Nov 28 '16 at 18:46
  • OK. What happens if you use `system("runas /user: \"C:\Windows\batFile.bat\"");`? – NathanOliver Nov 28 '16 at 18:48
  • Still get "The system cannot find the specified file". – JSicking84 Nov 28 '16 at 19:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129267/discussion-between-wannabecoder-and-nathanoliver). – JSicking84 Nov 28 '16 at 19:20

1 Answers1

0

Check file name, that you dont use single . Check current folder for program you'really running if path is relative Do not use system... especially if you have non-an so folder/file names. system () is ancient attempt to implement posix function and supports only ANSI and may be confused by modern quoted arguments as well. Use execve or spawn.

In fact, you can avoid running runas at all Requesting administrator privileges at run time

Community
  • 1
  • 1
Swift - Friday Pie
  • 8,633
  • 1
  • 16
  • 31