tags:

views:

32

answers:

2

Hi, I have few very simple question. I searched a web for them, but I found different answers so I just want to know which to follow.

So, first, I believe WinMain is NOT C or C++ standart, but is only added by Microsoft to determine when to load different CRT startup code, am I right?

And second, is WinMain called by OS, in a way of lets say similiar to dynamic linking, or is it just program startup point like main?

Why I ask? I mainly used C for programming MCUs. I am more HW oriented than SW, so I like MCUs, I find them and programming for them more "clear".

But when I started to get interested about C language itself and its standart, I found that its very hard. I mean, for example, on MCU, you need no int return type of main, as well as in win32 app you need different startup code than pure main has.

So, I like C but I find its standart to be somehow old. Thanks.

+1  A: 

I believe WinMain is NOT C or C++ standart, but is only added by Microsoft to determine when to load different CRT startup code, am I right?

Yes. All C and C++ standards define main() (and only main()) as the program entry point (although its exact signature may vary between languages and standard versions).

And second, is WinMain called by OS, in a way of lets say similiar to dynamic linking, or is it just program startup point like main?

It is actually called from main(). There is a main() in Windows programs too, just hidden deep within WinAPI code.

Péter Török
Thanks. But how can you call your code under WinMain using main? I mean, I consider main(){} to be somewhere in WinMain code, but if it would be so, main would be called first, and ignored code before it.
B.Gen.Jack.O.Neill
@B.Gen, you don't call `main()`, the OS does (to put it simple). And you don't need to call `WinMain()`, the framework does it for you. In Windows programs, `main()` contains a lot of (boilerplate) code which does all the magic to set up a Windows app. This code is written by Microsoft. Once it is done with the initialization, it calls `WinMain()`. That's where you can enter your own code to get executed.
Péter Török
Thanks. As I said, I know that main adds CRT startup code. Difference would be that in MCU world, its about 5 instructions....
B.Gen.Jack.O.Neill
A: 

Although it's all the same, consider C as being 3 languages:

  1. Standard free standing
  2. Standard hosted implementation
  3. Extended hosted implementation

What you describe (WinMain) belongs to type 3.

Type 3 programs work on computers which describe the specific extensions they use

Type 2 has a lot of rules, but offers a guarantee that programs written in that type will work the same on every computer system with a standard C compiler (virtually every computer with a keyboard attached (including PDA, wrist watch, ..., ...)).

Type 1 is the same as type 2 minus a few of the rules and the standard library -- and it should work for every processor on Earth.

The text of the Standard is from 1999 2001 2004 2007. You can find a PDF at the ISO site ( http://www.open-std.org/jtc1/sc22/wg14/www/standards )

pmg
"programs written in that type will work the same on every computer system" - at least if they are written with portability in mind, by competent developers :-)
Péter Török