Sign in to follow this  
tre

Console Window disappeared

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 e

l e
S a l a 2 5 0 0


Thanks again,
Marcus

** Updates: Trying to get the code tags to work **

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
With the code you posted:
- I copied and pasted it into the file which contains the WinMain function
- Removed the L""'s to get the code to compile

I removed all previous code, nothing but this is now trying to create and write to a console window.

And the output is:

T h i s i s g o i n g t o b e d i

With the cursor on the end, one space behind the "i".



Edit...
When I went through the previous code and saw that as well. Changed it but with no change. The strange thing is that I did get output with that code :)

Share this post


Link to post
Share on other sites
What IDE are you using ? If your IDE doesn't use UNICODE you need to make these changes to the code:



#include <windows.h>
#include <string>
#include <sstream>

using namespace std;

template <typename type>
string ToString(type t)
{
static ostringstream buffer;

buffer.str("");
buffer.clear();
buffer << t;

return buffer.str();
}

template <typename type>
void Write(HANDLE consoleHandle, type t)
{
static string 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, "Printing Text" , "Printing Text" , NULL);

Write(consoleHandle, "This is going to be displayed in the console");

MessageBox(NULL, "Finished Printing Text" , "Finished Printing Text" , NULL);

FreeConsole();

return 0;
}





Copy the code and try it now and tell me what output you get .

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

Share this post


Link to post
Share on other sites
That did the trick! Where can I find "deeper" information about the differences between "wstring" and "string"? wostringstream and ostringstream? What's the "w"? :)
I'd like to understand what happened :)

I'm using Visual C++ 2008 Express Edition. Don't know about UNICODE, where can I find information on that?

Thank you so very, very much for all your help. Every little thing that gets in my way makes me better at figuring out the programming. It's very much to take in (even in bits) but it's slowly falling in place at least :)

Share this post


Link to post
Share on other sites
Well ... wstring and string do the same thing , basically. The first one is used with Unicode or wide strings and the latter with regular strings.

Just like in the case of wstring and string , there is wostringstream and ostringstream.

UNICODE is just a character set, like ASCII only much more vast. It contains like almost every character of every language.

The fact is: every Windows function has two versions one which uses Unicode and one that doesn't. For example CreateWindowW() which uses Unicode and CreateWindowA() which doesn't. You don't have to explicitly state which version of the function you want because the compiler is smart enough to know which character set you are using. So you can just call CreateWindow().

I hope I've cleared some things up for you. If you have other questions, just post them here and I will try to answer them.

EDIT: I'm a bit suprised because I knew that Visual C++ 2008 Express uses Unicode by default whereas your version doesn't.

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

Share this post


Link to post
Share on other sites
Okay, that's really helpful. Now I see why my "MessageBox" calls sometime fail and why it says something like: "'MessageBoxA' : cannot convert parameter 2 from..." and so on.
The "L"'s before the strings, does it tell the compiler to run with UNICODE instead of ASCII on the string it's connected to? I thought it must have something to do with the formating of the string.

Edit: Yes, that's strange. I haven't changed the options other than the usual stuff with OpenGL and so on. No changes to the text format have been made.

Share this post


Link to post
Share on other sites
Yes ... something like that. You put an L in front of a string and you basically tell the compiler that you want the wide version (or the Unicode version) of that string.

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this