WinMain vs Main

Started by
14 comments, last by MichaBen 14 years, 10 months ago
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?
Advertisement
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!

-Lead developer for OutpostHD

http://www.lairworks.com

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
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.
Someone who uses a, euhm..., delta!?
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
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.

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?
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.
Kippesoep
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.
Someone who uses a, euhm..., delta!?
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.

[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe

This topic is closed to new replies.

Advertisement