EARTH WEEK
Go paperless and save 40% on eligible eBooks, 70% on video now through April 26. Shop the sale.
Register your product to gain access to bonus material or receive a coupon.
With Win32 System Programming, you can capitalize on your knowledge of high-end operating systems such as UNIX, MVS, and VMS to learn Windows system programming quickly. Written from the perspective of an experienced programmer, the book presents the core operating system services of Win32, the common API for the Windows 95 and Windows NT operating systems. It explains Win32 functions clearly, with numerous comparisons to corresponding UNIX calls, and highlights features unique to Win32.
Because most experienced programmers are already familiar with processes, virtual memory, and preemptive scheduling, the book spends little time introducing these concepts, but instead shows how they are implemented in Win32. This text is for programmers using systems services, and focuses on the management of core operating systems resources rather than the graphical user interface.
Win32 System Programming offers extensive coverage of file systems, memory management, Structured Exception Handling, and security. In particular, it delves deeply into processes and threads, with detailed explanations of process synchronization, interprocess communication, and memory mapping. You will also find discussions of other advanced topics including file locking, DLLs, asynchronous I/O, fibers, and the registry.
Numerous realistic examples demonstrate many of Win32's most sophisticated features. In addition, the book compares the performance of alternative implementations of some of these examples to illustrate the trade-offs between various Win32 features and the C library. The accompanying CD-ROM contains all of the code examples in the text, a suite of programs for testing system performance, and a collection of UNIX-like utilities.
1. Windows NT and Windows 95.
Windows 95 and Windows NT: Overview.
Operating System Essentials.
Win32, Windows NT, and Windows 95.
Win32, Standards, and Open Systems.
What You Need to Use This Book.
Win32 Principles.
The Standard C Library: When to Use It.
Example: A Simple Sequential File Copy.
Summary.
Exercises.
The Win32 File Systems.
Win32 File Naming.
Opening, Reading, Writing, and Closing Files.
Interlude: Unicode and Generic Characters.
Unicode Strategies.
Standard Devices and Console I/O.
Example: Printing and Prompting.
Example: Error Processing.
Example: Copying Multiple Files to Standard Output.
Example: ASCII to Unicode Conversion.
File and Directory Management.
Example: Printing the Current Directory.
Summary.
Exercises.
The 64-Bit File System.
File Pointers.
Getting the File Size.
Example: Viewing the Tail of a File.
File Attributes and Directory Searching.
Example: A File Delete Utility.
Example: Listing File Attributes.
Example: Setting File Times.
File Processing Strategies.
Summary.
Exercises.
Exceptions and Their Handlers.
Floating-Point Exceptions.
Errors and Exceptions.
Example: Treating Errors as Exceptions.
Termination Handlers.
Example: Processing Exceptions and Completion.
Console Control Handlers.
Example: A Console Control Handler.
Summary.
Exercises.
Win32 Memory Management Architecture.
Heaps.
Managing Heap Memory.
Example: Sorting Files with a Binary Search Tree.
Memory-Mapped Files.
Example: Sequential File Processing with Mapped Files.
Example: Sorting a Memory-Mapped File.
Example: Using Based Pointers.
Summary.
Exercises.
Security Attributes.
Security Overview: The Security Descriptor.
Security Identifiers.
Managing ACLs.
Example: UNIX-Style Permission for NTFS Files.
Example: Initializing Security Attributes.
Reading and Changing Security Descriptors.
Example: Reading File Permissions.
Example: Changing File Permissions.
Overview of Additional Security Features.
Summary.
Exercises.
Win32 Processes and Threads.
Process Creation.
Process Identities.
Duplicating Handles.
Exiting and Terminating a Process.
Waiting for a Process to Terminate.
Environment Blocks and Strings.
Example: Parallel Pattern Searching.
Process Execution Times.
Example: Process Execution Times.
File Locking.
Generating Console Control Events.
Example: Simple Job Management.
Timers.
Example: A Periodic Alarm.
Timed Waits.
Summary.
Exercises.
Anonymous Pipes.
Example: I/O Redirection Using an Anonymous Pipe.
Named Pipes.
Named Pipe Convenience Functions.
Example: A Simple Client/Server Command Line Processor.
Comments on the Client/Server Command Line Processor.
Mailslots.
Pipe and Mailslot Creation, Connection, and Naming.
Example: A Server That Clients Can Locate.
Summary.
Exercises.
Overview of Threads.
Thread Basics.
Thread Management.
Using the C Library in Threads.
Example: Multithreaded Pattern Searching.
Example: Merge-Sort/Divide and Conquer to Exploit SMP.
Thread Local Storage.
Process and Thread Priority and Scheduling.
Thread States.
Summary.
Exercises.
Thread-Safe Code.
Thread and Process Synchronization Objects.
Critical Sections.
Example: Protecting Shared Variables with Critical Sections.
Mutexes.
Semaphores.
Examples: Using Semaphores.
Events.
Example: A Connection-Oriented Client.
Example: The Threaded Server/Pipe and Thread Management.
Thread Security and Impersonation.
Comments on Thread Models.
Summary.
Exercises.
Dynamic Link Libraries.
Example: Explicitly Linking a File Conversion Function.
Example: An In-Process Command Server.
Example: A DLL Server.
The IIS and ISAPI.
Example: A Skeleton ISAPI DLL.
Summary.
Exercises.
Overview of Win32 Asynchronous I/O.
Overlapped I/O.
Example: Synchronizing on a File Handle.
Example: File Conversion with Overlapped I/O and Multiple Buffers.
Extended I/O with Completion Routines.
Example: File Conversion with Extended I/O.
Asynchronous I/O with Threads.
Overview of I/O Completion Ports.
Summary.
Exercises.
Fibers.
The Registry.
Beyond the System Services.
Summary.
Exercises.
Disc Organization.
Include File Listings.
Additional Utility Programs.
Chapters 3 and 4: File and Directory Management.
Chapter 5: Structured Exception Handling and Timers.
Chapter 6: Memory Management and Memory-Mapped Files.
Chapter 7: Security.
Chapter 8: Process Management.
Chapter 9: Interprocess Communication.
Chapter 10: Thread Management.
Chapter 11: Win32 Synchronization.
Chapter 13: Asynchronous I/O.
Test Configurations.
Performance Measurements.
Running the Tests.
This book shows how to use the Win32 Application Programming Interface (API), concentrating on the core system services, including the file system, process and thread management, interprocess communication, and synchronization. User interfaces, network programming, internals, and I/O drivers, although important and interesting topics, are outside the book's scope. The examples concentrate on scenarios that are likely to arise in practice, and, in many cases, the examples can be used as a base for real applications.
The Win32 API is supported by Microsoft's 32-bit operating systems: Windows NT and Windows 95. There is no doubt that Win32 is an important factor for application developers, in many cases replacing UNIX as the preferred API for application programs targeted at the desktop and client/server systems. Many observers predict that Win32 will become the dominant programming interface, whereas others feel that UNIX and Win32 will coexist and each will find its own niche.
Regardless of the outcome of the operating system wars, many experienced programmers will want to learn the Win32 API quickly, and this book is designed to help them.
The first objectives are to explain what Win32 is, show how to use it in realistic situations, and do so as quickly as possible without burdening the reader with unnecessary detail. The book is, therefore, not comprehensive, but it does explain the central features of the most important functions and shows how to use them in realistic programming situations. Equipped with this knowledge, the reader will be able to use the comprehensive Microsoft reference documentation to explore details, advanced options, and the more obscure functions as requirements or interests dictate. I have found the Win32 API easy to learn using this approach, and I have greatly enjoyed developing Win32 programs despite occasional frustration. This enthusiasm will show through at times, as it should. This does not mean that I feel that Win32 is necessarily better in any sense, but it certainly has many attractive features.
Most Win32 books are designed for Windows 3.1 programmers and often concentrate on the user interface. These books spend a great deal of time explaining how processes, virtual memory, interprocess communication, and preemptive scheduling work. A programmer experienced in UNIX, VAX VMS, IBM MVS, or another high-end operating system-that is, nearly anything other than the Windows 3.1 Win16 API-will be familiar with these concepts and will be impatient to find out how they are implemented in Win32. Most Win32 books also spend a great deal of space on user interface programming. This book avoids the user interface, beyond discussing simple character-based console I/O, in the interest of concentrating on core features.
Chapters are organized topically so that the features required in even a single-process application are covered first, followed by process and thread management features. This organization allows the reader to advance logically from file systems to security, memory management, and file mapping, and then to processes, threads, synchronization, and other Win32 system services. This organization also allows the examples to evolve in a natural way, much as a developer might create a simple prototype and then add additional capability. The advanced features, such as asynchronous I/O, appear last.
Within each chapter, after introducing the functionality area, such as process management or memory-mapped files, we discuss important Win32 functions and their relationships in detail. Illustrative examples follow. Within the text, only essential parts of programs are listed; complete programs and the required include files, utility functions, and the like are in an appendix or on the disc provided with the book. Throughout, we identify those features supported only by Windows NT, as Windows 95 does not implement many advanced features. Each chapter suggests related additional reading and gives some exercises.
Chapter 1 is a high-level introduction to Windows NT and Windows 95, showing why you might need to learn about them. Chapter 2 compares UNIX, the C library, and Win32, with short file copy programs illustrating and contrasting each. Chapter 2 also introduces the Win32 programming style and conventions.
Chapters 3 and 4 deal with file systems, console I/O, and directory management. Unicode, the extended character set used by Windows NT, is also introduced here. Examples include sequential and direct file processing and a directory traversal program.
Chapter 5 introduces Win32's Structured Exception Handling (SEH) capability, which will be used extensively starting in Chapter 6. Many books defer SEH to later chapters, but by introducing it early, this book lets you use it throughout.
Chapter 6 treats Win32 memory management and shows how to use memory-mapped files.
Chapter 7 explains Windows NT security, showing, in an example, how to emulate UNIX-style file permissions. Security upgrades can then be applied to the examples as appropriate. Although security is used in the later chapters, feel free to skip this chapter if you are not interested in security.
Chapter 8 introduces Win32 processes and simple process synchronization along with file locking. Chapter 9 then shows how to use anonymous and named pipes for interprocess communication. Chapter 10 discusses threads, and Chapter 11 covers the various synchronization primitives: mutexes, semaphores, events, and critical sections.
Chapter 12 gives a brief introduction to some additional topics, including dynamic link libraries, and shows how to create in-process servers using DLLs. The Internet Server API (ISAPI) is used to illustrate these concepts.
Chapter 13 shows how to use overlapped I/O with events and completion routines. For file systems, this feature applies only to Windows NT, and you can achieve much the same thing with threads, so this chapter will be optional for some readers.
Chapter 14 concludes with a survey of two specialized topics: fibers and the registry. Beyond discussing these features, this chapter illustrates that readers now have the necessary information to learn other Win32 topics beyond the core system services.
There are three appendixes. Appendix A describes the programs on the disc and how to use them. Appendix B contains several tables that compare Win32 functions with their counterparts in UNIX and the Standard C library. Appendix C compares the performance of alternative implementations of some of the examples in the text so that you can gauge the trade-offs between Win32 features, both basic and advanced, and the C library.
Within the text at appropriate points, we contrast Win32 style and functionality with the comparable UNIX and ANSI Standard C library features. Tables listing the comparable functions are in Appendix B. This information is included because many readers are familiar with UNIX and are interested in the comparisons between the two systems. Readers without a UNIX background should feel free to skip these paragraphs. Such discussions are indented, in a smaller font, and labeled with a UNIX icon.
Exercises at the end of each chapter suggest alternative designs, subjects for investigation, and additional functionality. Some exercises are easy, and a few are very challenging.
All examples have been debugged and tested under Windows NT and, where appropriate, under Windows 95. For Windows NT testing we used versions 3.51 and 4.0, and although the bulk of the development was performed on single-processor, Intel-based systems, the programs were also tested on multi-processor and Digital Alpha systems. The client/server applications have been tested using multiple clients simultaneously interacting with a server. Nonetheless, there is no guarantee or assurance of program correctness, completeness, or fitness for any purpose. Undoubtedly, even the simplest examples contain defects or will fail under some conditions; such is the fate of nearly all software. I will, however, gratefully appreciate any messages regarding program defects-and, better still, fixes.
Numerous people have provided assistance, advice, and encouragement. It is conventional to say that the book would not have been possible without them; in this case, that would be an understatement. The Addison Wesley Longman editors-Ben Ryan and Mike Hendrickson-have patiently steered the book through the many stages from first concept to publication, and Sarah Weaver has been an excellent and helpful project manager. Betsy Hardinger, the copy editor, made many improvements. Alan Feuer, the series editor, deserves special and immediate thanks and recognition. Not only has Alan provided the normal encouragement and prodding, but his suggestion to develop UNIX-like commands as sample programs greatly improved my first clumsy examples. Students in the Technology Exchange Company's course 835, Win32 System Programming, which is based on this book, have provided invaluable suggestions. Readers who have made useful comments or provided encouragement at various stages include, in alphabetical order, Maury Bach, Ed Dekker, Peggy Harris, Shirley Kaltenbach, Joe Newcomer, Ali Rafieymehr, Edward Schiebel, and Larry Schmuhl. Hein van den Heuvel and Marc Slater of Digital Equipment Corporation arranged access to the Alpha and SMP systems used for performance tests in Appendix C. Elissa Armour prepared the manuscript with patience, skill, and professionalism. Dominic Chan and Allan Deary of Peritus Software Services, Inc., have encouraged this effort and made computing resources available for developing both the text and the example programs. Most importantly, my wife Linda is the one whose support has really made this book possible. It's been a wonderful 30 years together, and we look forward to many, many more.
Johnson (John) M. Hart
0201634651P04062001