Get MinGW Cross Compiler at SourceForge.net. Fast, secure and Free Open Source software downloads

Hello World program using Win32API


Simple "Hello World" Progam

Here is a very simple Win32 program using Win32API directly.

List : hello.c

#include <windows.h> 

int WINAPI 
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInst, LPTSTR lpCmdLine, int nShowCmd) 
{ 
  MessageBoxW (NULL, L"Hello World!", L"hello", MB_OK | MB_ICONINFORMATION); 
  return 0; 
}

To build Win32 GUI application, -mwindows switch is added in the link. If -mwindows is not added, -mconsole is automatically added. If -mconsole is added for building GUI application, the window of command prompt is appeared at the same time as GUI application when you invoke the program on Windows.

Bulding hello.exe

$ i686-pc-mingw32-gcc hello.c -o hello.exe -mwindows
$ file hello.exe
hello.exe: PE32 executable for MS Windows (GUI) Intel 80386 32-bit
$ ./hello.exe &
 

figure : hello.exe executed on Wine

If you want to check in detail how the gcc builds your program, try to add -v option to gcc command.

Bulding hello.exe with -v option

$ i686-pc-mingw32-gcc hello.c -o hello.exe -mwindows -v
Using built-in specs.
Target: i686-pc-mingw32
Configured with: ../configure --prefix=/usr --bindir=/usr/bin --includedir=/usr
/include --libdir=/usr/lib64 --mandir=/usr/share/man --infodir=/usr/share/info 
--datadir=/usr/share --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux
-gnu --target=i686-pc-mingw32 --with-gnu-as --with-gnu-ld --verbose --without-n
ewlib --disable-multilib --with-system-zlib --disable-nls --without-included-ge
ttext --disable-win32-registry --enable-version-specific-runtime-libs --with-sy
sroot=/usr/i686-pc-mingw32/sys-root --enable-languages=c,c++
Thread model: win32
gcc version 4.3.2 (GCC) 
COLLECT_GCC_OPTIONS='-o' 'hello.exe' '-mwindows' '-v' '-mtune=generic'
 /usr/libexec/gcc/i686-pc-mingw32/4.3.2/cc1 -quiet -v hello.c -quiet -dumpbase 
hello.c -mwindows -mtune=generic -auxbase hello -version -o /tmp/ccmjS7aI.s
ignoring nonexistent directory "/usr/i686-pc-mingw32/sys-root/usr/local/include
"
ignoring nonexistent directory "/usr/lib64/gcc/i686-pc-mingw32/4.3.2/../../../.
./i686-pc-mingw32/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib64/gcc/i686-pc-mingw32/4.3.2/include
 /usr/lib64/gcc/i686-pc-mingw32/4.3.2/include-fixed
 /usr/i686-pc-mingw32/sys-root/mingw/include
End of search list.
GNU C (GCC) version 4.3.2 (i686-pc-mingw32)
	compiled by GNU C version 4.3.2 20081105 (Red Hat 4.3.2-7), GMP version
 4.2.2, MPFR version 2.3.2.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: fed0246486d20b4653fc64976dff8eaa
COLLECT_GCC_OPTIONS='-o' 'hello.exe' '-mwindows' '-v' '-mtune=generic'
 /usr/lib64/gcc/i686-pc-mingw32/4.3.2/../../../../i686-pc-mingw32/bin/as -v -o 
/tmp/ccIbBD0E.o /tmp/ccmjS7aI.s
GNU assembler version 2.19 (i686-pc-mingw32) using BFD version (GNU Binutils) 2
.19
COMPILER_PATH=/usr/libexec/gcc/i686-pc-mingw32/4.3.2/:/usr/libexec/gcc/i686-pc-
mingw32/4.3.2/:/usr/libexec/gcc/i686-pc-mingw32/:/usr/lib64/gcc/i686-pc-mingw32
/4.3.2/:/usr/lib64/gcc/i686-pc-mingw32/:/usr/lib64/gcc/i686-pc-mingw32/4.3.2/..
/../../../i686-pc-mingw32/bin/
LIBRARY_PATH=/usr/lib64/gcc/i686-pc-mingw32/4.3.2/:/usr/lib64/gcc/i686-pc-mingw
32/4.3.2/../../../../i686-pc-mingw32/lib/:/usr/i686-pc-mingw32/sys-root/mingw/l
ib/
COLLECT_GCC_OPTIONS='-o' 'hello.exe' '-mwindows' '-v' '-mtune=generic'
 /usr/libexec/gcc/i686-pc-mingw32/4.3.2/collect2 --sysroot=/usr/i686-pc-mingw32
/sys-root --subsystem windows -Bdynamic -o hello.exe /usr/i686-pc-mingw32/sys-r
oot/mingw/lib/crt2.o /usr/lib64/gcc/i686-pc-mingw32/4.3.2/crtbegin.o -L/usr/lib
64/gcc/i686-pc-mingw32/4.3.2 -L/usr/lib64/gcc/i686-pc-mingw32/4.3.2/../../../..
/i686-pc-mingw32/lib -L/usr/i686-pc-mingw32/sys-root/mingw/lib /tmp/ccIbBD0E.o 
-lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt -lgdi32 -lcomdlg32 -luser32 -lker
nel32 -ladvapi32 -lshell32 -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt /usr/l
ib64/gcc/i686-pc-mingw32/4.3.2/crtend.o

Related Links

  1. MSDN/MSDN Library/Win32 and COM Development/User Interface/Windows User Interface/Windowing/Windows/Windows Reference/Functions