I have a console command, something like:
std::wstring ConsoleCommand;
ConsoleCommand = L"c:\\somepath\\anotherpath\\program.exe -opt1 /opt2 --opt3";
I want to execute this command.
How do I do it?
(It may be a Win32 API function, or a standard C/C++ library.)
Try ShellExecute(). You probably want the open verb. You could also use CreateProcess().
You must _wsystem() or _wpopen() on Windows.
Related
I have a c++ program that run a command and pass some arguments to it. The code is as follow:
int RunApplication(fs::path applicationPathName,std::string arguments)
{
std::string applicationShortPath=GetShortFileName(applicationPathName);
std::string cmd="\""+applicationShortPath +"\" "+ arguments+" >>log.txt 2>&1 \"";
std::cout<<cmd<<std::endl;
int result=std::system(cmd.c_str());
return result;
}
When I run system command, the cmd window appears shortly and then closes, but the result is 1 and the cmd was not run (the command should generate output which is not generated).
To check that the cmd is correct, I stopped the application just before system line and copy/ paste cmd content to a cmd window and it worked.
I am wondering how can I find why application is not run in system()?
the cmd has this value just before running it:
"D:/DEVELO~3/x64/Debug/enfuse.exe" -w --hard-mask --exposure-weight=1 --saturation-weight=0.328 --contrast-weight=0.164 -o "C:/Users/m/AppData/Local/Temp/1.tif" "C:/Users/m/AppData/Local/Temp/1.jpg" "C:/Users/m/AppData/Local/Temp/2.jpg" >>log.txt 2>&1 "
How can I find why it is not working?
Is there any way that I set the system so it doesn't close cmd window so I can inspect it?
is there any better way to run a command on OS?
Does Boost has any solution for this?
Edit
After running it with cmd /k, I get this error message:
The input line is too long.
How can I fix it other than reducing cmd line?
There are two different things here: if you have to start a suprocess, "system" is not the best way of doing it (better to use the proper API, like CreateProcess, or a multiplatform wrapper, but avoid to go through the command interpreter, to avoid to open to potential malware injection).
But in this case system() is probably the right way to go since you in fact need the command interpreter (you cannot manage things like >>log.txt 2>&1 with only a process creation.)
The problem looks like a failure in the called program: may be the path is not correct or some of the files it has to work with are not existent or accessible with appropriate-permission and so on.
One of the firt thing to do: open a command prompt and paste the string you posted, in there. Does it run? Does it say something about any error?
Another thing to check is how escape sequence are used in C++ literals: to get a '\', you need '\\' since the first is the escape for the second (like \n, or \t etc.). Although it seems not the case, here, it is one of the most common mistakes.
Use cmd /k to keep the terminal: http://ss64.com/nt/cmd.html
Or just spawn cmd.exe instead and inspect the environment, permissions, etc. You can manually paste that command to see whether it would work from that shell. If it does, you know that paths, permssions and environment are ok, so you have some other issue on your hands (argument escaping, character encoding issues)
Check here How to execute a command and get output of command within C++ using POSIX?
Boost.Process is not official yet http://www.highscore.de/boost/process/
What I'd like to do is have my C++ code open up Mplus (statistical program that I've downloaded on my computer) and run it. Is it possible?
You may be able to do what you want with std::system() calls like:
std::system("program -e input_commands.txt"); // Assuming it accepts some sort of command line args
std::system("program < input_commands.txt"); // Assuming it responds to stdin
It depends on the program if this approach will work.
I am writing a Qt application that needs to call system programs (netsh) and run them as administrator.
However, QProcess, QDesktopServices and system() don't allow me to run the application as administrator (not even with runas).
The only solution that I found is to use ShellExecute, but it does not even open the program.
My code is:
#ifdef Q_OS_WIN {
ShellExecute(0, LPCWSTR("runas"), LPCWSTR("netsh wlan start hostednetwork"), 0, 0, SW_SHOWNORMAL);
}
I have also tried to use other options, such as open and tried to run other programs, such as Notepad (notepad.exe) and Control Panel (control.exe), nothing worked.
I have also tried to add an manifest file and nothing was solved.
Do I miss something in my code? (examples are welcome).
LPCWSTR("runas") - this is incorrect, you typecast string to widestring, and probably ShellExecute will return error and does not start an application. Specify "L" prefix instead.
Also, you need to split command and parameters, "netsh wlan start hostednetwork" will not work as command name.
Use it like this:
ShellExecute(0, L"runas", L"netsh", L"wlan start hostednetwork", 0, SW_SHOWNORMAL);
I am using int res = system("uname -p"); in my c++ code.
It will give the the result in standard output by using
fprintf(stdout,"execution returned %d.\n",res);
I want to store this result string in a variable, I am unable to store it.
I google it but unable to find proper solution, Can any one tell me the correct way.
First, you don't need to run the uname command programmatically to get your processor. You can simply run the uname(2) syscall (which the uname command invokes). And you could also read and parse /proc/cpuinfo from your program.
If you wanted to read the output of some command, use popen(3) library function.
See also my answer to a related question.
I want to change image's exif data. For that I've used Exiv2.exe. Now I want to fire command from my program which is written in vc++ 08. For modify GPS data of image, exive command is
exiv2 -M"set Exif.GPSInfo.GPSLatitude 4/1 15/1 33/1" D:\test\image.jpg
I've placed exiv2.exe into system32 folder. And this command works fine from command prompt. For example,
C:\Users\Me>exiv2 -M"set Exif.GPSInfo.GPSLatitude 4/1 15/1 33/1" D:\test\image.jpg
Now how can I fire this same command from my c++ program?
Thanks in advance...
Have a look at the documentation for ShellExecute() or CreateProcess() - either of these ought to get you where you want to be.
Finally got it,
const char *change_latitude = "exiv2 -M\"set Exif.GPSInfo.GPSLatitude 14/1 15/1 13/1\" D:\\test\\image.jpg";
system(change_latitude);
In this example assumption is : exiv2.exe in system32 folder.
Thanks...