Multiple consoles for a single application C++ - c++

Is it possible to create two console windows (one being the main) and the secondary being a pop-up like a message box in Windows Forms?
I only want the secondary console window to hold IDs (that will be hard coded into the application) So the user does not have to keep returning to the main menu to check available IDs
If so how would you go about it?
Many Thanks

Yes, you can do it.
The solution is actually very simple - our process can start a new helper child-process, so the helper process will display whatever our process sends it. We can easily implement such a solution with pipes: for each new console (that I'll call logger), we'll open a pipe, and execute a Console-Helper application - the role of this application is very simple, it will print everything sent through the pipe. Check out this article Multiple consoles for a single application for details (contains the source code).
In the code, it implement a console class CConsoleLogger, then you can create multiple console windows like:
CConsoleLogger another_console;
another_console.Create("This is the first console");
another_console.printf("WOW !!! COOLL !!! another console ???");
And you will get something like:

Take a look at http://msdn.microsoft.com/en-us/library/windows/desktop/ms682528(v=vs.85).aspx for instructions for creating a console window.

Nop
A process can be associated with only one console.
AllocConsole

Related

Sending a message between two separate applications

Hi,
I'm relatively new to c++ and software development in general, so I hope I can explain clearly my question.
I need two apps, one that listens the keyboard for a combo key press, and one that executes a function when that combo was pressed. I can't make them in a single app, because I'm trying to build something that resembles a kyosk, that when launched, starts a new desktop with limited functionality, and when a combo is detected, the original desktop is switched back. So, what I did, is launch this keyboard hook in the new desktop using CreateProcess, launch a new explorer.exe using the same function and switch to the new desktop.
I was doing some reading about Messages and Message Queues and I found out that this is a way to communicate between threads. So I was wondering if I could create my own Queue, put a message there at the combo key press, and periodically interogate this queue from another process, to make the necessary changes. If possible, could you post a link, or a code sample.
Thanks

Console App that produced a form(Windows Form)?

How can I create a windows form using a console app? Like when I run the console app it will show a form(windows form). Anyone can help me?
First, you have to understand, how windows in Windows works. Each windowed program manages something called a message queue. Messages are informations from system (and other applications), that something happened, for example someone pressed a key or moved a mouse, such that window can react to that user action properly.
Processing messages (including dispatching them to proper window) is a responsibility of the program and in Windows Forms applications is done in Application.Run method (or somewhere near that) by some internal message processing loop. But if you want to display a window in your console application, someone has to take responsibility for this process, otherwise window won't work correctly (actually, they won't work at all).
If you display a window modally, it will process messages coming to it by itself, so you can call directly myWindow.ShowDialog from your code and that will work - but will lock console input away until the window is closed. That means, you will be able to send text to console by Console.WriteLine, but you won't be able to - for instance - ask user for text by Console.ReadLine until the window is closed.
class Program
{
public static void Main(string[] args)
{
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
f.Click += (o, e) => { Console.WriteLine("Clicked!"); };
f.ShowDialog();
Console.ReadLine();
}
}
// (run, then click on a form to observe result)
If, on the other hand, you want to display a window in a non-dialog way, things get complicated. The simplest thing comming to my mind is to create Windows Forms application and use AllocConsole to create separate console window for application (for example Blender does that). The other option is to create two different applications - console and Windows Forms one, run the second from the first and manage their communication by some mechanism (named pipes, shared global memory, windows messages, TCP/IP etc.)
It is not directly supported no.
What you could do is have two separate applications, one form and one console. Start the console one from the form one and pass data between the applications. If you want to set the form to be "inside" the console application you can use
http://msdn.microsoft.com/en-us/library/aa984420%28v=vs.71%29.aspx

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

It is possible to put a hook catching messages that are send to console by child process?

So I start an ordinary console program and then create another process within it. Next the child process tries to write something onto console that is owned by parent process.
Is it possible to put a hook on this console so when child process tries to write some text I could do search and replace on this text and put it changed onto the console.
(I know it is possible with pipes, but here I ask specifically of doing it at the console level, because some programs behave differently if they see they are attached to pipes instead of console.)
Has SetWindowsHookEx something to do with my question ?
Just to avoid confusion, windows messages aren't involved with this so hooking is entirely the wrong concept.
If you want to change the child processes data written to STDOUT, you'll need to create your own pipes that you pass to the process when creating it. This allows you to read the data from it, change as you require then print to STDOUT yourself. There is no way to intercept data written direct to the console.
So you want to install a winproc hook on the console? Sure, you can do it, first you have to get the HWND (read here) of the console window then you can call your SetWindowsHookEx() and hook it. Do note that in your hook procedure , calling defwinproc will call the console's origional winproc, so if you don't want to modify the entire behavior of the console your in the clear.

Add console to existing MFC application

I'm working with 2 friends in a class project to make a D&D game. so far for the assignments I've been doing character creation stuff and strutting on the command line.
Now we're bringing or part together and I need to output ny dice rolls on a console and a few things on another one that will have to become the main view or tab or whatever it's called when it requires input/attention.
Problem is I never learned MFC yet because I didn't need it. How hard would it be to make a sample MFC console all that I can give to the teammate in charge of the GUI?
Could anyone point me to some instruction on making a console for an MFC app and how to give it output and receive output?
First, you can't. for both Unix/Linux and Windows, there is a one console/process limit. If you want another console, you have to create another process, that writes and reads the other console, while you send and receive the data.
You can use a NamedPipe http://msdn.microsoft.com/en-us/library/windows/desktop/aa365590%28v=vs.85%29.aspx to send data between processes, and the CreateProcess() function lets you create a process with a separate Console window.
Alternatively you can just write a Console-Look-a-Like window in some GUI.