Sign in to follow this  
Dauid

Unity WinMain vs Main

Recommended Posts

Dauid    110
In this thread we see how to invoke the WinMain-method manually. http://www.gamedev.net/community/forums/topic.asp?topic_id=375803 What I wonder is, what is major the diffrence between actually using the WinMain-method and creating a window from a simple main-method? Since it's working by passing NULL to all WinMain's parameters, aren't those parameters obsolete? Are there any functionality of concern that uses any of those parameters?

Share this post


Link to post
Share on other sites
SiCrane    11839
WinMain() is the entry point for an application compiled for the Windows subsystem. main() is the entry point for an application compiled for the console subsystem. When an application is started in the console subsystem you automatically get a console window. If your application runs in the Windows subsystem if you want a console window you need to allocate or attach it yourself.

Share this post


Link to post
Share on other sites
LessBread    1415
The names of the start up function are set by convention not by necessity. You could name the function "sweetness" if you wanted to, so long as you configured your compiler to use the function named "sweetness" as the start up function. Microsoft chose the name "WinMain" to convey that the resulting program would execute within the GUI subsystem, leaving the more traditionally named "main" start function to convey that the resulting program would execute within the console subsystem (aka CUI).

You could write a console program with the start function WinMain if you wanted. To produce a console window you would have to use AllocConsole and then redirect stdout,stdin,stderr to use console io (eg. printf, scanf, cin, cout). If you didn't want to add that extra code, you could configure the project so that the resulting program ran in the console subsystem. I haven't tried it, but I presume that would result in the console window manifesting when the program was executed, just the same as what happens with ordinary console programs.

If you wanted to launch a window from "main" within a console program, just plug in the appropriate window set up code. The functions involved in setting up a message loop invoke the GUI subsystem automatically. You'll still have to contend with the console window, which can get messy.

Yes, the WinMain parameters are pretty much obsolete. The instance argument can be obtained using GetModuleHandle(NULL). The previous instance argument is a throwback to 16 bit windows. It's always null. And there are API functions for retrieving the command line.

It seems to me that the only reason the WinMain parameters remain is to prevent compiler complaints about missing prototypes etc.

Share this post


Link to post
Share on other sites
Sneftel    1788
Quote:
Original post by LessBread
You could write a console program with the start function WinMain if you wanted. To produce a console window you would have to use AllocConsole and then redirect stdout,stdin,stderr to use console io (eg. printf, scanf, cin, cout). If you didn't want to add that extra code, you could configure the project so that the resulting program ran in the console subsystem. I haven't tried it, but I presume that would result in the console window manifesting when the program was executed, just the same as what happens with ordinary console programs.
It would, but it wouldn't be quite the same as a real console program. Most tellingly, it would create a new console window even if run from a command interpreter window.

Share this post


Link to post
Share on other sites
SiCrane    11839
If you want to more closely emulate a console subsystem process, you could first attempt to attach to the parent process' console window. Without error handling, it'd look something like:

BOOL b = AttachConsole(ATTACH_PARENT_PROCESS);
if (!b) {
AllocConsole();
}
freopen("conin$","r",stdin);
freopen("conout$","w",stdout);
freopen("conout$","w",stderr);

Unfortunately, this still wouldn't be exactly the same as an application run in the console subsystem. In particular, it wouldn't play nicely with pipes. Off the top of my head, I can't think of a way to cleanly emulate that.

In any case, just switching the subsystem to console wouldn't allow you to use a program with WinMain() in the console subsystem. The linker will get pissy if it doesn't find main() or wmain(); you'll also need to change the /ENTRY linker option (for MSVC anyways).

Share this post


Link to post
Share on other sites
legalize    116
If for some reason you really needed to do that, you could simply consult the C++ runtime startup code for console applications and emulate what it does. Of course, by the time you do that, you've re-implemented everything they're doing so you might as well just code your application as a console subsystem application. Console apps can call everything in the Win32 API, so there's nothing preventing you from doing GUI stuff in a "console" application.

Share this post


Link to post
Share on other sites
SiCrane    11839
I'm looking at the CRT startup code right now for MSVC 2008 and I don't see anything about allocating the console. Which source files are you referring to? Are you sure that stuff is handled by the CRT and not Windows itself?

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  

  • Similar Content

    • By dobbey
      Who are you?
      We call ourselves Dobbey. Our team currently consists of two people.
      We are both early 20's and come from Germany. At the moment this is our hobby.
      What's the name of the game and what is it about?
      The game is called Insane and it is a 2D adventure puzzle game with horror elements.
      Insane is about a scientist who has been doing human experiments for years.
      His most successful experiment is number 73, which seems to be mentally labile.
      This experiment manages to flee one day but can't distinguish between reality and imagination.
      In this game you will find a creepy atmosphere with difficult and varied puzzles.
      What inspired you?
      The style of the game is inspired by Limbo. But we were also inspired by Jump 'n' Run games and movies.
      Of course, we have also our own elements like an inventory system, butterfly effect, the "INSANE" effect, using a QR-Code..
      Which program do you use to develop the game?
      We are programming it with Unity. In our opinion, it is the best program for indie games.
      And of course we are working with Photoshop to create the graphics.
      What platform will Insane be available on?
      For PC.
      How can I support you?
      Twitter:        http://bit.ly/20POkfM    
      YouTube:     http://bit.ly/2rLXoM0
      Instagram:   http://bit.ly/2qwU2bl
      Facebook:    http://bit.ly/2p3nwRQ
      Website:       http://bit.ly/2s8buqU
       

      Here you can see some of our screenshots.

       

      (You can find the animation on Twitter/Instagram.)
       

       

       

      (You can find the animation on YouTube.)

      What do you think about it?
       
      Yo can see more screenshots or videos on Twitter, Instagram or YouTube.
    • By Liquid1Phantom
      Hello, my name is Thomas and I am currently starting up a project. 
      The project is going to be a very hardcore, and tactical FPS that is similar to Escape from Tarkov. It will be made within Unity, and will be programmed in C#. Currently, I have a Unity project started, an organized Discord setup, and as well as a very organized Google Drive. The Google Drive has art references, and plenty of folders for organization. Lastly, the payment for this project will be solely rev-share as I have no money.
      I am looking for anyone who can contribute: 2D artists, 3D artists, composers, programmers, etc.
      If you would like to help out and be of use, please email me at: thomasmunson2277@gmail.com
      Alternatively, add my Discord: Thomas#3788
    • By ilovegames
      Score points by shooting enemies! Before you is one of the best representatives of the simulator sniper games genre, with stunning graphics, quality effects, and realistic ballistics of bullets. It will not be easy to win for sure. Are you ready? Then take a sniper rifle and fight!
      Download http://falcoware.com/Sniper.php




  • Popular Now