Advertisement Jump to content


This topic is now archived and is closed to further replies.


WinMain(...) / main()

This topic is 6551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is something I've never had adequately explained: As you all know, every Win32 windowing application uses WinMain(...) as the entry point, while every other application (including Win32 console) uses main(). What I'm wondering is: How is this done? Does the linker link your application to an external library which has main() defined somewhere, so that when executing the calling order will be like this: (not sure about capitalization here...)_crtstartup()->main()->_WinCRTStartup(...)->WinMain(...) Or, is the whole WinMain thing just some handwaving on the part of the compiler, so that it accepts a program without main() defined? Apparently the C++ standard states(I don't have a copy) that every C++ program should have a main() function defined, except in certain freestanding environments. Is Win32 a freestanding environment? (What is a freestanding environment anyway?) Anyone care to enlighten me? Edited by - Eraserhead on February 12, 2001 10:34:52 AM

Share this post

Link to post
Share on other sites
You''re basically right. If you check your docs for the /entry switch on the linker you can see that you can change the entry point to be whatever you want.

After the OS loads your program it jumps to the routine specified by /entry. The routine is prototype thusly: "void foo(void)". When you''re using MSVC there is a function in the C runtime called "mainCRTStartup" (or "WinMainCRTStartup") that initializes the C/C++ stuff and then jumps to main/WinMain with the appropriate parameters.

It all comes down to the fact that your program needs it''s environment initialized before it can even start running. Hence "main" is just the entry point for the user''s code, not necessarily the actual program. Other OS''s have similiar hoops although they may be more or less hidden.

WinMain probably exists (instead of just main) because 16-bit windows didn''t go through as many hoops to make things as transparent.


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!