The Module Definition File

In addition to the C source code, another file is required for Windows programs. It is called a ”module definition file“ and has the extension .DEF. The module definition file aids the LINK linker in creating the .EXE file by telling it the characteristics of the program's code and data segments, the size of the program's local data heap (from which the program can allocate memory), and the size of the program's stack. This information becomes part of the header section of the New Executable file format. The HELLOWIN.DEF file is shown in Figure 1-5 on page 16.

The NAME line defines HELLOWIN as a program (rather than a dynamic link library) and gives it a module name, which is usually the name of the program's .EXE file. The DESCRIPTION line simply inserts some text into the .EXE file. This is an excellent place for a copyright notice or version information. The EXETYPE line identifies the program as a Windows program. (OS/2 programs also use module definition files and the New Executable file format.)

The STUB is a program that is inserted into the .EXE file to be executed when anyone attempts to run HELLOWIN.EXE from the MS-DOS command line. The WINSTUB.EXE program included with the Windows Software Development Kit simply displays the message ”This program requires Microsoft Windows“ and terminates.

The CODE statement indicates that the program's code segment is flagged as PRELOAD (which means that Windows will load the segment into memory immediately) and MOVEABLE (which means that Windows can move the code segment to another location in memory if it needs to consolidate blocks of free memory). The DISCARDABLE option makes the code ”discardable“ (which means that Windows can discard the code segment from memory and later reload it from the .EXE file). These are the normal options for Windows programs. If you follow proper Windows programming practice, you will not (in theory) encounter any problems when Windows moves your code.

The DATA statement indicates that we want the data segment to be PRELOAD, MOVEABLE, and MULTIPLE. Again, we are giving Windows permission to move the data segment in memory if necessary. The MULTIPLE keyword requests that each instance of the program gets its own separate data segment. This is necessary because the data segment contains the program's stack and other data items that must be separate for each instance. The code segment, on the other hand, is shared by all instances of the program.

The HEAPSIZE line specifies the amount of extra local memory (memory in the program's own data segment) that will be available for allocation. The value depends on what the program needs. HELLOWIN doesn't need to allocate any local memory, but we'll throw in a small value nonetheless. Windows can expand a program's local heap if necessary.

The STACKSIZE line specifies the size of the stack. The value 8192 bytes is a minimum recommended value. You'll want a bigger stack size if your program has recursive functions or large non-static variables.

Finally, the EXPORTS line lists the window procedure WndProc. For reasons I'll discuss in Chapter 7, all window procedures that a program contains must be listed in the EXPORTS section of the module definition file.