Windows Programming

Microsoft Windows 9x/NT are 32-bit windows operating systems. However, programs on Window 3.x systems are 16-bit programs. This means, integers in Windows 3.x programs are like those in MS-DOS programs, but integers in Windows 9x/NT programs are 32-bit integers, so they equal to long int's but not short int's.

The structure of a Windows 3.x program is usually simpler than that of a Windows 9x/NT program, so I'll talk about the former first:

  1. There is a WinMain function that is called on starting of the program.
  2. In the WinMain function, the window class of the main window of the program should be registered.
  3. Then the main window of the program is created.
  4. After the creation of the main window, the main window is shown and drawn.
  5. The WinMain function begins getting messages from Windows.
  6. As a message is got, it is translated into a common format and dispatched to all windows of the application.
  7. When there are no more messages to get, the WinMain function stops getting messages and quits.
  8. After a message is got, there are window procedures that processes the message. Each window of the application has a window procedure.
  9. The message sent to the window procedure is in the following format:
    HWND hWnd, unsigned int message, WORD wParam, LONG lParam
  10. A message is usually for only one window, so when it is dispatched, only one window receives it.

From the text above you can see, a windows program is message-driven. That is to say, a windows program should wait for messages, then respond to them. Of course, due to this design, the state of the application should be recorded between two messages.

Actually there is a mechanism that sometimes makes this simpler: Windows uses call-back functions to process messages. They are called back when messages arrive. Surpose in a window procedure, a modal dialog box is started. As the dialog box cannot return immediately, the window procedure is held there. Actually, after the dialog box is created, the code of the window procedure is quitted but the position of the breaking is preserved, and the data are also preserved. When further messages arrive, the window procedure still works. When the dialog box quits, the window procedure is resumed at the recorded position and provided with the preserved data. The more modal dialogs are opened, the more data are preserved, so the more memory is consumed.

A Windows 9x/NT program has the similar basic structure, plus: threads are available. There are two kinds of threads available: working threads and window threads. A program usually starts with a window thread. As I talked earlier in the article "Windows Process Management in My Guess", working threads differ from window threads so that they provide better support for long processing. Threads provide us a way to have one program do several jobs at the same moment.

Return to Windows 9x/NT Overview