WinMain(...) / main()
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
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.
-Mike
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.
-Mike
-Mike
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement