mfc how to write commands in command window - visual-c++

I need to write some commands in command window using C++ code. How to implement it. I have tried with CreateProcess function but it seems some wrong in it. Please refer my code below:
STARTUPINFO sInfo = {0};
sInfo.cb = sizeof(sInfo);
PROCESS_INFORMATION pInfo = {0};
CreateProcess("C:\\WINDOWS\\System32\\cmd.exe",""0,0,TRUE,
NORMAL_PRIORITY_CLASS,0,0,&sInfo,&pInfo);
It opens the command window successfully. My doubt is how to write command through code in it.

First thing, you need not to create a separate process just to write text output to a console window.
It depends what and how you want to write. You may create a console application itself, or create a console itself, and attach to the current process. You need to use pipes for the same and redirect the output to given pipe (i.e. send data to pipe). At the other end of pipe, you will read the text/buffer and render the output wherever you would like to.
These articles may help:
Console Output from GUI program
Real time Console Output redirection
Since your question is not very clear, this is just assumption.
Or, are you playing with the console itself - like changing colors, dimension etc?

Related

A way to output logs without using process.stdout.write in Node JS?

So I'm trying to pipe two Node based js scripts together, which works as expected doing something like this.
How to pipe Node.js scripts together using the Unix | pipe (on the command line)?
essentially
$ ./a.js | ./b.js
The pipe works fine as long as the only thing output to the next script is valid JSON (for example). But I would like to see some debug logs in the first script (ideally without using the popular debug module). Funny enough, I know the debug module will do this without sending unwanted data to the pipe. How does it do that? I'd rather not dig into their code to see (lazy).
Seems like console.log, and console.error both use process.stdout/err so if I log something out, I end up mucking up the pipe.
Difference between "process.stdout.write" and "console.log" in node.js?
Is there a way to use a different tty socket or something? No idea where to start.
Looks like debug module on npm writes to stderr instead:
By default debug will log to stderr
/**
* Invokes `util.format()` with the specified arguments and writes to stderr.
*/
function log(...args) {
return process.stderr.write(util.format(...args) + '\n');
}

QProcess runs process but readAll returns nothing

I am trying to start a QProcess by
QProcess process= new QProcess();
process.start("javac file.java");
It starts successfully and I can see the output in the Qt Creator's log window. But when I try to read it from the program using process.readAll(), nothing was read. But when I try to do something like
process.start("echo Print this message");
then process.readAll() returns "Print this message".
Can anybody help me why this happens and how can I get that work. I am trying to make a simple IDE with it.
You're reading from the process's standard output channel, but your process outputs on the standard error channel. You need to read both. You also have the option of merging them. See QProcess documentation - read it and make sure you understand it. Edit your question to ask for clarification if anything is unclear.

Can QT open a Linux terminal and then write into it?

I can open a terminal by doing this from my QT code:
QProcess process;
process.start("xterm");
process.waitForFinished(-1);
But then I can't figure out how to write commands to it?
I need to do that because I want my app to ssh an equipment and then write commands once logged in and see the output.
I'm open to other solutions too!
Thanks
QProcess has a write command, but you don't want to be calling waitForFinished.
QProcess proc;
proc.start("xterm");
proc.waitForStarted();
proc.write(someData, dataSize);
If you want response from the terminal, connect a slot to the readyRead() signal
// Qt 5 syntax
connect(proc &QProcess::readyRead, this, &MyClass::readData());
Then call one of the read functions, such as readAll() from your readData slot function.

Application to accept arguments while running

I am using visual studio 2008 and MFC. I accept arguments using a subclass of CCommandLineInfo and overriding ParseParam().
Now I want to pass these arguments to the application while running. For example "test.exe /start" and then to type in the console "test.exe /initialize" to be initialized again.
is there any way to do that?
Edit 1: Some clarifications. My program starts with "test.exe /start". I want to type "test.exe /initialize" and initialize the one and only running process (without closing/opening). And by initialize I mean to read a different XML file, to change some values of the interface and other things.
I cannot think of an easy way to accomplish what you're asking about.
However, you could develop your application to specifically receive commands, and given those commands take any actions you wanted based upon receiving them. Since you're already using MFC, you can do this rather easily. Create a Window (HWND) for your application and register it. It doesn't have to be visible (this won't necessarily make you application a GUI application). Implement a WndProc, and define specific messages that you will receive based on WM_USER + <xxx>.
First and obvious question is why you want to have threads, instead of processes.
You may use GetCommandLine and CommandLineToArgvW to get the fully formatted command line. Detect the arguments, and the call CreateProcess or ShellExecute passing /watever to spawn the process. You may also want to use GetModuleBaseName to get the name of your own EXE.

Catching a direct redirect to /dev/tty

I'm working on an application controller for a program that is spitting text directly to /dev/tty.
This is a production application controller that must be able to catch all text going to the terminal. Generally, this isn't a problem. We simply redirect stdout and stderr. This particular application is making direct calls to echo and redirecting the result to /dev/tty (echo "some text" > /dev/tty). Redirects via my application controller are failing to catch the text.
I do have the source for this application, but am not in a position to modify it, nor is it being maintained anymore. Any ideas on how to catch and/or throw away the output?
screen -D -m yourEvilProgram
should work. Much time passed sinced I used it, but if you need to read some of its output it could even be possible that you could utilize some sockets to read it.
[Added: two links, Rackaid and Pixelbeat, and the home page at GNU]
The classic solution to controlling an application like this is Expect, which sets up pseudo-terminals, does logging, and drives the controlled application from a script. It comes with lots of sample scripts so you can probably just adapt one to fit your needs.
This is what I did in python
import pty, os
pid, fd = pty.fork()
if pid == 0: # In the child process execute another command
os.execv('./my-progr', [''])
print "Execv never returns :-)"
else:
while True:
try:
print os.read(fd,65536),
except OSError:
break
I can't quite determine whether the screen program mentioned by #flolo will do what you need or not. It may, but I'm not sure whether there is a logging facility built in, which appears to be what you need.
There probably is a program out there already to do what you need. I'd nominate sudosh as a possibility.
If you end up needing to write your own, you'll probably need to use a pseudo-tty (pty) and have your application controller sit in between the user's real terminal connection and the the pty device, where it can log whatever you need it to log. That's not trivial. You can find information about this in Rochkind's "Advanced UNIX Programming, 2nd Edn" book, and no doubt other similar books (Stevens' "Advanced Programming in the UNIX Environment" book is a likely candidate, but I don't have a copy to verify that).

Resources