Archived

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

Densun

Replacing main()

Recommended Posts

Cyberdrek    100
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
Headhunter Soft
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Arild Fines    968
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 this post


Link to post
Share on other sites
coderx75    435
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 this post


Link to post
Share on other sites
Arild Fines    968
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 this post


Link to post
Share on other sites
coderx75    435
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Dean Harding    546
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 this post


Link to post
Share on other sites
Arild Fines    968
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Oluseyi    2116
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 this post


Link to post
Share on other sites
Kylotan    10013
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.

Share this post


Link to post
Share on other sites