Quick viewing(Text Mode)

Copyrighted Material

Copyrighted Material

4134FM.qxd 8/27/02 11:34 AM Page viii

Contents at a Glance

Introduction xix

Part I An Overview of the Win32 API 1

Chapter 1: Overcoming Holes in the .NET Framework 3 Chapter 2: Working with Win32 API Data 21 Chapter 3: Accessing the Win32 API 45 Chapter 4: Processing Windows Messages 83 Chapter 5: Using Callback Functions 109

Part II Fixing Holes in Standard Applications 131

Chapter 6: Gaining Support for the Console Application 133 Chapter 7: Accessing the Hardware Directly 165 Chapter 8: Overcoming Security Issues 199

Part III Fixing Advanced Win32 API Problems 239

Chapter 9: Accessing Windows XP Special Features 241 Chapter 10: Using Special Functions 275 Chapter 11: COPYRIGHTEDAccessing the Media Player MATERIAL 301 Chapter 12: Working with Management Console 335

Part IV Working with DirectX 375

Chapter 13: Creating DirectX Structures and Data Elements 377 Chapter 14: Developing DirectX Access Routines 401 4134FM.qxd 8/27/02 11:34 AM Page ix

Chapter 15: Creating Applications with DirectX 8.1 437 Chapter 16: Extended DirectX Programming Examples 467

Part V Appendices 501

Appendix A: Fifty-Two Tips for Error-Free Win32 API Access 503 Appendix B: Fixes for Common API Access Errors 511 Glossary 519 Index 537 4134FM.qxd 8/27/02 11:34 AM Page x

Contents

Introduction xix

Part I An Overview of the Win32 API 1

Chapter 1 Overcoming Holes in the .NET Framework 3 Why Access the Win32 API? 4 A Case of Missing Functionality 5 Win32 Function Types 7 Win32 Access Requirements 10 Win32 Access for C# Developers 14 Understanding the Effects of Unsafe Code 14 Understanding the Effects of Pointers 16 Advantages for the C# Developer 17 Win32 Access for Visual Basic Developers 18 Understanding Visual Basic Limitations 18 Advantages for the Visual Basic Developer 19 Where Do You Go from Here? 20

Chapter 2 Working with Win32 API Data 21 A Short View of Data 22 Working with Variables 28 Converting Variables to C# Types 29 Converting Variables to Visual Basic Types 30 Special Rules for COM 31 Working with Data Structures 33 Working with Pointers 37 Working with Enumerations 38 Importing Resources 39 Understanding .NET Framework Functionality 40 4134FM.qxd 8/27/02 11:34 AM Page xi

Contents xi

Using the IDE Features 41 Working with the Win32 API 42 Where Do You Go from Here? 43

Chapter 3 Accessing the Win32 API 45 An Overview of the DLLs 46 Types of Win32 Access 48 Direct DLL Access 48 C LIB Access 50 A Direct DLL Access Example 52 A C LIB Wrappers Access Example 54 A C LIB Substitute Functions Example 62 Interpreting Error and Result Values 63 Helpful Win32 Programming Tools 66 Dependency Walker 66 Error Lookup 75 Spy++ 76 Where Do You Go from Here? 81

Chapter 4 Processing Windows Messages 83 Understanding the Windows Message Types 84 Windows Message Handlers Found in the .NET Framework 88 An Overview of Windows Message Functions 90 SendMessage() 90 PostMessage(), PostThreadMessage, and PostQuitMessage() 92 SendNotifyMessage() 99 SendMessageCallback() 99 GetMessage() and PeekMessage() 100 RegisterWindowMessage() 101 Creating a Windows Message Handler Example 101 Creating the Event 102 Creating the Windows Message Loop Override 102 Creating the Event Handler 104 Demonstrating the Windows Message Handler 104 Developing for Thread Safety 106 Where Do You Go from Here? 107 4134FM.qxd 8/27/02 11:34 AM Page xii

xii Contents

Chapter 5 Using Callback Functions 109 What Is a Callback Function? 110 Using Callback Functions 111 An Overview of Callback Function Prototypes 112 Implementing a Callback from the Managed Environment 113 Implementing a Callback from a Wrapper DLL 118 Enumerating Information Example 123 Creating the Macro Wrapper DLL 124 Creating the EnumCalendarInfoEx() Function Code 127 Demonstrating the Calendar Enumeration 128 Where Do You Go from Here? 129

Part II Fixing Holes in Standard Applications 131

Chapter 6 Gaining Support for the Console Application 133 What Does the .NET Framework Provide? 134 Clearing the Screen Example 136 Getting Console Information Examples 139 Working with Code Pages 139 Gaining Access to Console Mode Information 141 Working with Processes 147 Changing the Environment Examples 149 Accessing the Environmental Strings 149 Using Cursors and Fonts 153 Determining the Console Window Title 158 Manipulating the Console Screen Buffer 160 Where Do You Go from Here? 164

Chapter 7 Accessing the Hardware Directly 165 When Is Direct Access Required? 167 Serial Port Access Examples 168 Creating a Serial Stream 169 Determining the Port Status 177 Parallel Port Access Examples 185 Creating the Wrapper DLL 186 4134FM.qxd 8/27/02 11:34 AM Page xiii

Contents xiii

Viewing the First Level of Printer Information 189 Viewing the Second Level of Printer Information 191 Where Do You Go from Here? 196

Chapter 8 Overcoming Security Issues 199 An Overview of Windows Security 200 A Detailed View of the Windows Security API 202 An Overview of the Functions 209 Using the Access Control Editor 214 Using the Security Configuration Editor 216 Understanding How .NET Role-Based Security Differs 219 Looking Up an Account SID Example 223 Using the GetFileSecurity() Function Example 227 Working with ACEs Example 230 Where Do You Go from Here? 236

Part III Fixing Advanced Win32 API Problems 239

Chapter 9 Accessing Windows XP Special Features 241 What’s New in Windows XP? 242 Common Features 243 Professional Edition Exclusive Features 245 Determining the Operating System Version Example 246 Working with Theme Support Example 253 Balloon Help Example 262 Using NUnit for Automated Testing 268 Where Do You Go from Here? 272

Chapter 10 Using Operating System Special Functions 275 Accessing Status and Other Information 277 Learning How to Avoid Version Compatibility Problems 278 Determining System Power Status Example 281 Creating an Application Shortcut Example 286 Shutting the System Down Remotely Example 290 4134FM.qxd 8/27/02 11:34 AM Page xiv

xiv Contents

Obtaining Device Capabilities Example 292 File Compression 295 Using PC-Lint for C++ Development 296 Where Do You Go from Here? 300

Chapter 11 Accessing the Media Player 301 A Quick Overview of the Media Player 302 General Operation 303 Internet Content 305 Using the Media Library 306 Displaying a Configuration Dialog Example 307 Playing Sound Files Example 313 Determining Device Capabilities 314 Opening a File 317 Playing a File 326 Pausing and Stopping a File 328 Required Application Cleanup 330 Using IconForge 330 Where Do You Go from Here? 334

Chapter 12 Working with Microsoft Management Console 335 Using GUIDGen 337 The Case for an MMC Snap-In 338 Creating a Wrapper DLL 341 Creating an Interface 342 Writing the MMC Wrapper DLL 347 Developing the MMC Snap-In Example 360 Developing an About Dialog 360 Working with IDataObject 363 Creating IComponent and IComponentData 366 Testing the Snap-In within MMC 369 Where Do You Go from Here? 372 4134FM.qxd 8/27/02 11:34 AM Page xv

Contents xv

Part IV Working with DirectX 375

Chapter 13 Creating DirectX Structures and Data Elements 377 Using the DXDIAG Utility 378 Learning about DirectX Compatibility 380 Viewing the Drivers 383 Working with the DirectX Structures 384 An Overview of the Data Structures 385 Structure Conversion Essentials 388 Understanding DirectX Data Pitfalls in the Managed Environment 397 Where Do You Go from Here? 399

Chapter 14 Developing DirectX Access Routines 401 Working with DirectX Functions 402 DirectDrawCreate() and DirectDrawCreateEx() 403 DirectDrawCreateClipper() 405 DirectDrawEnumerate() and DirectDrawEnumerateEx() 405 Function Declarations 406 Function Return Values 409 Creating DirectX Callback Function Prototypes 412 DDEnumCallback() and DDEnumCallbackEx() 412 EnumModesCallback() and EnumModesCallback2() 413 EnumSurfacesCallback(), EnumSurfacesCallback2(), and EnumSurfacesCallback7() 413 EnumVideoCallback() 414 Delegate Declarations 414 Working with the DirectX Interfaces and Classes 415 Learning the DirectX Capabilities of the Host Machine Example 416 Using the DirectDrawEnumerateEx() Function 416 Using the DirectX Caps Viewer Tool 419 A Simple 2D DirectDraw Example 424 Importing the DirectX COM Library 424 Writing the Code 425 Using the GIF Construction Set 430 Where Do You Go from Here? 435 4134FM.qxd 8/27/02 11:34 AM Page xvi

xvi Contents

Chapter 15 Creating Applications with DirectX 8.1 437 An Overview of DirectX 7 and DirectX 8.1 Differences 438 Consolidated Objects 439 Updated Objects and Features 440 An Overview of the Tools 442 Using the DirectX Application 442 Using the DMO Test Utility 447 Creating a Test File Using GraphEdit 447 Testing the WavesReverb DMO 449 A Simple DirectSound Example 452 Using the Force Feedback Editor 454 A Simple DirectInput Example 459 Using the DirectX Texture Tool 463 Where Do You Go from Here? 465

Chapter 16 Extended DirectX Programming Examples 467 Using the GraphEdit Utility 468 Creating a Connection 469 Working with Multiple Streams 470 Working with Filters 472 Performance Tuning 474 Saving the Result 476 Using the DirectPlay Network Simulator 477 Using the MeshView Tool 479 Creating a Mesh 480 Adding Color 482 Making Other Changes 485 Using a Mesh Example 489 Initializing the Application 489 Loading a Mesh File 491 Displaying the Image On Screen 494 A Few Words about Output 498 Where Do You Go from Here? 500 4134FM.qxd 8/27/02 11:34 AM Page xvii

Contents xvii

Part V Appendices 501

Appendix A Fifty-Two Tips for Error-Free Win32 API Access 503

Appendix B Fixes for Common API Access Errors 511 Resolving Data Corruption in Data Structures 512 The [DllImport] Attribute Works Improperly 513 Data Transfer Problems 514 .NET Implementation of COM Interface Doesn’t Work 516 Handling Memory Leaks and Other Resource Problems 517 Windows Doesn’t Appear to Handle Messages Correctly 518

Glossary 519

Index 537