Command prompt open @ the same time as another window?

Is it possible to keep Command prompt open at the same time as another window?
please post sample code...
What do you need? Two console windows? Or one console window and a GUI window?
A console window for error messages and a GUI for OpenGL.
Never done, but I am guessing you can. I imagine that you require on thread for the console window and another thread for the GUI part. Never done OpenGL or DirectX myself, so I don't think I can elaborate further.
If it's just for debugging puposes, it's pretty easy to create a new console for your GUI application: you just have to call AllocConsole(). But it's not so easy to attach a GUI application to an existing console.

And as your application doesn't hook up the input and output streams, etc -- as the GUI init routine is being called rather than the console equivalent -- you've got to do it yourself. Which takes more work to do; most of the function below! (this assumes you want to use cout/printf/... to write your output rather than WIN32 calls.)

The following code is what I use to display error and status messages from my GUI apps. It's based on code originally published in an edition of the Windows Developer Journal (1997) which I found on a web site (see link in code comment below).

I just call RedirectIOToConsole() at the top of my WinMain. There is no need to spawn a new thread.

Note that I don't normally put the implementation in a .h file, but as this is debug code I make an exception. This file must be included in just one .cpp file in a project (the one with WinMain in it).

And closing the console while your app's running can cause upset. You can stop the close button from working, but as I only use this approach for development purposes, I have not followed this up.

Andy

e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13
int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
	int nRet = 0;

#if defined(_MSC_VER) || defined(__MINGW32__)
#ifdef _DEBUG
	RedirectIOToConsole();
	std::cout << "Debug Console" << std::endl;
#endif
#endif

	...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once

// guiconsole.h
//
// http://dslweb.nwnexus.com/~ast/dload/guicon.htm
// Adding Console I/O to a Win32 GUI App
// Windows Developer Journal, December 1997

#ifndef _WINDOWS_
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include <cstdio>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <fstream>

#ifndef _USE_OLD_IOSTREAMS
using namespace std;
#endif

void RedirectIOToConsole()
{
	// maximum mumber of lines the output console should have
	static const WORD MAX_CONSOLE_LINES = 500;

	BOOL bRet = FALSE;

	// allocate a console for this app
	{
		bRet = AllocConsole();
	}

	if(FALSE != bRet)
	{
		// set the screen buffer to be big enough to let us scroll text
		{
			HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

			CONSOLE_SCREEN_BUFFER_INFO coninfo;
			GetConsoleScreenBufferInfo(hStdOut, &coninfo);

			coninfo.dwSize.Y = MAX_CONSOLE_LINES;

			SetConsoleScreenBufferSize(hStdOut, coninfo.dwSize);
		}

		// redirect unbuffered STDOUT to the console
		{
			long lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);

			int hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);

			FILE* fp = _fdopen( hConHandle, "w" );

			*stdout = *fp;

			setvbuf( stdout, NULL, _IONBF, 0 );
		}

		// redirect unbuffered STDIN to the console
		{
			long lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);

			int hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);

			FILE* fp = _fdopen( hConHandle, "r" );

			*stdin = *fp;

			setvbuf( stdin, NULL, _IONBF, 0 );
		}

		// redirect unbuffered STDERR to the console
		{
			long lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);

			int hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);

			FILE* fp = _fdopen( hConHandle, "w" );

			*stderr = *fp;

			setvbuf( stderr, NULL, _IONBF, 0 );
		}

		// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog 
		// point to console as well
		{
			ios::sync_with_stdio();
		}
	}
}

// End of File 

Last edited on
Using OpenGL in a window doesn't change the use of the <windows> part of the program @ all.
I already know that it's possible, because Blender (the 3d modeling software) has used the command line type window in conjunction with a gui for years. However multiple languages are used to build it, which made me unsure of whether or not it could be done through c++.
That led me to recently experiment with the <windows> lib. Since I've just started trying to use the library, I'm unsure as to how I'd make the command line window work @ the same time as a GUI. Which ended with me asking for sample code...
Hmm, I'll have to try that...
Thanks for the code.
I may not reply for a couple of days due to the lack of Internet there.
Last edited on
Topic archived. No new replies allowed.