QProcess STDIN with show console - c++

I want to create a process under QProcess with console show and acccess STDIN and STDOUT streams.
my code:
QProcess *p1 = new QProcess(this);
p1->start("cmd.exe");
if I want to show console I must use startDeatached() function, but by using this, I lost my STDIN/OUT access in my program.
if I want to have access these, I dont have my console show!!!???
help me tanx.

after more investigations, I did not find my exact answer. But I did a quick trick (kalak rashti) for this problem.
I used a QTextEdit custom class to simulate some thing behaves like console. frome this link:
https://code.google.com/p/qterminalwidget/source/browse/trunk/
with some changes for my purpose.

Related

QProcess doesn't show the command window

I have the following code which runs an exectuable using QProcess. The code all runs fine and the new executable runs and is all fine.
QString fileName = ui.textBrowser_csvFile->toPlainText();
QString tableName = ui.textBrowser_2->toPlainText();
QString program = "resources/myExe.exe";
QStringList arguments;
arguments << tableName << fileName;
bool res = QProcess::startDetached(program, arguments);
It is a Qt Console Application using QCoreApplication and there it doesn't spawn the terminal window like it would if I run it normally. It would be useful to monitor the progress of the executable so how do I get my Application to run the new program and display the terminal window?
Edit Possible duplicate does technically answer the question, but I have answered this question with a working solution.
So as discussed in the comments on my questions this StackOverflow post explains that this is infact correct behaviour when using the startDetached() function.
I'm not entirely sure what the answer to that question was suggesting to do but here is my working solution.
system() is a windows specific function which "can execute any command that can run on terminal if operating system allows" link
If I replace this line:
bool res = QProcess::startDetached(program, arguments);
with the following, then it works:
system(QString("D:\\Qt\\5.9.1\\msvc2017_64\\bin\\myApp.exe " +tableName +" " + fileName).toStdString().c_str());
In the short term I have simply moved this application into the Qt folder because it needs the DLLs however with a proper release of this app you can run it from wherever, including from next to the application that is running it.
I do then get a terminal window and my app runns correctly.
When migrating from Qt 5.7.0.0(x86) to 5.10.0.0(x64) I was really surprised to see that using the new Qt version, a child (launched with "QProcess::startDetached") process will not show up it's console (even though it's a console application! (SubSystem:CONSOLE))
MS documentation regarding "AllocConsole" says:
Console applications are initialized with a console, unless they are
created as detached processes (by calling the CreateProcess function
with the DETACHED_PROCESS flag).
https://learn.microsoft.com/en-us/windows/console/allocconsole
Console processes are not attached to a console if they are created
using CreateProcess with DETACHED_PROCESS
https://learn.microsoft.com/en-us/windows/console/creation-of-a-console
So I'm assuming that new Qt versions are using the "CreateProcess" with the "DETACHED_PROCESS" flag.
What I've ended up doing:
For child process I'm now using "SubSystem:WINDOWS". (Really
important)
Inside the child process I'm creating the new console by using "AllocConsole()"
Using: "freopen("CONOUT$", "w", stderr);" and "freopen("CONOUT$",
"w", stdout);" ("stderr" is really important if you want to capture qDebug, qInfo, etc...)
P.s.
If you would need to use "SubSystem:CONSOLE", be sure to call "FreeConsole" before calling "AllocConsole". This is required, because child process will by default be using parent process console...

display printf output in main window in QT

I am new to Qt programming, I have made a simple gui with a single push button. Basically I have written a program in C++ now I want to make Gui for my project. I want to display output of all printf statement in my gui. printf statements showing their output in console but I want to add something similar to console in my gui so that whenever I call printf statement it shows its result in the gui. Could you please guide me how can I do this?
You can use a QLabel to show your output in the GUI.
Everytime you call printf, you call setText(...) instead. Now the debug text will be shown in the text label in your GUI.
You can add several QLabels for different debug outputs, if you want.
EDIT:
This could also be of interest.
use QProcess start your CLI program and use readData/writeData to get your information and put them into a QTextEdit
I have found the answer of my question, I have used textbrowser in gui and make a function which I call for printing my data in gui. I can not show the picture of my gui because I have less reputations.
here is the function that I have used for printing.
void MainWindow::print(const QString &input){
data_lab += input;//to display all data in stream
ui->label->setText(input);
ui->textBrowser->setText(data_lab);
}
and here is the call of function from main.
w.print("hellok\n");
w.print("l\n");
I hope this will help somebody like me.

Key logger wont record key strokes without console

I created a small basic key logger in C++. For some reason when I compile and run the program with the console displayed, it will record every key stroke I make in whatever program I am using such as a browser and store it in a text file. However when I make it so that it WON'T display a console window, it will not record anything and it's just a process in the background doing nothing. Here is the link to my code: http://pastebin.com/4wqQyLJ9
The function that is giving me trouble with hiding the console, is the Stealth() function. Any suggestions, tips or hints will be helpful.
Use this function , it works for me pretty well.
ShowWindow(GetConsoleWindow(), SW_HIDE);
Instead of hiding the window after the program starts, I solved this by not
having a window to begin with. Compile with -mwindows and a window is not
created when the program starts.
Example
I would consider a Windows Service for this kind of thing if you don't need UI. Also using GetAsyncKeyState can be more stealthy if required. This C++ source might be of use...
Windows Service Keylogger

Win32 application with console output and without new window

I'd like to create a tool that can either act as command line (display some console output based on input parameters), or display a window, based on input parameters.
I'm using MSV2012 with C++, and it seems you have to 'choose' between console and window app.
I know the net is filled with samples which use AllocConsole() and redirect std::out, but it doesn't make it feel like a command line application : calling the exe from the windows console will open a new window with the console output...
Is there a way to have it use the current console window instead of allocating a new one?
If not possible, I'll make 2 applications instead, but that's a pity..
Someone else may have a more authoritative answer, but I don't believe it's supported.
The usual workaround is to create a Windows app, but have a command-line wrapper that launches it from the CLI (and provides a channel for communicating with the original console).
It's not technically supported but I found a good solution by getting a snapshot for the current process, finding the parent process, attaching to it's console if it's a console app or creating one with AllocConsole, redirecting the output, getting the thread of the parent process if it's cmd.exe and suspending it, resuming it just before I exit my app

Calling command prompt from Qt application without freezing?

In my Qt GUI application, I am calling the command prompt through:
system("lots.exe & of.exe && commands.exe");
It opens up the command prompt (like I want it to), but freezes the Qt GUI application until I close the command prompt. Is there someway to prevent this? I saw that there is a QProcess class, but can't get it to bring up the command prompt.
Any help would be greatly appreciated!
QProcess is really the answer. If you want to use something like system() you'll have to either put the call in another thread or use popen or something simmilar for your platforms.
QProcess does have the setReadChannel which you could use to display your own console window to show the output.
You just need to put that system call in a separate thread.
If you do not need any of the output, the easiest way would be to use QProcess::startDetached().
http://doc.qt.io/archives/4.6/qprocess.html#startDetached
If you do need the output, QtConcurrent::run with a futurewatcher containing the output would be less overhead/work than deriving QThread.