If you've been working with MS-DOS programming for awhile, you might guess that a Windows program interfaces with Windows through a software interrupt such as the MS-DOS Interrupt 0x21. You might guess that the linker adds bindings to your Windows programs that convert the Windows function calls into this software interrupt. But you would be wrong. A Windows program interfaces to Windows through a process called ”dynamic linking.“
Like MS-DOS programs, Windows executables have the filename extension .EXE. However, this is not the same .EXE format that is used in MS-DOS. Instead, Windows programs use a .EXE format called the New Executable file format, similar to that used in OS/2. Whenever a Windows program calls a Windows function, the C compiler generates assembly-language code for a far call. A table in the .EXE file identifies the function being called using a dynamic link library name and either a name or a number (called the ordinal number) of the function in that library.
Windows itself consists largely of three dynamic link libraries, called KERNEL (responsible for memory management, loading and executing programs, and scheduling), USER (the user interface and windowing), and GDI (the graphics). These libraries contain the code and data for the Windows functions. You can find these three dynamic link libraries in the SYSTEM subdirectory of your Windows directory.
When a Windows program is loaded into memory, the far calls in the program are resolved to point to the entry of the function in the dynamic link library, which is also loaded into memory. This is why all Windows functions must be defined as far: The code in the dynamic link libraries is not in the same segment as the program's code. Also, pointers passed in Windows functions must also be defined as far to avoid confusion with the dynamic link library's own code and data segments.
Generally, you don't have to worry about the use of far calls and far pointers because the functions are declared as far functions with far pointers in WINDOWS.H: The C compiler will perform the necessary address translations for you.
When you link a Windows program to produce an executable, you must link with a special ”import library“ provided with the Windows Software Development Kit. This import library contains the dynamic link library names and ordinal numbers of all the Windows functions. LINK uses this information to construct the table in the .EXE file that Windows uses to resolve calls to Windows functions when loading the program.