Sign in to follow this  
Hedanito

Visual studio and console windows

Recommended Posts

So I am writing a small engine for a school project, and I want to be able to compile it both as a debug and release version. The debug version has a console window, and the release version doesn't. This works fine but the project automatically starts with a console windows, so even with the release build you will see the console window pop up for a short moment before it gets removed. This looks kinda lame so I want to know how to start a project with no console window at all so I can create one when I need it, instead of doing it the other way around.

I know creating a win32 project works, but I want to be able to port the engine so I want to start from main instead of WinMain.

Oh and another short question, the function printf has a ... parameter. I however am unable to find any documentation on this (google does not like ...). It seems like it could do some cool stuff. So does anyone have any idea how that works?

Thanks in advance.

Share this post


Link to post
Share on other sites
1.) printf takes a variable number of arguments; that's what the "..." is telling you. Each argument after the first gets "substituted into" the string. For example, here,
printf("My favorite number is %i.\n", 2);
printf("My favorite numbers are %i and %f.\n", 2, 0.3);

the two different calls to printf are done with different numbers of arguments. Moreover, note that the type of the argument is inferred at runtime from the corresponding format string.

2.) I'd just deal with the "WinMain" vs. "main" issue using appropriate #defines. Put all your real code elsewhere, and treat these as nothing more than OS-specific entry points that immediately call OS-independent functions. E.g.,

#ifdef _WIN32
CALLBACK WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// Do Window-specific setup. Then,
doEverything();
}
#endif

#ifndef _WIN32
int main(int argc, char ** argv)
{
// Do generic 'nix setup. Then,
doEverything();
}
#endif

I don't mean to suggest this particular form. E.g., I wouldn't name a function doEverything. But you get the point.

Finally, if you're creating console windows on-the-fly, etc... is your code really cross-platform to begin with?

Share this post


Link to post
Share on other sites
Quote:

Oh and another short question, the function printf has a ... parameter. I however am unable to find any documentation on this (google does not like ...). It seems like it could do some cool stuff. So does anyone have any idea how that works?

It is a Varadic Function. You can use the va_* functions to create your own functions that support the ... . BUT they aren't type or size safe at all. You are better off learning about operator overloads and how std::cout chains the << operator for type safe variable arguments. Or templates. Or any number of other ways of going about it in a more type safe way.

Share this post


Link to post
Share on other sites
1.) I know how printf works, I was just wondering how the ... argument works, and if there is a way to use it yourself someway.

2.) I think that is a rather ugly solution. I was hoping I can just change some project settings. After all, a Windows project does it somehow as well. Also, creating console windows can easily be wrapped, just like window creations, allowing me to use the same function everywhere in my code. So it could easily be cross made platform. And it that same way, I want to always use main as my entry point.

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
Quote:

Oh and another short question, the function printf has a ... parameter. I however am unable to find any documentation on this (google does not like ...). It seems like it could do some cool stuff. So does anyone have any idea how that works?

It is a Varadic Function. You can use the va_* functions to create your own functions that support the ... . BUT they aren't type or size safe at all. You are better off learning about operator overloads and how std::cout chains the << operator for type safe variable arguments. Or templates. Or any number of other ways of going about it in a more type safe way.


That should help me along :). I know about templates etc, but I just thought it would be interesting to know about this kind of stuff. I would maybe also like to make a print function that for example automatically adds the time of the current message in front of it, but still have the same functionality as printf. It all looks a bit hacky though :P But I should be able to find that out myself now, thx :).

Share this post


Link to post
Share on other sites
Quote:
So I am writing a small engine for a school project, and I want to be able to compile it both as a debug and release version. The debug version has a console window, and the release version doesn't. This works fine but the project automatically starts with a console windows, so even with the release build you will see the console window pop up for a short moment before it gets removed. This looks kinda lame so I want to know how to start a project with no console window at all so I can create one when I need it, instead of doing it the other way around.


Compile as a Window application and create the console window in code. Here's a link describing how to do it, as well as how to redirect i/o to the console so you can use printf/cout, etc.
http://www.halcyon.com/~ast/dload/guicon.htm

If you want something portable you can look into QT or another API.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hedanito
1.) I know how printf works, I was just wondering how the ... argument works, and if there is a way to use it yourself someway.

There is, once I wrote a function using the ellipsis that used fread and fwrite to write out multiple variables in a single function call. It turns out that even though it worked, it wasn't really that useful at all.

I suspect you'll find something similar with other things you try to do with it.

Share this post


Link to post
Share on other sites
Quote:
Original post by taz0010
Quote:
So I am writing a small engine for a school project, and I want to be able to compile it both as a debug and release version. The debug version has a console window, and the release version doesn't. This works fine but the project automatically starts with a console windows, so even with the release build you will see the console window pop up for a short moment before it gets removed. This looks kinda lame so I want to know how to start a project with no console window at all so I can create one when I need it, instead of doing it the other way around.


Compile as a Window application and create the console window in code. Here's a link describing how to do it, as well as how to redirect i/o to the console so you can use printf/cout, etc.
http://www.halcyon.com/~ast/dload/guicon.htm

If you want something portable you can look into QT or another API.


Please read the question more carefully as well as the replies.

I just want to make clear that this is not something that must be fixed. It is simply a slight annoyance. I just want a better and more elegant way to do it. So please take that in consideration before you come up with some way that has to take special cases into account (for example, Windows).

Share this post


Link to post
Share on other sites
In Visual Studio you have two* options for executables:


  1. Compile as a Windows subsystem project. You get no console window, and the top
    level function is WinMain.

  2. Compile as a Console subsystem project. You get a console window, and the top level function is main.



There is no project setting for top level function of main, no console window.

You can do it yourself though. Compile for the windows subsytem in release mode and the console subsytem in debug mode, and add the following source file to your Visual Studio project...

#include <windows.h>

int main();

CALLBACK WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
return main();
}





If you need to use the argv & argc parameters, you need to parse the lpCmdLine in WinMain into the correct forms. Porting to another platform is easy - simply don't compile this file on other platforms.


*There are more, but they're not interesting for the purposes of this discussion.

Share this post


Link to post
Share on other sites
I've somehow managed to make it run without a console window and still start with the main function.
In the project settings I compile it as a Windows project, and I set the entry point to main.
This works fine until I try to create a console window.
When I try to create a console using AllocConsole() it gives me a "Access is denied" error.
When I call FreeConsole() before AllocConsole() I get a "Parameter is incorrect" error (even though the function has no parameters).
This is not caused by FreeConsole(), I get no errors when I just use FreeConsole.
In both cases, the console still shows up, but I am unable to print something on it.

Share this post


Link to post
Share on other sites
You can't just set the entry point to main. The entry point isn't the top level user function - it's a function which initializes the C++ runtime and initialises static and global objects. If you set the entry point to your top level user function, your program will almost certainly crash.

If you want to know more, put a break point in main and step up the callstack - I believe VS2010 comes with full source code to the CRT.

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