Win32 Systems Programming
Authors: Johnson M. Hart
Venue:  (1997)

From the Book: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. Audience Anyone who has experience programming in UNIX or in an operating system other than Windows 3.1 and who wants to learn about Win32 quickly. Programmers and software engineers who must port existing applications, often in UNIX, to Win32 for operation under Windows NT or Windows 95. This book contains many comparisons between Win32, UNIX, and standard C library functions and programming models. All common UNIX functionality, including process management, synchronization, file systems, and interprocess communication, is covered in Win32 terms. Programmers who are developing servers or other systems in which management of resources such as processes and threads is of primary importance and in which the user interface is of secondary importance. Readers starting new projects who are not constrained by the need to port existing code. Many aspects of program design and implementation are covered, and Win32 functions are used to create useful applications and to solve common programming problems. Computer science students at the upperclass undergraduate or beginning graduate level in courses covering systems programming or application development. This book would be a useful complementary text to a book such as W. Richard Stevens's Advanced Programming in the UNIX Environment (see the Bibliography) so that students could compare Win32 and UNIX. Organization 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. UNIX and C Library Notes and Tables 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. Examples The examples are designed to do the following: Illustrate common, representative, and useful applications of the Win32 functions. Correspond to real programming situations encountered in program development, consulting, and teaching. Emphasize how the functions actually behave and interact, which is not always as you might first expect after reading the documentation. Grow and expand, adding new capability to a previous solution in an easy and natural manner and exploring alternative implementation techniques. Frequently implement UNIX commands, such as ls, rm, and sort, showing the Win32 functions in a familiar context while creating a useful set of utilities. Different implementations of the same command will also give us an easy way to compare performance benefits available with advanced Win32 features. Appendix C contains the results of these performance tests. 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. Acknowledgments 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 jmhart@world.std.com