• Announcements

Archived

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

Replacing main()

Recommended Posts

Is it syntactically legal for Microsoft to have replaced main() with WinMain() when programming with Win32 API?

Share on other sites
quote:
Original post by Densun
Is it syntactically legal for Microsoft to have replaced main() with WinMain() when programming with Win32 API?

I can''t say if it is legal or not but that''s the way it works, deal with it..

"And that''s the bottom line cause I said so!"

** I WANT TO BE THE MODERATOR FOR THE LINUX FORUM **

Cyberdrek
A division of DLC Multimedia

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer
/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com

Share on other sites
Well, given than _start() calls main() and that main() calls WinMain(), yes; it is.

I''m not sure I understand the question, actually. It''s syntaxically legal to call your API''s main entry point whatever you want. You don''t get to see main(), but it doesn''t mean it''s not there.

Now I will tell you than I really dislike the WinMain() thingie, but that''s probably only ''cause I''m picky.

Anyway, I don''t code for windows so...

Share on other sites
AP: No, it doesnt work that way. When a console program starts, windows calls mainCRTstartup(), which in turn calls main(). For GUI programs, its Windows->WinMainCRTStartup->WinMain.
It is also perfectly legal for Microsoft to do this. From the draft standard, article 3.6.1.1:
quote:

A program shall contain a global function called main, which is the designated start of the program. It is
implementationdefined
whether a program in a freestanding environment is required to define a main
function.

Fantastic doctrines (like Christianity or Islam or Marxism or Microsoft-bashing) require unanimity of belief. One dissenter casts doubt on the creed of millions. Thus the fear and hate; thus the torture chamber, the iron stake, the gallows, the labor camp, the psychiatric ward - Edward Abbey

Share on other sites
I often wonder about this myself... C and C++ were meant to be compiled on multiple platforms without changing code... unless you use assembly, of course. But as long as you stick to the standard libraries, it should always work. Once something is written in Visual C++ for Win32, you can''t compile on another platform without making changes. I always thought it would make more sense to stick with standard C++ and access the GUI through a library (which IS the case but then why WinMain?)

I have a lot of experience working with Windows and I understand how it works and why. It is efficient for windows (and MS biz strategy) but it''s not efficient in a global sense.

Overall tho, what''s the big deal? Using Windows, you''re already tied into a GUI. But it''d be cool if there was a Windows GUI for Unix that you could tie into without having to change any code.

Many of the truths we cling to depend greatly on our own point of view

Get Tranced!

Share on other sites
What you want then is the java Swing API.

ECKILLER

Share on other sites
quote:
Original post by coderx75
Once something is written in Visual C++ for Win32, you can''t compile on another platform without making changes.

Only if you use VC++ to write Win32-specific code.

quote:

I always thought it would make more sense to stick with standard C++ and access the GUI through a library (which IS the case but then why WinMain?)

If you are writing Windows apps using the Win32 API, you are already writing non-portable code. main/WinMain is pretty irrelevant at this point.

quote:

Overall tho, what''s the big deal? Using Windows, you''re already tied into a GUI. But it''d be cool if there was a Windows GUI for Unix that you could tie into without having to change any code.

There are heaps of cross-platform GUI libraries around. Take a look at QT or wxWindows.

Fantastic doctrines (like Christianity or Islam or Marxism or Microsoft-bashing) require unanimity of belief. One dissenter casts doubt on the creed of millions. Thus the fear and hate; thus the torture chamber, the iron stake, the gallows, the labor camp, the psychiatric ward - Edward Abbey

Share on other sites
Sure, but my point and I would guess Densun''s point is why change the main() function? Obviously, so that MS can tie people into their compiler. But to answer Densun''s question, I would say no, it is not correct.

Many of the truths we cling to depend greatly on our own point of view

Get Tranced!

Share on other sites
quote:
Original post by Arild Fines
AP: No, it doesnt work that way. When a console program starts, windows calls mainCRTstartup(), which in turn calls main(). For GUI programs, its Windows->WinMainCRTStartup->WinMain.
It is also perfectly legal for Microsoft to do this. From the draft standard, article 3.6.1.1:
[quote]
A program shall contain a global function called main, which is the designated start of the program. It is
implementationdefined
whether a program in a freestanding environment is required to define a main
function.

Wow it is even less standard than I thought.

Share on other sites
You can write windows apps starting with the main() function. Just compile with /subsystem:console and away you go! A console application can still access all the other Windows APIs, create windows, whatever - it''ll just have a console as well.

WinMain was more useful back in the days of Windows 3.1 where the second HINSTANCE parameter was actually used (it used to be the HINSTANCE of a previously running instance of your application) but it''s use has since been deprecated in newer windows (since there''s better ways to detect if your application is already running).

If you like the argc/argv parameters of the main() function, you can still access them through the __argc and __argv global variables, since they''re calculated before it decides to call either main or WinMain.

codeka.com - Just click it.

Share on other sites
Oh yeah, and if you think it''s bad they replaced main() with WinMain(), what about MFC which doesn''t have a "main" function at all!

codeka.com - Just click it.

Share on other sites
Grrrr...didnt anyone read that passage from the standard I quoted? Its perfectly legal for a ''freestanding environment'' to define its own entry point.
And I think the suggestion that this is some sort of attempt to tie people into using Microsoft technology is just silly. The moment you start writing code that requires a WinMain, you are already writing platform dependent code. You cannot get Win32-specific code to compile under any other platform anyway, WinMain or no WinMain.

Fantastic doctrines (like Christianity or Islam or Marxism or Microsoft-bashing) require unanimity of belief. One dissenter casts doubt on the creed of millions. Thus the fear and hate; thus the torture chamber, the iron stake, the gallows, the labor camp, the psychiatric ward - Edward Abbey

Share on other sites
quote:
what about MFC which doesn''t have a "main" function at all!

Yes it does it has two. A WinMain() that calls AfxWinMain(). Try running a MFC app through a debugger.

Share on other sites
quote:
Original post by coderx75
Sure, but my point and I would guess Densun''s point is why change the main() function? Obviously, so that MS can tie people into their compiler.

*Ahem*

Their compiler? That would seem to suggest that a Windows application written to use WinMain would not compile under any other compiler - clearly false given the abundance of available Win32 development environments.

The question is moot, the discussion is moot. The more interesting question is why it irks you.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share on other sites
Regardless of whether MS wants to ''lock you in'' to using their compiler, WinMain has the following signature:

int WINAPI WinMain(  HINSTANCE hInstance,  // handle to current instance  HINSTANCE hPrevInstance,  // handle to previous instance  LPSTR lpCmdLine,      // pointer to command line  int nCmdShow          // show state of window);

Which obviously has different requirements to main(). So simply using ''main'' would not be easy.

• Forum Statistics

• Total Topics
628291
• Total Posts
2981862

• 11
• 10
• 10
• 11
• 17