26991 total geeks with 3514 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 



User's box

Forgot password?
New account

It's Friday... That's good enough for me!
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
how things are going guys? Here... boring...
I must be going wrong on the password lengths then, as long as it was done on ECB
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate and help us fund new challenges
Due Date: Aug 31
August Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00

News Feeds
The Register
Cheapo Firefox OS
mobes to debut in
India ? definitely
not one for
Google gobbles
Fitbit whiz Gecko
Design to
strengthen those
things that don"t
shift a
Stiffed by
ransomware crims?
Try F-Secure"s
python tool
iPhone owners EARN
mobile report
FCC not quite sold
on Comcast TWC
Galileo, Galileo!
Galileo, Galileo!
Galileo fit to go.
Red Hat: ARM
servers will come
when people crank
out chips like
AMD"s 64-bit
Forrester says it"s
time to give up on
physical storage
Pedals and wheel in
that Google
robo-car or it"s
off the road ? Cali
Intel, HyTrust
geo-fence wandering
virty servers
Virtual Machine
Brings X86 Linux
Apps To ARMv7
"MythBusters" Drops
Kari Byron, Grant
Imahara, Tory
Google Wants To
Test Driverless
Cars In a
Illinois University
Restricts Access To
Social Media,
Online Political
Ask Slashdot: Where
Can I Find Good
Finding an ISIS
Training Camp Using
Google Earth
Researchers Hack
Gmail With 92
Percent Success
A Better Way To
Prosthetic Limbs
A Better Way To
Prosthetic Limbs
When Customer
Dissatisfaction Is
a Tech Business
Article viewer

How to Start Windows Programming with MinGW

Written by:xpi0t0s
Published by:Nightscript
Published on:2005-03-22 07:30:15
Search OSI about Windows.More articles by xpi0t0s.
 viewed 94315 times send this article printer friendly

Digg this!
    Rate this article :
Describes how to get started with C++ programming under Windows using Minimalist GNU for Windows (MinGW).

  • Some ability with Windows. This article does not micromanage every last tiny step of the process.
  • Some knowledge of C++ is required. Teaching C++ is outside the scope of this article.

Software Download

This seems like a reasonable place to start. Go to SourceForge and search for mingw. Probably the top result will be MinGW itself.

The reason for going via search is so that (a) you can see what sort of other stuff is available; (b) so that this document does not go out of date when SF reorganises itself. The scope of this article is just to get you started, so it won't touch on anything else.

After entering the MinGW page, go to Files. There's a lot of stuff that could be downloaded, but fortunately one of the downloads is a package of related utilities. At the time of writing the most recent is MinGW-3.1.0-1. This contains the following packages:
  • GCC-3.2.3-20030504-1.tar.gz
  • binutils-2.13.90-20030111-1
  • mingw-runtime-3.1
  • w32api-2.4
  • gdb-5.2.1-1
  • mingw32-make-3.80.0-3
  • mingw-utils-0.2.tar.gz

So we'll download this. Click the SourceForge Download MinGW-3.1.0-1.exe link, and proceed as normal through the SourceForge download process. If your browser gives you the option of running the executable, this should be ok.

At this stage I doubt that package changes will affect the contents of this article, so if there is a more recent package you'll probably be ok to download that instead. I'll also assume that we won't need to upgrade any of the packages.


Run MinGW-3.1.0-1.exe from wherever you saved it. I'll be installing in D:\MinGW.

You could associate C++ files with d:\mingw\bin\gcc if you like, but I'll be working mainly within the command line.

You won't want to clutter the bin directory with programs, so add d:\mingw\bin to your PATH. Go to Start -> Settings -> Control Panel -> System -> Advanced -> Environment Variables, then either update the system path or add/modify the user path.

Then open a new DOS box and type "gcc" (not from the MinGW bin directory). You should get the message "gcc: no input files". This shows the installation has been successful.

Your First MinGW Program

For me, DOS boxes are opened in the directory c:\documents and settings\dave. This is as good a place as anywhere. Create a new file with the command notepad hello_dos.cpp.

Then enter:
#include <stdio.h>
int main()
  printf("Hello, world\n");
  return 0;

and save the file. Build it with g++ hello_dos.cpp -o hello_dos

and run it; you should get the output: "Hello, world"

Your first MinGW Windows program

Petzold's introductory Hello Windows program was a bit long at 84 lines, although the program existed for a different reason to this one. This one will be considerably simpler.

Create hello_win.cpp with Notepad as above and enter the following:
#include <windows.h>

  MessageBox(0,"Hello, Windows","MinGW Test Program",MB_OK);
  return 0;

Compile with g++ -mwindows hello_win.cpp -o hello_win

Then run it, and you should get a message box in the centre of the screen. This can also be run from Explorer. Without the -mwindows option, when run from Explorer you would also get a nasty DOS box appear for the duration of the program. However this might be a useful place to send debugging information; if you also include stdio.h you can use printf to display on this window.

For the WinMain prototype, go to MSDN and search for winmain. There are downloadable WinAPI references available elsewhere but I won't touch on them here.

Now for a REAL Windows program

There's quite a lot to a Windows program - creating the window class, the window procedure, not to mention resources and DLLs. I plan to pull this program apart in a future article. So here's a simple Windows program that displays a window.

#include <windows.h>

char *AppTitle="Win1";
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

int WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int nCmdShow)
  HWND hwnd;
  MSG msg;


  if (!RegisterClass(&wc))
    return 0;

  hwnd = CreateWindow(AppTitle,AppTitle,

  if (!hwnd)
    return 0;


  while (GetMessage(&msg,NULL,0,0) > 0)

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  switch (msg)
    case WM_PAINT:
      HDC dc;
      RECT r;
      DrawText(dc,"Hello World",-1,&r,DT_SINGLELINE|DT_CENTER|DT_VCENTER);

    case WM_DESTROY:

      return DefWindowProc(hwnd, msg, wparam, lparam);
  return 0;

This can also be downloaded from http://www.osix.net:80/modules/folder/index.php?tid=6126&action=vf.

WinMain is the entry point to the program. This function creates and registers the window class which stores information relevant to the application window - the icon, menu, window procedure and so on. Then it creates the window and enters into the application message loop. The GetMessage function returns zero when the application receives a WM_QUIT message.

WindowProc is the function that receives messages for the window and can be confusing in terms of what it should return, so here it is in very simple terms:
- if you process a message, you must return 0.
- if you don't process a message, you must return the result of DefWindowProc.

If you look at other programs you'll see window procedures in slightly different formats. Some will end a case block with a return 0 instead of a break, often also calling DefWindowProc at the end (outside the switch) and not in a default: clause. Whichever way you choose, make sure the code follows the above two rules and you should be ok. None is really more correct than any others, although my choice uses the standard switch structure (case/break not case/return), and associates the default action with the default: clause.

If the application should close when the main window is closed, which is common although not always the case, you should call PostQuitMessage in response to WM_DESTROY which is the last message sent to a window. If you don't, the application main window will close, but the application will still be active and the only way to kill it will be with Task Manager or equivalent.

Drawing on a window isn't necessarily the most intuitive process. All drawing operations operate on a device context (HDC), not a window handle. BeginPaint is one way to get a device context; the main benefit of BeginPaint over GetDC is that BeginPaint's device context is already clipped to the area of window that needs redrawing (specified in ps.rcPaint), thus providing a considerable speedup for applications whose redraw operations are long and complicated. BeginPaint should only be called in response to WM_PAINT; GetDC can be called at any time.

One advantage to using device contexts is that everything that can be drawn on - windows, memory bitmaps, printers etc - has an associated device context, so it is possible for exactly the same code to be used for drawing on the screen and printing. Further, if you decide to change the drawing to use double buffering to eliminate flicker, none of the sometimes complicated redraw code needs changing, only where it is invoked.


Did you like this article? There are hundreds more.

2005-03-22 07:44:56
cool, do you know of any popular applications written using mingw?
2005-03-24 14:53:22
Firefox can be compiled with MinGW. I think until quite recently it was as standard.

I once created a small Win32 app which could read, display and alter ID3 tags from MP3 files. It was a good exercise in file IO and Windows GUI coding. Perhaps you could choose a simple subject like that and step through the building of a GUI and filling out the event loop.
2005-04-01 12:01:39
I'm currently working on a resources article as a follow-up to this one. But I'm getting a bit bogged down in the detail so this could be a good alternative approach.
2005-04-14 17:15:33
There are 2 *very* basic articles about building resources (not MingW so it could be a good idea to explain how to link those resources?)
2005-08-26 17:02:36
Anyone know a decent (pref GPL'd) resource editor for Windows? Editing resource text files by hand is a right PITA, which is the main reason part 2 is still under development. There's lcc-win32 (iirc), but he sez you have to give him cash if you want to sell your software, which I suppose is fair enough, but I'm a stingy sod - if I wanted to pay for software I'd just get Visual Studio and be done with it - the resource editor there is excellent.

Tried Dev-C++ but v4 is quite frankly total pants (managed to get it to crash within about 2 seconds seconds of using it), and v5 has no resource editor at all.
2005-08-30 11:51:31
you still have ResHack

But it won't give you a resource script valid with your resource compiler (lcc)
At least it generates a script with a different syntax than masm resource compiler.

It is useful for coordinates (since it's visual)
and I suppose it can compile the scripts it generates so you can at least use the .RES
2005-08-30 11:53:38
lol, typo in the url :p
Should be:
2008-06-12 07:06:03
Thanks for posting this article. I have used your examples with g++ and had success at compiling them.
Have you ever tried to compile a program on the net called kbmidi.c ?
I believe it was written in MS c++7. I have tried to get it to compile with g++, and had errors I could not remedy. I have compared some of the code (winmain example and kbmidi winmain), and it looked like some easy fixes, but I am still getting errors. I do not know if you or someone else can help, but thanks if you or someone else has time.


2008-06-13 16:30:32
checkout the NetBeans IDE (www.netbeans.org) it is excellent for Java and has a C/C++ plugin - it helps to keep things together
2008-12-11 14:10:56
Visite Online-Flash-Game.com and play online Racing games .Visite Online-Flash-Game.com and play online Shooting games
2009-07-01 03:01:03
great porn at www.hannah-fans.com
2009-09-30 08:34:12
very good! thanks!
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:

Blogs: (People who have posted blogs on this subject..)
Blog entry for Mon 24th Dec 11pm on Mon 24th Dec 11pm
I am trying to make a batch file that will open the run menu. My problem is that I do not know the source for the run menu. I know this is probly something simple. Here is what I got... ........................................ ....... @echo off star
SVN as windows service calling post-commit hanging as not asynchronous on Wed 19th Dec 1pm
As any script you put inside post-commit.bat seems to be called synchronously, and doesnt inform the svn client that the commit has finished until the script has finished. I had to write a calling application which just starts the script in a new thread.
Blog entry for Thu 1st Mar 6am on Thu 1st Mar 6am
Hi my name is adam LOL

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Microsoft by abhijangda

Quiz based on the Microsoft Operating System
Reverse Engineering basics by sefo

I tried to cover the range of skills you will need to understand a win32 executable. Some of the following questions will take some time to answer. Do the test when you have enough free time.

Your Ad Here
Copyright Open Source Institute, 2006