I am looking for a way to start a process and not to capture its STDOUT/STDERR.
I.e. I want the STDOUT/STDERR to be printed directly to the shell, just like in a regular stdlib system() call
Thanks
Yes, you can do this using QProcess::setProcessChannelMode(ProcessChannelMode mode) with argument QProcess::ForwardedChannels. Here is the documentation entry: link
Another option for something equivalent to a stdlib system() call, is QProcess::startDetached() static method. Note that it will not wait for the process to finish, again, like the stdlib system() call
Related
I tried with findwindow and sendmessage, but, with sendmessage I only can send message for the GUI. Help!!!!!
Try to use the keybd_event function. It's not very friendly so if you want to use it for complex tasks you may take a look to this library on CodeProject that emulates the well-known SendKey command for VBScript/.NET.
I think that best way to start command line process using CreateProcess function that takes STARTUPINFO structure with input / output handles specified.
When cmd process is already started use SendInput. But IMHO this way is less reliable.
I can't call taskkill or tskill inside VC++ with system i don't know any alternatives, i have tried writing a .bat file and trying calling tskill, that dind't work either.
system("tskill example");
system("dins.bat");
they don't work.
thanks
Use TerminateProcess instead. I also recommend reading this.
I have a program that executes various shell commands via system() and occasionally prints to cout. I want to redirect all output coming from system() calls to a log file so they don't clutter up the normal output. Can I do this without having to append > log to all my system commands?
Looks like you can use popen
Close the stdio file descriptors (0, 1, and 2) and re-open them on whatever output device you like.
Using system is just a bad idea, period. If you use fork and execve or posix_spawn, you can easily make the necessary redirections and avoid all sorts of vulnerabilities from shell quoting issues.
If you can use a library that wrap process call. It is hard to code from posix. I use boost.process, it works fine. you can simply tell the lib how you want the output to be redirected...
my2c
I have a c++ program (very complicated, and lengthy both in code and execution time).
Once in a while this program stops and calls a user-specified shell script.
Before calling the script, my program creates a .out file with current data. I call the script via system() command. The script then reads the .out file, and creates its own script.out file and exits.
Then the system() function call ends, and my program reads and parses the script.out file.
Question: is there a better way to execute communication between my c++ program and a random shell script?
My intent is to have full communication between the two. Script could virtually "ask" the program "What data do you have right now?" and the program would reply with some strict convention. Then the script could say "Add this data...", or "delete all your previous data" etc.etc.
The reason I need this is because the shell script tells the program to modify its data. The exact data that was put in the original .out file. So after the modification is done -- the actual data held by the program does not correspond to the data written in the .out file.
Thanks!
P.S.
I swear I've searched around, but everyone suggests an intermediate file.
There are certainly ways to do that without intermediate files. The most common approach is to use command line arguments for input, and pipes for standard output; others also use pipes for input. The most straight-forward alternative to system then is to use popen.
On a unix-like system? Perhaps pipe (2) will work for you?
From the man page (Mac OS X 10.5 version):
SYNOPSIS
#include <unistd.h>
int pipe(int fildes[2]);
DESCRIPTION
The pipe() function creates a pipe (an object that allows unidirectional
data flow) and allocates a pair of file descriptors. The first descrip-
tor connects to the read end of the pipe; the second connects to the
write end.
You will, of course, have to follow the creation of the pipes with a fork and exec pair. Probably this has already been answered in detail, and now you know what to search on...
It's been a while since I did this, but:
In the main process, before forking the sub-process you call pipe twice. Now you have two pipes and control both ends of both of them.
You fork.
The main process will read from one pipe and write from the other. It doesn't matter which is which, but you need to be clear about this.
The child process will call one of the exec family of function to replace it's image with that of the shell you want to run but first you will use dup2 to replace it's standard input and output with the ends of the two pipes (again, this is where you need to be clear about which pipe is which).
At his point you have two processes, the main process can send things into one pipe ad they will be received on the standard input of the script, and anything the script writes to it's standard output will be sent up the other pipe to the controlling process. So they take turns, just like interacting with the shell.
You can use pipes or (maybe more convenient) sockets - for example frontends to gdb, or expect do that. It would require changes to your shell scripts, and switching from system() to more low-level fork() and exec().
It's rather complicated so please, be more specific about your environment and what you need to clarify.
You are asking the question on Interprocess Communication (IPC).
There are a lot of ways to do that. You can do a simply search and Internet will return you most answers.
If I am not wrong, Google chrome uses a technique called Named Pipe.
Anyway, I think the most "portable way" is probably a file. But if you know you are working on which operating system, you can definitely use most of the IPC techniques.
I'm using ShellExecuteEx to execute a command in C. Is there a way to use ShellExecuteEx and capture standard in/out/err?
Note: I don't want to use CreateProcess.
I use to found the problem like you.
Suppose, You want to capture the output from STDOUT that it's generated by dir command and save the captured into out.txt.
Use text editor and type dir > out.txt and save it with mybat.bat (*.bat, don't *.txt)
In your c/c++ program, type WinExec("mybat.bat", SW_HIDE); and run your application.
Open the out.txt you will see the name of folders and files in current directory.
Also, you can run any executable files (*.exe) at the same way as follow.
xxx.exe > out.txt
I hope it can be helps you.
Sorry, my English really not good.
No. The only way to do this is to use CreatePipe and CreateProcess. See the MSDN article here
That's not possible. ShellExecute(Ex) basically executes the application in the context of the shell - so you are basically doing what explorer does.
Capturing STDIN and STDOUT is something the shell generally doesn't do, you you will have to go the CreateProcess route (which, after all, is what ShellExecute eventually calls if the file to execute is a program and the verb is 'open').
As mentioned by pilif and Bob, you need to use CreateProcess.
If you want code that wraps it all up for you, I do have a class for this exact issue at:
http://code.google.com/p/kgui/source/browse/trunk/kguithread.cpp.
The class (kGUICallThread) handles Linux, macOS and Windows versions. The code is licensed LGPL.
CreateProcess is what most people use.
You may also want to consider using _popen
http://msdn.microsoft.com/en-us/library/96ayss4b%28VS.80%29.aspx