WinMain()

J

Jrdman

i wrote this code :
#include<windows.h>
int APIENTRY WinMain(HINSTANCE hinstane,
HINSTANCE hpinstance,
LPSTR lpcmdline,
int cmdshow){

while(1)
;

}
and when i excute it,it gives me a console window like the ordinary
one that we get when we use main().so what's the difference between
main() and WinMain() in ths case?
 
J

Jens Thoms Toerring

Jrdman said:
i wrote this code :
#include<windows.h>
int APIENTRY WinMain(HINSTANCE hinstane,
HINSTANCE hpinstance,
LPSTR lpcmdline,
int cmdshow){

}
and when i excute it,it gives me a console window like the ordinary
one that we get when we use main().so what's the difference between
main() and WinMain() in ths case?

The difference is that main() is the start of a standard compliant
C program, understood by all C compilers, while the above is some
"dialect" of C that may be understood by (some?) compilers on Win-
dows machines but hardly anywhere else. So, unless you have some
specific needs that require the use of this "dialect", better stick
with main() (and also avoid <windows.h> which also is only available
on Windows machines).
Regards, Jens
 
C

Chris Dollin

Jrdman said:
i wrote this code :
#include<windows.h>
int APIENTRY WinMain(HINSTANCE hinstane,
HINSTANCE hpinstance,
LPSTR lpcmdline,
int cmdshow){

while(1)
;

}
and when i excute it,it gives me a console window like the ordinary
one that we get when we use main().so what's the difference between
main() and WinMain() in ths case?

One of them isn't required to work (by the C standard), and the other is
(on a conforming implementation).
 
J

jacob navia

Jrdman said:
i wrote this code :
#include<windows.h>
int APIENTRY WinMain(HINSTANCE hinstane,
HINSTANCE hpinstance,
LPSTR lpcmdline,
int cmdshow){

while(1)
;

}
and when i excute it,it gives me a console window like the ordinary
one that we get when we use main().so what's the difference between
main() and WinMain() in ths case?

To avoid the console window you have to tell the linker
that your program is a windows application. How do you do
this is specific to the compiler system. If you use
lcc-win, for instance, you should add
lc myprog.c -subsystem windows

or

lcc -c myprog.c
lcclnk -subsystem windows myprog.obj
 
J

Jrdman

To avoid the console window you have to tell the linker
that your program is a windows application. How do you do
this is specific to the compiler system. If you use
lcc-win, for instance, you should add
lc myprog.c -subsystem windows

or

lcc -c myprog.c
lcclnk -subsystem windows myprog.obj

how to avoid the console window with the compiler MinGW of Dev-C++?
 
C

CBFalconer

Richard said:
Chris Dollin said:

The C Standard /does/ require the WinMain code to work under
freestanding implementations (such as, for example, Win32
compilers) that allow that code as a legal entry point syntax for
a C program. See C89 2.1.2.1:

While your statement is perfectly true, bringing it up here simply
obscures the important points made by other replies.
 
H

Harald van Dijk

Chris Dollin said:

The C Standard /does/ require the WinMain code to work under
freestanding implementations (such as, for example, Win32 compilers)
that allow that code as a legal entry point syntax for a C program. See
C89 2.1.2.1:

I'm curious, does that apply even if you need to use a non-standard header
(or similarly, identifiers such as __stdcall) which would otherwise render
the code undefined? If an implementation defines WinMain with the given
declaration as the correct entry point, must this program be accepted?

#include<windows.h>
int APIENTRY WinMain(HINSTANCE hinstane,
HINSTANCE hpinstance,
LPSTR lpcmdline,
int cmdshow){
int APIENTRY = 0;
return APIENTRY;
}

I assume non-standard headers are permitted to define non-reserved
identifiers as macros, so the implementation may legitimately reject my
code. If so, who's to say in the original example that <windows.h> doesn't
define int, hinstance, hpinstance, lpcmdline, cmdshow, or while as a macro?
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top