Console Window disappeared

Started by
15 comments, last by tre 14 years, 7 months ago
Hi, having a bit of a problem here. I'm between being a beginner and... less of a beginner, I guess :) And I'm programming some stuff in OpenGL right now. Problem is... somehow and along the way the black console window disappeared and I'ven't even thought about it until now, but I really need it back to print confirmations into when I'm testing extensions and availability of OpenGL and GLSL and so on. Just tell me what you need from me, code wise and I'll post it. I don't even know where to begin, so I'll let you tell me. If it matters I'm using the NEHE framework for OpenGL and just standard C++ in Visual C++ 2008 Express Edition. Hope someone can help me, can't seem to find information on this anywhere (but I do find a lot of information on how to make the console window disappear, though) :) Thanks! Marcus [Edited by - jpetrie on September 11, 2009 10:19:09 AM]
Advertisement
This is usually caused by you creating a Win32 application instead of a Console application. You can re-create the project, or go to project settings (Alt+F7) -> Linker Settings -> System -> Subsystem: Console (That might not be the exact location, but it's somewhere near there anyway).
When you started using OpenGL you would have also changed to using a different type of project - Win32 Application rather than Console Application - your Win32 application does not (by default) include a console window, but you can create one using the AllocConsole function. See also Creation of a Console.

Hope that helps. [smile]

- Jason Astle-Adams

Thanks guys!
Evil Steve, your solution did not work with my project. When I changed the subsystem the application failed to build.

jbadams, this worked. I just put the AllocConsole() call in my WINAPI WinMain-function and now I've got a console running. Seems like I can't print to it though. I try to check for the OpenGL version and print it to the console with printf("OpenGL version 2.0 - OK"). The else statement is similar, just with NO, instead of OK.
Nothing prints to the console.
I do this in my initialization function to not get it called every time I update the screen :)
You cannot use printf with a Win32 console. Win32 consoles have their own function for writing.I made a tutorial about creating and using a Win32 console.
You can find it here. here. If you have any problems, I'll be glad to help you solve them.
-------------------------------------------------------------------------------------------
My Website - Portfolio, Tutorials
My blog
Quote:Original post by tre
Thanks guys!
Evil Steve, your solution did not work with my project. When I changed the subsystem the application failed to build.
What was the error? If it was "unresolved external symbol 'main'", you'll need to change your WinMain function to int main(int, char**). If you need the HINSTANCE parameter passed in, you can use GetModuleHandle(NULL).
Hi again.

Evil Steve, that was the error message I got, yes. I don't know what else would have to be rewritten to convert my program into a console application though, so I'll probably keep it as is. And I really don't know how to get the hPreviousInstance, lpCmdLine or nCmdShow. So it's not looking like the smoothest of problem solvers right now. I might get back to this, though.

ArthY303, I've downloaded your tutorial, I got it working somewhat. It's writing to the console, but the words aren't matching.
If I send "Hello" to the screen, your code outputs "H e l".

I'm really happy you guys are taking the time out to help me with this.
And I'm really trying to understand these new things. I didn't really think I'd be this difficult to get a console working for my application. Anyone know why there aren't any native console functions for win32 applications?
I mean, sure, complete control over what goes where is great but why are there no standard commands for writing to the console. There's AllocConsole to create it, but it's extremely difficult to use it after it's been created.
I don't know, but it just seems weird.

Thanks!
Hello ... I think you are doing something wrong because I tried outputting "Hello" in the console and it worked as it should. Posting your console code might be a good idea. Without the code there is not much I can tell you.

-------------------------------------------------------------------------------------------
My Website - Portfolio, Tutorials
My blog
Hi again.

ArthY303, I'll post the code below.
Since our projects doesn't look much alike I've been trying to piece it together.

(Output below)
template <typename type> wstring ToString(type t){	static wostringstream buffer;	buffer.str(L"");	buffer.clear();	buffer << t;	return buffer.str();}template <typename type> HANDLE Write(type text);BOOL writeToTheConsole(void){	HANDLE consoleHandle = NULL;	if(consoleHandle = NULL){		return FALSE;	}	MessageBox(NULL, "Writing to the console", "console msg", MB_OK);	wstring name = L"Test Name";	int age = 26;	char gender = 'm';	double salary = 2500.13;	Write(L"Name: " + name);	Write(EndLine);	Write("Age: " + age);	Write(EndLine);	Write("Gender: " + gender);	Write(EndLine);	Write("Salary: ");	Write(salary);	Write(EndLine);	return TRUE;}template <typename type> HANDLE Write(type text){	static wstring writeString;	static HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);	if(consoleHandle == NULL){		MessageBox(NULL, "No console has been created", "console msg", MB_OK);		AllocConsole();	}	writeString = ToString(text);	WriteConsole(consoleHandle, writeString.c_str(), (DWORD)writeString.length(), NULL, NULL);	return consoleHandle;}


Instead of using a separate function to call the console I just do it in my WinMain with AllocConsole();
Like this:
HANDLE CreateConsole() {	if(!AllocConsole()) {		MessageBox(NULL, "Konsol skapades ej!", "Information", MB_OK);	} else {		MessageBox(NULL, "Konsol skapad!", "Information", MB_OK);	}	return GetStdHandle(STD_OUTPUT_HANDLE);}// Program Entry (WinMain)int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){	// create a console	CreateConsole();	...}


I call writeToTheConsole() from my initialization point. No frame updates should mess with the output. Still, with the above code, the output is:
N a m e :  T el eS a l a 2 5 0 0


Thanks again,
Marcus

** Updates: Trying to get the code tags to work **
Hello ... I don't really know what is wrong with your code. Here is a simplified version of what you have to do to write to the console:

#include <windows.h>#include <string>#include <sstream>using namespace std;template <typename type>wstring ToString(type t){	static wostringstream buffer;	buffer.str(L"");	buffer.clear();	buffer << t;	return buffer.str();}template <typename type>void Write(HANDLE consoleHandle, type t){    static wstring temp;    temp = ToString(t);    WriteConsole(consoleHandle, temp.c_str(), (DWORD)temp.length(), NULL, NULL);}int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int cmdShow){    HANDLE consoleHandle = NULL;    AllocConsole();    consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);    if(consoleHandle == NULL)        return 1;	MessageBox(NULL, L"Printing Text" , L"Printing Text" , NULL);	Write(consoleHandle, L"This is going to be displayed in the console");	MessageBox(NULL, L"Finished Printing Text" , L"Finished Printing Text" , NULL);    FreeConsole();    return 0;}


I hope it helps ...


EDIT:

BOOL writeToTheConsole(void){	HANDLE consoleHandle = NULL;	if(consoleHandle = NULL){		return FALSE;	}	MessageBox(NULL, "Writing to the console", "console msg", MB_OK);	wstring name = L"Test Name";	int age = 26;	char gender = 'm';	double salary = 2500.13;	Write(L"Name: " + name);	Write(EndLine);	Write("Age: " + age);	Write(EndLine);	Write("Gender: " + gender);	Write(EndLine);	Write("Salary: ");	Write(salary);	Write(EndLine);	return TRUE;}


There is a problem in this function because:

HANDLE consoleHandle = NULL;

if(consoleHandle = NULL){
return FALSE;
}

First you make the console handle NULL. After that you check it and it is always going to be NULL. Also, there should be == instead of =. So the program will never get to writing anything to the console in this function.

-------------------------------------------------------------------------------------------
My Website - Portfolio, Tutorials
My blog

This topic is closed to new replies.

Advertisement