Sign in to follow this  
jeff8j

WinMain vs Main

Recommended Posts

jeff8j    787
I have a game that up until now I have been using sdl to handle the windowing since thats the only thing I use it for I would like to get rid of sdl. I have the ability to run it in server mode and take in command line arguments and not even need a window just run straight from the console. So my question/problem is will the winmain function the same as the regular main function? int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State { Wouldn't that create a window? And if thats the entry point how would I launch the game in server mode?

Share this post


Link to post
Share on other sites
leeor_net    343
By server mode do you mean a command line interface (CLI)? Because if you're looking for a CLI than you don't want winmain.

Winmain doesn't actually create a window. It's just the entry point function for a Windows application. You need to use the Windows API to create windows, draw to them and to listen to and respond to windows messages (and windproc() function... forget the argument list). Note that if you don't respond to window messages your program will 'stop responding'. In older versions of Windows (e.g., the dreaded Windows '95), this could actually cause the whole system to go down. Nice to know how much better Windows handles badly behaved programs.

I would suggest you google Win32 programming. This site looked prommising but I can't attest to how good it actually is.

Just note that Win32 GUI programming can be a nightmare. But if you're willing to plow through it, go right ahead.

It may also be prudent to buy a Windows Programming book. Windows is a very, very large API and there are a lot of things to learn with it.

Good luck!

Share this post


Link to post
Share on other sites
jeff8j    787
I do mean cli when its in server mode it shouldn't need any graphics and im trying to keep things as portable as possible. I have been stuck on this for days now I thought it would be easier going from linux to windows ha wish someone would have told me.

Its funny how you mention that the program will not respond if I dont handle the messages right that's exactly whats happening to me now.

I guess I have a long journey ahead of me I thought opengl was going to be the hard part that actually made since I feel like I got opengl books for no good reason although there is still some advanced things I haven't touched.

Thank You

Share this post


Link to post
Share on other sites
delta user    164
winmain and main are both entry points, which means that those are the functions that get called when your program gets run.

With main, Windows will automaticaly create a console window and bind the input and output streams of your program to that console window. You can create a window but you than would first have to get the parameters that would get passed to WinMain.

With WinMain, you get the parameters needed to create windows right away (sort of), but Windows does not create a console window. So if you want to use a console window in this case you would have to call an API function to open a console window and then bind the input and output stream of your program to the console window.

So basicaly, you can use both entry points (only one at a time). The choice of one only makes it harder to get the other thing done, but it is possible.

[Edit]
Anyway, WinMain does not create a window right away. You would still need to do that. So WinMain can also be used for CLI apps.

Share this post


Link to post
Share on other sites
jeff8j    787
I dont know how but the way you said that made it all the sudden click to me in a way I can use it similar to x11.

Of course I have to take the hard way there is no other lol but I think I can now do it much simpler than I was trying before.

Thank you delta user

Share this post


Link to post
Share on other sites
MichaBen    481
Quote:
Original post by delta user
With main, Windows will automaticaly create a console window and bind the input and output streams of your program to that console window.


Not exactly, main() doesn't have to make a console window, and you can have a console window using WinMain() if you want to as well. A window always has to be created manually, the console window is as far as I know a compiler option.

Share this post


Link to post
Share on other sites
SiCrane    11839
Quote:
Original post by MichaBen
Not exactly, main() doesn't have to make a console window

How would you have it not create a console window?

Share this post


Link to post
Share on other sites
Kippesoep    892
Quote:
Original post by SiCrane
How would you have it not create a console window?

There's a linker option that changes the subsystem to console (/SUBSYSTEM:CONSOLE) and that is actually what causes the console to be created. By using /SUBSYSTEM:WINDOWS that won't happen. It selects the default for, but is otherwise independent of the entrypoint (/ENTRY option). The default for console apps is (w)mainCRTStartup (calling (w)main), whereas the default for Win32 apps is (w)WinMainCRTStartup (calling (w)WinMain). Normally, one doesn't mess with that, as it is simply set when choosing to create a console or Win32 app, but it is possible.

Share this post


Link to post
Share on other sites
delta user    164
Yes ofcourse you need to choose the right linker option.
But I ment that when you are using main, you (most likely) will be compiling as a console app and when you use winmain it is probably a native win32 app.

I just wanted to make it clear that there is no real difference between main and WinMain besides the arguments (if you want it to). Don't make it harder than it is.

Share this post


Link to post
Share on other sites
daviangel    604
Quote:
Original post by delta user
Yes ofcourse you need to choose the right linker option.
But I ment that when you are using main, you (most likely) will be compiling as a console app and when you use winmain it is probably a native win32 app.

I just wanted to make it clear that there is no real difference between main and WinMain besides the arguments (if you want it to). Don't make it harder than it is.

Ah but there is a difference. I think whenever I see main I'm looking at cross-platform somewhat portable code whereas if I see WinMain I'm working with more complexity since it all depends on how they have defined WinMain to work.
For example Visual C++ supports defining a wmain function and passing wide-character arguments to your Unicode application.

Share this post


Link to post
Share on other sites
Somnia    122
Is there any reason not to have a console window sitting there when your game is run? I'm using the default project settings for an SFML app, which has the console sitting behind my game window. I've left it there since I thought it might be handy for debugging output, and I don't want to mess with linker options if I don't have to.

Share this post


Link to post
Share on other sites
MichaBen    481
Quote:
Original post by SiCrane
How would you have it not create a console window?


That depends on the IDE/linker you are using. Some have a simply drop down list where you can pick "Console" or "GUI".

About having a console as well as window, I personally often do that for debug versions of my program. For the release version it's set to GUI of course, as in my opinion having a console window while you have a graphical interface is useless for people other then the developers.

Share this post


Link to post
Share on other sites
SiCrane    11839
Quote:
Original post by MichaBen
That depends on the IDE/linker you are using. Some have a simply drop down list where you can pick "Console" or "GUI".

If all you do is change the subsystem to Windows your program will fail to link, since it will search for WinMain() and not main().

Share this post


Link to post
Share on other sites
Oluseyi    2103
Quote:
Original post by SiCrane
Quote:
Original post by MichaBen
That depends on the IDE/linker you are using. Some have a simply drop down list where you can pick "Console" or "GUI".

If all you do is change the subsystem to Windows your program will fail to link, since it will search for WinMain() and not main().

You can override that with /ENTRY, though.

Share this post


Link to post
Share on other sites
SiCrane    11839
Which you need to be careful with, since the obvious choice of argument, main(), is not the correct one.

Share this post


Link to post
Share on other sites
MichaBen    481
Quote:
Original post by SiCrane
If all you do is change the subsystem to Windows your program will fail to link, since it will search for WinMain() and not main().


I can link Windows programs with main() only perfectly without need to override anything, just as you can link WinMain() programs with a console window. But as I already said, it might depend on the IDE/compiler/linker you are using. But obviously, if you are programming for Windows, you should use WinMain, if you are using both console and GUI window I also use WinMain. For cross-platform code, just use #ifdef to pick the correct main function (and later to pass the correct variables to the GUI creator) or use 2 separated main functions also between #ifdef blocks.

#ifdef _WIN32_
int APIENTRY WinMain( HINSTANCE inst, HINSTANCE prevInst, LPSTR cmdLine, int show ) {
#else
int main( int argc, char *argv[] ) {
#endif

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