Program entry point question

A

Angus

If I want to write a text based C program I use int main etc as my
program entry point. But if I want to write for aexample a GUI
application for some platform then I would use a different entry point
- eg WinMain for MS Windows.

I had a look in crt0.c file included with my compiler and the code
calls mainCRTStartup or WinMainCRTStartup dependent on some defines.
Presumably This is all controlled by defines you setup in your
project?

But why can we not use main for these 'other' program types?
 
N

Nick Keighley

If I want to write a text based C program I use int main etc as my
program entry point.  But if I want to write for aexample a GUI
application for some platform then I would use a different entry point
- eg WinMain for MS Windows.

I had a look in crt0.c file included with my compiler and the code
calls mainCRTStartup or WinMainCRTStartup dependent on some defines.
Presumably  This is all controlled by defines you setup in your
project?

But why can we not use main for these 'other' program types?

ask Microsoft! I'm afraid the details of this are all very OS
specific. You nedd to ask your question on a Microsoft programming
group. I've found this one helpful

comp.os.ms-windows.programmer.win32

and check out MSDN
 
B

bartc

Angus said:
If I want to write a text based C program I use int main etc as my
program entry point. But if I want to write for aexample a GUI
application for some platform then I would use a different entry point
- eg WinMain for MS Windows.

I had a look in crt0.c file included with my compiler and the code
calls mainCRTStartup or WinMainCRTStartup dependent on some defines.
Presumably This is all controlled by defines you setup in your
project?

But why can we not use main for these 'other' program types?

For Win32, you have a choice of WinMain() or main(). This can be for console
or GUI programs.
 
K

Keith Thompson

Angus said:
If I want to write a text based C program I use int main etc as my
program entry point. But if I want to write for aexample a GUI
application for some platform then I would use a different entry point
- eg WinMain for MS Windows.

I had a look in crt0.c file included with my compiler and the code
calls mainCRTStartup or WinMainCRTStartup dependent on some defines.
Presumably This is all controlled by defines you setup in your
project?

But why can we not use main for these 'other' program types?

A quick Google search for "WinMain" shows that it has a substantially
different set of parameters than the C standard main() function.
If you're interested in the details, the first Google hit for
WinMain has what appears to be a good description.

To use main() for such programs, the system would have to provide
another mechanism for obtaining that information. Microsoft apparently
decided it would be more convenient to provide a different entry point.

Other systems may have similar considerations. (Unix-like systems
usually don't, because C and Unix were developed together.)
 
R

Rui Maciel

Angus said:
If I want to write a text based C program I use int main etc as my
program entry point. But if I want to write for aexample a GUI
application for some platform then I would use a different entry point
- eg WinMain for MS Windows.

The reason for that is that MS's APIs are poorly designed, in order to lead the clueless
newbie to believe that writing windows apps is some sort of strangely magical act.


But why can we not use main for these 'other' program types?

But we can and we do. For example, let's take Qt. It's an API which is well designed and
puts in shame any of MS's attempts at a GUI API. Yet, it relies on a main() function which
may be similar to the following (incomplete) code:

<code>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow mainWin;
mainWin.show();

return app.exec();
}
</code>
 
U

Uno

Keith said:
A quick Google search for "WinMain" shows that it has a substantially
different set of parameters than the C standard main() function.
If you're interested in the details, the first Google hit for
WinMain has what appears to be a good description.

To use main() for such programs, the system would have to provide
another mechanism for obtaining that information. Microsoft apparently
decided it would be more convenient to provide a different entry point.

Other systems may have similar considerations. (Unix-like systems
usually don't, because C and Unix were developed together.)

Using proper MS tools, the entry point is trivial, literally.

I spent the first 15 years on the MS bandwagon, and I think open source
is the new juggernaut.

C pretends not to know anything about filehandles, so the WinMain call
is outside C's purview. When I first questioned Keith along these
lines, I was amazed to find out how much my MSVC4++ resembled his ISO C.

Cheers,
 
S

spinoza1111

Using proper MS tools, the entry point is trivial, literally.

I spent the first 15 years on the MS bandwagon, and I think open source
is the new juggernaut.

You got that right. Free stuff made by willing, happy slaves fo de
corporation, wif de uppity slaves who talk back "banned"? Sho' nuff,
boss. The ruling class did NOT intend to create a class of highly
educated programmers able to master complex systems and get paid,
since such men and women would then turn around and start questioning
multinational capitalism. Even Bill Gates wised up.

Therefore, the level of programming skill today (as seen in this
newsgroup, especially in Peter Seebach's Coding Horrors) is abominable
by design.
C pretends not to know anything about filehandles, so the WinMain call
is outside C's purview.  When I first questioned Keith along these
lines, I was amazed to find out how much my MSVC4++ resembled his ISO C.

As Dave Hansen told me at Princeton, Microsoft and IBM programmers can
be just as smart as programmers in the DEC/unix/Linux tradition. The
regs here think they're slick because they use Linux.
 
P

Phil Carmody

Uno said:
Keith Thompson wrote:
[SNIP - stuff about the GUI program entry point in windows]
Using proper MS tools, the entry point is trivial, literally.

Using proper Unix tools, the entry point is trivial too. Trivial,
and the same as the entry point if you were not writing a GUI
program. I think that makes it even more trivial, personally.
I spent the first 15 years on the MS bandwagon, and I think open
source is the new juggernaut.

Nope, MS is still the lumbering smelly thing.
C pretends not to know anything about filehandles, so the WinMain call
is outside C's purview.

MSVC knows nothing about 'filehandles' either. If you meant 'file handles',
then taking 'thing handle' as the term for an opaque type which carries
enough information for another agent to operate on a unique object of
type 'thing', then C knows as much about file handles as any other
language, and represents them with the C type FILE*.

Which has anything to do with WinMain, so I can't see any logic in
your sentence at all.

WinMain is outside C's purview precisely because it's not within
C's purview. Pretending that it's because of some single arbitrary
property completely unrelated to WinMain's definition is a wild
misdirection.
When I first questioned Keith along these
lines, I was amazed to find out how much my MSVC4++ resembled his ISO
C.

I'm not sure you questioned Keith cogently, and likewise doubt that
you'd have understood his response, given the above.

Phil
 
N

Nick Keighley

I spent the first 15 years on the MS bandwagon, and I think open source
is the new juggernaut.

rolls over people and crushes them to death?

C pretends not to know anything about filehandles, so the WinMain call
is outside C's purview.

duh? What have filehandles got to do with WinMain()?

MSDN:
int WINAPI WinMain(
__in HINSTANCE hInstance,
__in HINSTANCE hPrevInstance,
__in LPSTR lpCmdLine,
__in int nCmdShow
);
 When I first questioned Keith along these
lines, I was amazed to find out how much my MSVC4++ resembled his ISO C.

it's not "his ISO C" It's ISO's. Microsoft are actually quite
conforming (to an older standard) if you press the right buttons.
 
J

jacob navia

Angus a écrit :
If I want to write a text based C program I use int main etc as my
program entry point. But if I want to write for aexample a GUI
application for some platform then I would use a different entry point
- eg WinMain for MS Windows.

I had a look in crt0.c file included with my compiler and the code
calls mainCRTStartup or WinMainCRTStartup dependent on some defines.
Presumably This is all controlled by defines you setup in your
project?

But why can we not use main for these 'other' program types?

You can use main() for GUI programs, and Microsoft has explicitely mentioned this fact since at
least windows 95, when win32 appeared. The only difference in a GUI program and a console program is
a bit set or unset in the executable that tells the program loader to open a console for it or not.

All the answers you got in this goup are wrong, and do not consider this fact either because the
people answering here are ignorants of Microsoft windows conventions, or because they do not like
Microsoft, that they consider the evil empire.

This bit in the executable will be set by your LINKER, not by a "define" or whatever in the crt0
code. The linker accepts a command line option to set or not this bit. Read the documentation of
your linker and you will find it.

All the console programs have full access to ALL GUI functions: they can open windows, close
windows, use sound, display video, you name it. All GUI programs can use a console by forcing the
system to open one with AllocConsole().
 
J

jacob navia

Keith Thompson a écrit :
A quick Google search for "WinMain" shows that it has a substantially
different set of parameters than the C standard main() function.
If you're interested in the details, the first Google hit for
WinMain has what appears to be a good description.

To use main() for such programs, the system would have to provide
another mechanism for obtaining that information. Microsoft apparently
decided it would be more convenient to provide a different entry point.


This is wrong. Completely wrong since 1995. It has been 15 years that Microsoft
explained that.

You can use a program with an entry point like this:

int main(void)

and open windows, display video, and access ALL GUI functions.

You can use a program with WinMain to open a console, use printf, etc.

There is no differences in the capaibilities of either since win32 appeared,
in 1995. WinMain was retained for compatibility reasons with older (16 bit) windows
programs
 
J

jacob navia

Rui Maciel a écrit :
The reason for that is that MS's APIs are poorly designed, in order to lead the clueless
newbie to believe that writing windows apps is some sort of strangely magical act.

You are spreading misinformation. The documentation of Microsoft makes it explicit since 1995 that
there is no difference between a console program and a GUI program. Both can open windows, display
video, whatever.

If you do not like microsoft it is your right. Spreading misinformation is another matter.

http://blogs.msdn.com/oldnewthing/archive/2006/12/04/1205831.aspx
 
K

Keith Thompson

jacob navia said:
Keith Thompson a écrit :


This is wrong. Completely wrong since 1995. It has been 15 years that
Microsoft explained that.

You can use a program with an entry point like this:

int main(void)

and open windows, display video, and access ALL GUI functions.

You can use a program with WinMain to open a console, use printf, etc.

There is no differences in the capaibilities of either since win32
appeared, in 1995. WinMain was retained for compatibility reasons with
older (16 bit) windows programs

I'm glad to hear it. My answer was incomplete, since I didn't know
about this additional information. But other than saying that the
system "would have to provide another mechanism" when it has already
done so, I fail to see what was wrong, much less "Completely wrong",
about my response.

Microsoft *does* provide an entry point other than the C
standard main(). As far as I can tell (and please correct me if
I'm mistaken), they seem to encourage the use of the WinMain()
entry point for GUI programs, even though they also allow the
use of main(). Presumably it can be more convenient to use
the WinMain() entry point, since it allows for easier access to
certain information. There's nothing wrong with that; it's simply
an extension as explictly permitted by the C standard (C99 5.1.2.2.1
doesn't permit it, but 4p6 does).

Read my post again, pretending it was written by someone you respect
rather than by me. Is it still "completely wrong"? If there are
errors I don't know about, I'd like to know about them.

And didn't you recently say that you never answer my posts?
 
S

sandeep

jacob said:
Angus a écrit :

You can use main() for GUI programs, and Microsoft has explicitely
mentioned this fact since at least windows 95, when win32 appeared. The
only difference in a GUI program and a console program is a bit set or
unset in the executable that tells the program loader to open a console
for it or not.

All the answers you got in this goup are wrong, and do not consider this
fact either because the people answering here are ignorants of Microsoft
windows conventions, or because they do not like Microsoft, that they
consider the evil empire.

This bit in the executable will be set by your LINKER, not by a "define"
or whatever in the crt0 code. The linker accepts a command line option
to set or not this bit. Read the documentation of your linker and you
will find it.

Hello Jacob ~

It is a few years since I programmed for Windows but I think you are
mistaken about this.

If you want to use windows, graphics, sound etc. you MUST make a GUI
program, you CANNOT do this from a console program! And in a GUI program
you need to use WinMain() not main() so that your program has registered
hWnds etc.

Regards ~
 
B

bartc

sandeep said:
jacob navia writes:
It is a few years since I programmed for Windows but I think you are
mistaken about this.

If you want to use windows, graphics, sound etc. you MUST make a GUI
program, you CANNOT do this from a console program! And in a GUI program
you need to use WinMain() not main() so that your program has registered
hWnds etc.

#include <windows.h>

int main(void)
{
MessageBoxA(0,"Hello World!","Hello",0);
}
 
S

sandeep

bartc said:
#include <windows.h>

int main(void)
{
MessageBoxA(0,"Hello World!","Hello",0); }

Hello Bart ~

I am sure this will NOT work - definitely not on some older versions of
Windows, I don't know about Vista etc.

Regards ~
 
J

jacob navia

sandeep a écrit :
Hello Bart ~

I am sure this will NOT work - definitely not on some older versions of
Windows, I don't know about Vista etc.

Regards ~

This has worked since windows 95. 15 years ago.

That did not work in windows 3.1 (16 bits), 19 years ago.
 
B

Bill Reid

I am sure this will NOT work - definitely not on some older versions of
Windows, I don't know about Vista etc.
Well, you may be sure, but you are very wrong.
I just compiled this very program on an old
Windows 98 system and it basically works as
stupidly advertised (pops up an empty "console"
along with a Windows message box saying "Hello
World!", both remain until the message box
is dismissed)...stupid and pointless except
for proving you technically incompetent...

I DID get two warnings: one about "pre-compiled
code in header" which is specific to a "feature"
of my IDE, and of course, "function should return
a value" (this of course is only there to make
the weenies who only know the return value of
main() feel more manly).
 
M

Mark Hobley

Angus said:
If I want to write a text based C program I use int main etc as my
program entry point. But if I want to write for aexample a GUI
application for some platform then I would use a different entry point
- eg WinMain for MS Windows.

This provides legacy support for the "This program requires Microsoft Windows"
message on an MSDOS operating system.

Entry via main can display "This program requires Microsoft Windows" or take
some other action.

Running from Microsoft Windows, the entry point is WinMain and a different
action is taken. (Obviously we do not want the error message if the program
is already running in Microsoft Windows).

Mark.
 
N

Nick Keighley

Rui Maciel a écrit :



You are spreading misinformation. The documentation of Microsoft makes it explicit since 1995 that
there is no difference between a console program and a GUI program. Both can open windows,
display video, whatever.

If you do not like microsoft it is your right. Spreading misinformation is another matter.

http://blogs.msdn.com/oldnewthing/archive/2006/12/04/1205831.aspx

thanks for the link! Though I'd still MS don't exactly shount about
this. For instance even link you gave I had to read about halfway down
before I found how to do it. Mr Chen still seemed to consider WinMain
to be the entry point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top