|
-
September 5th, 2005, 03:32 AM
#1
What's the point in WinMain?
Hi, ok i get the point when your making a gui, but apart from that, why don't people just use the no-thrills int main() ?
-
September 5th, 2005, 03:52 AM
#2
Re: What's the point in WinMain?
The reason is that int main() is for console applications. If you want to make an application with neither a GUI nor console output, you would use WinMain() and simply don't create a window. If you used a console app (int main) a blank console window would show up automatically. That's not what you would want, is it?
Please don't forget to rate users who helped you!
-
September 5th, 2005, 04:12 AM
#3
Re: What's the point in WinMain?
you can usually set it to invisible under the compiler options..
-
September 5th, 2005, 04:19 AM
#4
Re: What's the point in WinMain?
But it will still be there and consume cpu and memory, though not very much I have to admit.
Asked the other way:
What is the point with int main()? OK, I get the point when making a console application. But please don't tell me that "int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nShowCmd)" requires more typing than "int main(int argc, char[] argv)". That does not count as a reason for me.
Please don't forget to rate users who helped you!
-
September 5th, 2005, 04:26 AM
#5
Re: What's the point in WinMain?
I was under the impression you had to initiallise loads of other stuff to use with it like, GetMessage loop, and the callback etc
-
September 5th, 2005, 04:34 AM
#6
Re: What's the point in WinMain?
 Originally Posted by dave2k
I was under the impression you had to initiallise loads of other stuff to use with it like, GetMessage loop, and the callback etc
Ah, I see. You only have to use that stuff, if you want to make a window (for a GUI). I'll demonstrate that it is not needed in other cases:
Hello World application as Win32-app
Code:
#include <windows.h>
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nShowCmd)
{
MessageBox(NULL, "Hello World", "Hello World app", MB_OK);
return 0;
}
Hello World application as Win32-console app
Code:
#include <windows.h>
int main(int argc, char[] argv)
{
MessageBox(NULL, "Hello World", "Hello World app", MB_OK);
return 0;
}
Please don't forget to rate users who helped you!
-
September 5th, 2005, 04:54 AM
#7
Re: What's the point in WinMain?
cheers for clearing that up
-
September 5th, 2005, 05:47 AM
#8
Re: What's the point in WinMain?
Just an addition... never... never ever use an calling convention directly under Windows programming. There are Windows macros wrapping up the calling convention for specific needs. So the Winmain method should have WINAPI instead, because this macro might change internaly in the future, which will lead to an incompatibilty of those who use the calling convention correctly.
Code:
int WINAPI WinMain ( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int ShowCmd )
{
return 0;
}
-
September 5th, 2005, 06:49 AM
#9
Re: What's the point in WinMain?
ah! I was guided incorrectly! bah.
-
September 5th, 2005, 07:02 AM
#10
Re: What's the point in WinMain?
 Originally Posted by dave2k
ah! I was guided incorrectly! bah.
You should have read NoHero's post better.
Using __stdcall is not incorrect.
It may become incorrect in future versions of windows (vista?). Who knows? If you want to be on the safe side use WINAPI like NoHero suggested.
Please don't forget to rate users who helped you!
-
September 5th, 2005, 07:19 AM
#11
Re: What's the point in WinMain?
-
September 5th, 2005, 12:09 PM
#12
Re: What's the point in WinMain?
 Originally Posted by philkr
You should have read NoHero's post better.
Using __stdcall is not incorrect.
It may become incorrect in future versions of windows (vista?). Who knows? If you want to be on the safe side use WINAPI like NoHero suggested.
That was the same with the FORTRAN and PASCAL calling convention... Things can change and it's always better to be on the safe site. Of course it will work, but how long? . It's not only a fact of style or personal preferences. 
BTW: You use MB_OK too instead of it's numerical value (0), this is exactly the same. And are you doing... A question of readability. That's exactly the same with WINAPI. If a function is marked __stdcall do the most people - who read your code - know that your function is WinAPI compatible? But will know it for sure when you use WINAPI instead.
Last edited by NoHero; September 5th, 2005 at 12:13 PM.
-
September 6th, 2005, 04:58 AM
#13
Re: What's the point in WinMain?
 Originally Posted by NoHero
BTW: You use MB_OK too instead of it's numerical value (0), this is exactly the same. And are you doing... A question of readability. That's exactly the same with WINAPI. If a function is marked __stdcall do the most people - who read your code - know that your function is WinAPI compatible? But will know it for sure when you use WINAPI instead.
Of course, you are right. A long time I didn't know what this WINAPI means until I looked it up in the header file and found out it is __stdcall. My failure was that I never thought that it might be changed some day.
I just think it is not good sometimes when you simply have #define replacements which don't show what they really are. I mean, it should have been WINAPICALLCONV or something else, but probably that would have been to long.
Please don't forget to rate users who helped you!
-
September 8th, 2005, 05:29 AM
#14
Re: What's the point in WinMain?
Actually neither 'WinMain' nor 'main' are entry-points into any Windows application. I dont seem to recall the name of the entry point symbol, but you can override it, if you want to jump start a smaller executable and initialise everything yourself (the STL, STD libraries, call constructors for global objects etc). I wouldnt recommend it though, first because under-the-hood system always changes, and because the 'WinMain' and 'main' wrap a hardware dependent functionality, like calling constructors on a x86 system is not the same as calling constructors on a AMD64 system, even though both can run Windows programs.
There are people providing their own startup code though, like TINYLIB thing from some Microsoft guy, who desperately needed to minimize the size of the executable.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|