Windows System Programming, Paperback

Johnson M. Hart  
Total pages
September 2015
Related Titles

Product detail

Product Price CHF Available  
Windows System Programming, Paperback
82.60 approx. 7-9 days


Windows System Programming, 4/e is the definitive developer's guide to making the most of the core Windows APIs, including those introduced with Windows Vista, Windows Server 2008, and beyond. Thoroughly updated to reflect Microsoft's new APIs, this book focuses on essential core system services -- file system, memory, processes and threads, synchronization, communication, and security -- rather than the more commonly featured graphical user interface functions. Beginning with an examination of the features required in a single-process application, the text gradually progresses to increasingly sophisticated functions and multithreaded environments. Each chapter contains realistic examples, and this edition's code samples have been updated and streamlined to reflect today's best practices for Windows development and 64-bit code portability. This edition's extensive new coverage includes: " An all-new chapter on parallelism and performance in multicore systems " Detailed new coverage of source code portability across Windows, Linux, and UNIX " New coverage of .NET and managed code impact and co-existence " When, why, and how to use the Windows API vs. .NET " More coverage of security, benchmarking, and other key topics Many readers have noted that Hart's book perfectly complements Mark Russinovich's well-known Windows Internals: Hart shows how to make the most of the features that Russinovich describes.


  • The book's Web site ( contains downloadable Examples file with complete code and projects for all the book’s examples a number of exercise solutions, alternative implemeaitons, instructions, and performance evaluation tests.
  •  Website will contain book errata, along with additional examples, reader contributions, additional explanations and much more
  • Website will contain PowerPoint slides for professional training and college courses
  • Includes extensive new coverage of Win64, parallelism, multicore system performance, source code portability, .NET coexistence, security, benchmarking, and much more
  • Updated, streamlined code examples reflect today's most effective Windows programming techniques

  • The perfect practical complement to Mark Russinovich's Windows Internals


New to this Edition

1. A new chapter on parallelism and performance with multicore systems. This material will follow naturally from existing material in Chapter 7.
2. A new chapter on source code portability (Windows, Linux, UNIX)
3. .NET and managed code impact and co-existence (perhaps a chapter, or, perhaps integrated into the existing chapters as appropriate). Open Question: Also, something about C# and maybe VB.
4. 64-bit source-code portability throughout, as opposed to the current separate Chapter 16.
5. New API features (Vista and beyond).
6. When, why, and how to use the Windows API, with benefits (as opposed to using .NET, for example).
7. The security chapter needs some extension. I haven't figured this out yet.
8. Appendix C (performance measurements) can be extended; more is needed, for example, on parallelism and memory-mapped I/O vs. file I/O (I've had some really good results there recently).
9. New examples as appropriate.
10. Modifications: Some of the example code can be improved and streamlined. Some coding style needs work as well.

Table of Contents

Figures xvii

Tables xix

Programs xxi

Program Runs xxv

Preface xxvii

About the Author xxxvii


Chapter 1: Getting Started with Windows 1

Operating System Essentials 1

Windows Evolution 2

Windows Versions 3

The Windows Market Role 5

Windows, Standards, and Open Systems 6

Windows Principles 7

32-bit and 64-bit Source Code Portability 10

The Standard C Library: When to Use It for File Processing 10

What You Need to Use This Book 11

Example: A Simple Sequential File Copy 13

Summary 20

Exercises 22


Chapter 2: Using the Windows File System and Character I/O 25

The Windows File Systems 26

File Naming 27

Opening, Reading, Writing, and Closing Files 28

Interlude: Unicode and Generic Characters 34

Unicode Strategies 37

Example: Error Processing 38

Standard Devices 39

Example: Copying Multiple Files to Standard Output 41

Example: Simple File Encryption 43

File and Directory Management 46

Console I/O 51

Example: Printing and Prompting 53

Example: Printing the Current Directory 55

Summary 56

Exercises 57


Chapter 3: Advanced File and Directory Processing, and the Registry 59

The 64-Bit File System 59

File Pointers 60

Getting the File Size 64

Example: Random Record Updates 65

File Attributes and Directory Processing 70

Example: Listing File Attributes 75

Example: Setting File Times 78

File Processing Strategies 80

File Locking 81

The Registry 86

Registry Management 88

Example: Listing Registry Keys and Contents 92

Summary 96

Exercises 97


Chapter 4: Exception Handling 101

Exceptions and Their Handlers 101

Floating-Point Exceptions 108

Errors and Exceptions 110

Example: Treating Errors as Exceptions 112

Termination Handlers 113

Example: Using Termination Handlers to Improve Program Quality 117

Example: Using a Filter Function 120

Console Control Handlers 124

Example: A Console Control Handler 126

Vectored Exception Handling 128

Summary 129

Exercises 130


Chapter 5: Memory Management, Memory-Mapped Files, and DLLs 131

Windows Memory Management Architecture 132

Heaps 134

Managing Heap Memory 137

Example: Sorting Files with a Binary Search Tree 143

Memory-Mapped Files 149

Example: Sequential File Processing with Mapped Files 156

Example: Sorting a Memory-Mapped File 158

Example: Using Based Pointers 162

Dynamic Link Libraries 167

Example: Explicitly Linking a File Conversion Function 172

The DLL Entry Point 174

DLL Version Management 175

Summary 177

Exercises 178


Chapter 6: Process Management 181

Windows Processes and Threads 181

Process Creation 183

Process Identities 190

Duplicating Handles 191

Exiting and Terminating a Process 192

Waiting for a Process to Terminate 194

Environment Blocks and Strings 195

Example: Parallel Pattern Searching 197

Processes in a Multiprocessor Environment 201

Process Execution Times 202

Example: Process Execution Times 202

Generating Console Control Events 204

Example: Simple Job Management 205

Example: Using Job Objects 215

Summary 219

Exercises 220


Chapter 7: Threads and Scheduling 223

Thread Overview 223

Thread Basics 225

Thread Management 226

Using the C Library in Threads 231

Example: Multithreaded Pattern Searching 232

Performance Impact 235

The Boss/Worker and Other Threading Models 236

Example: Merge-Sort–Exploiting Multiple Processors 237

Introduction to Program Parallelism 244

Thread Local Storage 245

Process and Thread Priority and Scheduling 246

Thread States 249

Pitfalls and Common Mistakes 251

Timed Waits 252

Fibers 253

Summary 256

Exercises 256


Chapter 8: Thread Synchronization 259

The Need for Thread Synchronization 259

Thread Synchronization Objects 268


A CRITICAL_SECTION for Protecting Shared Variables 271

Example: A Simple Producer/Consumer System 273

Mutexes 279

Semaphores 284

Events 287

Example: A Producer/Consumer System 289

More Mutex and CRITICAL_SECTION Guidelines 294

More Interlocked Functions 296

Memory Management Performance Considerations 297

Summary 298

Exercises 298


Chapter 9: Locking, Performance, and NT6 Enhancements 301

Synchronization Performance Impact 302

A Model Program for Performance Experimentation 307

Tuning Multiprocessor Performance with CS Spin Counts 307

NT6 Slim Reader/Writer Locks 309

Thread Pools to Reduce Thread Contention 312

I/O Completion Ports 316

NT6 Thread Pools 316

Summary: Locking Performance 324

Parallelism Revisited 325

Processor Affinity 329

Performance Guidelines and Pitfalls 331

Summary 332

Exercises 333


Chapter 10: Advanced Thread Synchronization 335

The Condition Variable Model and Safety Properties 336

Using SignalObjectAndWait 342

Example: A Threshold Barrier Object 344

A Queue Object 348

Example: Using Queues in a Multistage Pipeline 352

Windows NT6 Condition Variables 362

Asynchronous Procedure Calls 366

Queuing Asynchronous Procedure Calls 367

Alertable Wait States 368

Safe Thread Cancellation 371

Pthreads for Application Portability 372

Thread Stacks and the Number of Threads 372

Hints for Designing, Debugging, and Testing 372

Beyond the Windows API 375

Summary 375

Exercises 376


Chapter 11: Interprocess Communication 379

Anonymous Pipes 380

Example: I/O Redirection Using an Anonymous Pipe 380

Named Pipes 384

Named Pipe Transaction Functions 390

Example: A Client/Server Command Line Processor 393

Comments on the Client/Server Command Line Processor 399

Mailslots 401

Pipe and Mailslot Creation, Connection, and Naming 405

Example: A Server That Clients Can Locate 406

Summary 408

Exercises 408


Chapter 12: Network Programming with Windows Sockets 411

Windows Sockets 412

Socket Server Functions 414

Socket Client Functions 419

Comparing Named Pipes and Sockets 421

Example: A Socket Message Receive Function 422

Example: A Socket-Based Client 423

Example: A Socket-Based Server with New Features 426

In-Process Servers 434

Line-Oriented Messages, DLL Entry Points, and TLS 436

Example: A Thread-Safe DLL for Socket Messages 437

Example: An Alternative Thread-Safe DLL Strategy 442

Datagrams 445

Berkeley Sockets versus Windows Sockets 447

Overlapped I/O with Windows Sockets 447

Windows Sockets Additional Features 448

Summary 448

Exercises 449


Chapter 13: Windows Services 453

Writing Windows Services–Overview 454

The main() Function 454

ServiceMain() Functions 455

The Service Control Handler 460

Event Logging 461

Example: A Service “Wrapper” 461

Managing Windows Services 467

Summary: Service Operation and Management 471

Example: A Service Control Shell 472

Sharing Kernel Objects with a Service 476

Notes on Debugging a Service 477

Summary 478

Exercises 478


Chapter 14: Asynchronous Input/Output and Completion Ports 481

Overview of Windows Asynchronous I/O 482

Overlapped I/O 483

Example: Synchronizing on a File Handle 487

Example: File Conversion with Overlapped I/O and Multiple Buffers 487

Extended I/O with Completion Routines 492

Example: File Conversion with Extended I/O 496

Asynchronous I/O with Threads 500

Waitable Timers 501

Example: Using a Waitable Timer 503

I/O Completion Ports 505

Example: A Server Using I/O Completion Ports 509

Summary 516

Exercises 517


Chapter 15: Securing Windows Objects 519

Security Attributes 519

Security Overview: The Security Descriptor 520

Security Descriptor Control Flags 523

Security Identifiers 523

Managing ACLs 525

Example: UNIX-Style Permission for NTFS Files 527

Example: Initializing Security Attributes 531

Reading and Changing Security Descriptors 535

Example: Reading File Permissions 537

Example: Changing File Permissions 538

Securing Kernel and Communication Objects 539

Example: Securing a Process and Its Threads 541

Overview of Additional Security Features 542

Summary 544

Exercises 544


Appendix A: Using the Sample Programs 547

Examples File Organization 548


Appendix B: Source Code Portability: Windows, UNIX, and Linux 549

Source Code Portability Strategies 550

Windows Services for UNIX 550

Source Code Portability for Windows Functionality 551

Chapters 2 and 3: File and Directory Management 556

Chapter 4: Exception Handling 561

Chapter 5: Memory Management, Memory-Mapped Files, and DLLs 562

Chapter 6: Process Management 563

Chapter 7: Threads and Scheduling 565

Chapters 8—10: Thread Synchronization 567

Chapter 11: Interprocess Communication 569

Chapter 14: Asynchronous I/O 571

Chapter 15: Securing Windows Objects 572


Appendix C: Performance Results 575

Test Configurations 575

Performance Measurements 577

Running the Tests 591


Bibliography 593


Index 597


Johnson M. Hart is a consultant specializing in Microsoft Windows and .NET application development, open systems computing, technical training and writing, and software engineering. He has more than twenty-five years of experience as a software engineer, manager, engineering director, and senior technology consultant at Cilk Arts, Inc., Sierra Atlantic, Hewlett-Packard, and Apollo Computer. He served as computer science professor at the University of Kentucky for nine years, and has authored all four editions of Windows System Programming.

Reader Review(s)

“If you’re writing a native Win32 program or just want to know what the OS is really doing underneath, you need John’s book. He covers the stuff that real systems programmers absolutely must know. Recommended.”

–Chris Sells, Microsoft Corporation


“This fourth edition does a great job of incorporating new features in the Vista, Windows 2008, and Windows 7 API, but also stays true to teaching the foundational elements of building applications that target the Windows OS.”

–Jason Beres, Product Management, Infragistics