ebook img

.Net Framework Solutions: In Search of the Lost Win32 API PDF

437 Pages·2002·4.3 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview .Net Framework Solutions: In Search of the Lost Win32 API

.NET Framework Solutions—In Search of the Lost Win32 API John Paul Mueller Associate Publisher Joel Fuggazzotto Acquisitions and Developmental Editor Denise Santoro Lincoln Editors Judy Flynn William Rodarmor Production Editor Kylie Johnston Technical Editor Ross Russell Mullen Graphic Illustrator Jeff Wilson Happenstance Type-O-Rama Electronic Publishing Specialists Jill Niles Judy Fung Scott Benoit Proofreaders Laurie O’Connell Dave Nash Nancy Riddiough Monique van den Berg Indexer Lynnzee Elze CD Coordinator Dan Mummert CD Technician Kevin Ly Cover Designer Caryl Gorska/Gorska Design Cover Photographers Peter Samuels Tony Stone Copyright © 2002 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved. The author(s) created reusable code in this publication expressly for reuse by readers. Sybex grants readers limited permission to reuse the code found in this publication or its accompanying CD-ROM so long as (author(s)) are attributed in any application containing the reusable code and the code itself is never distributed, posted online by electronic transmission, sold, or commercially exploited as a stand-alone product. Aside from this specific exception concerning reusable code, no part of this publication may be stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to photocopy, photograph, magnetic, or other record, without the prior agreement and written permission of the publisher. Library of Congress Card Number: 2002108076 ISBN: 0-7821-4134-X SYBEX and the SYBEX logo are either registered trademarks or trademarks of SYBEX Inc. in the United States and/or other countries. Screen reproductions produced with FullShot 99. FullShot 99 © 1991-1999 Inbit Incorporated. All rights reserved. FullShot is a trademark of Inbit Incorporated. The CD interface was created using Macromedia Director, COPYRIGHT 1994, 1997-1999 Macromedia Inc. For more information on Macromedia and Macromedia Director, visit http://www.macromedia.com/ . TRADEMARKS: SYBEX has attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer. The author and publisher have made their best efforts to prepare this book, and the content is based upon final release software whenever possible. Portions of the manuscript may be based upon pre- release versions supplied by software manufacturer(s). The author and the publisher make no representation or warranties of any kind with regard to the completeness or accuracy of the contents herein and accept no liability of any kind including but not limited to performance, merchantability, fitness for any particular purpose, or any losses or damages of any kind caused or alleged to be caused directly or indirectly from this book. Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Software License Agreement: Terms and Conditions The media and/or any online materials accompanying this book that are available now or in the future contain programs and/or text files (the “Software”) to be used in connection with the book. SYBEX hereby grants to you a license to use the Software, subject to the terms that follow. Your purchase, acceptance, or use of the Software will constitute your acceptance of such terms. The Software compilation is the property of SYBEX unless otherwise indicated and is protected by copyright to SYBEX or other copyright owner(s) as indicated in the media files (the “Owner(s)”). You are hereby granted a single-user license to use the Software for your personal, noncommercial use only. You may not reproduce, sell, distribute, publish, circulate, or commercially exploit the Software, or any portion thereof, without the written consent of SYBEX and the specific copyright owner(s) of any component software included on this media. In the event that the Software or components include specific license requirements or end-user agreements, statements of condition, disclaimers, limitations or warranties (“End-User License”), those End-User Licenses supersede the terms and conditions herein as to that particular Software component. Your purchase, acceptance, or use of the Software will constitute your acceptance of such End-User Licenses. By purchase, use or acceptance of the Software you further agree to comply with all export laws and regulations of the United States as such laws and regulations may exist from time to time. Reusable Code in This Book The authors created reusable code in this publication expressly for reuse for readers. Sybex grants readers permission to reuse for any purpose the code found in this publication or its accompanying CD-ROM so long as all of the authors are attributed in any application containing the reusable code, and the code itself is never sold or commercially exploited as a stand-alone product. Notice The Graphic Workshop and GIF Construction Set software included with this publication is provided as shareware for your evaluation. If you try this software and find it useful, you are requested to register it as discussed in its documentation and in the applications’ “About” screen. SYBEX has not paid the registration fee for this shareware. Software Support Components of the supplemental Software and any offers associated with them may be supported by the specific Owner(s) of that material, but they are not supported by SYBEX. Information regarding any available support may be obtained from the Owner(s) using the information provided in the appropriate read.me files or listed elsewhere on the media. Should the manufacturer(s) or other Owner(s) cease to offer support or decline to honor any offer, SYBEX bears no responsibility. This notice concerning support for the Software is provided for your information only. SYBEX is not the agent or principal of the Owner(s), and SYBEX is in no way responsible for providing any support for the Software, nor is it liable or responsible for any support provided, or not provided, by the Owner(s). Warranty SYBEX warrants the enclosed media to be free of physical defects for a period of ninety (90) days after purchase. The Software is not available from SYBEX in any other form or media than that enclosed herein or posted to http://www.sybex.com/. If you discover a defect in the media during this warranty period, you may obtain a replacement of identical format at no charge by sending the defective media, postage prepaid, with proof of purchase to: SYBEX Inc. Product Support Department 1151 Marina Village Parkway Alameda, CA 94501 Web: http://www.sybex.com/ After the 90-day period, you can obtain replacement media of identical format by sending us the defective disk, proof of purchase, and a check or money order for $10, payable to SYBEX. Disclaimer SYBEX makes no warranty or representation, either expressed or implied, with respect to the Software or its contents, quality, performance, merchantability, or fitness for a particular purpose. In no event will SYBEX, its distributors, or dealers be liable to you or any other party for direct, indirect, special, incidental, consequential, or other damages arising out of the use of or inability to use the Software or its contents even if advised of the possibility of such damage. In the event that the Software includes an online update feature, SYBEX further disclaims any obligation to provide this feature for any specific duration other than the initial posting. The exclusion of implied warranties is not permitted by some states. Therefore, the above exclusion may not apply to you. This warranty provides you with specific legal rights; there may be other rights that you may have that vary from state to state. The pricing of the book with the Software by SYBEX reflects the allocation of risk and limitations on liability contained in this agreement of Terms and Conditions. Shareware Distribution This Software may contain various programs that are distributed as shareware. Copyright laws apply to both shareware and ordinary commercial software, and the copyright Owner(s) retains all rights. If you try a shareware program and continue using it, you are expected to register it. Individual programs differ on details of trial periods, registration, and payment. Please observe the requirements stated in appropriate files. Copy Protection The Software in whole or in part may or may not be copy-protected or encrypted. However, in all cases, reselling or redistributing these files without authorization is expressly forbidden except as specifically provided for by the Owner(s) therein. This book is dedicated to my loving wife— she is my first, best friend. Acknowledgments Thanks to my wife, Rebecca, for working with me to get this book completed during an exceptionally difficult time. I really don’t know what I would have done without her help in researching and compiling some of the information that appears in this book (especially the glossary). She also did a fine job of proofreading my rough draft and page-proofing the final result. Russ Mullen deserves thanks for his technical edit of this book. He greatly added to the accuracy and depth of the material you see here. I really appreciated the time he devoted to checking my code for accuracy—especially the last minute checks of test utilities. Russ also supplied some of the URLs you see in the book and other helpful tips and hints. Matt Wagner, my agent, deserves credit for helping me get the contract in the first place and taking care of all the details that most authors don’t really consider. I always appreciate his help. It’s good to know that someone wants to help. Finally, I would like to thank Denise Santoro Lincoln, Kylie Johnston, Judy Flynn, Dan Mummert, Kevin Ly, Lynnzee Elze, William Rodarmor, and the rest of the production staff at Sybex for their assistance in bringing this book to print. It’s always nice to work with such a great group of professionals. About the Author John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 55 books and over 200 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current books include a C# developer guide, a small business and home office networking guide, and several Windows XP user guides. His technical editing skills have helped over 29 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He’s also contributed articles to magazines like SQL Server Professional, Visual C++ Developer, and Visual Basic Developer. He’s currently the editor of the .NET electronic newsletter for Pinnacle Publishing. When John isn’t working at the computer, you can find him in his workshop. He’s an avid woodworker and candle maker. On any given afternoon, you can find him working at a lathe or putting the finishing touches on a bookcase. One of his newest craft projects is glycerin soap making, which comes in handy for gift baskets. You can reach John on the Internet at [email protected]. John is also setting up a Web site at http://www.mwt.net/~jmueller/. Feel free to look and make suggestions on how he can improve it. One of his current projects is creating book FAQ sheets that should help you find the book information you need much faster. Table of Contents Introduction.........................................................................................................................................................1 Overview..................................................................................................................................................1 Hunting for Treasure...................................................................................................................1 Hidden Nuggets..........................................................................................................................1 Who Should Read This Book?.................................................................................................................2 Tools Required.........................................................................................................................................2 Conventions Used in This Book..............................................................................................................2 Icons.........................................................................................................................................................3 Part I: An Overview of the Win32 API.............................................................................................................5 Chapter 1: Overcoming Holes in the .NET Framework.................................................................................6 Overview..................................................................................................................................................6 Why Access the Win32 API?..................................................................................................................6 A Case of Missing Functionality................................................................................................7 Win32 Function Types................................................................................................................8 Win32 Access Requirements....................................................................................................11 Win32 Access for C# Developers..........................................................................................................14 Understanding the Effects of Unsafe Code...............................................................................14 Understanding the Effects of Pointers......................................................................................15 Advantages for the C# Developer.............................................................................................16 Win32 Access for Visual Basic Developers..........................................................................................16 Understanding Visual Basic Limitations..................................................................................17 Advantages for the Visual Basic Developer.............................................................................18 Where Do You Go from Here?..............................................................................................................18 Chapter 2: Working with Win32 API Data....................................................................................................19 Overview................................................................................................................................................19 A Short View of Data............................................................................................................................19 Working with Variables.........................................................................................................................24 Converting Variables to C# Types............................................................................................25 Converting Variables to Visual Basic Types............................................................................25 Special Rules for COM.............................................................................................................26 Working with Data Structures...............................................................................................................28 Working with Pointers...........................................................................................................................32 Working with Enumerations..................................................................................................................32 Importing Resources..............................................................................................................................33 Understanding .NET Framework Functionality.......................................................................33 Using the IDE Features.............................................................................................................34 Working with the Win32 API...................................................................................................35 Where Do You Go from Here?..............................................................................................................36 Chapter 3: Accessing the Win32 API..............................................................................................................37 Overview................................................................................................................................................37 An Overview of the DLLs.....................................................................................................................37 Types of Win32 Access.........................................................................................................................38 Direct DLL Access...................................................................................................................39 C LIB Access............................................................................................................................40 A Direct DLL Access Example.............................................................................................................42 A C LIB Wrappers Access Example.....................................................................................................43 i Table of Contents Chapter 3: Accessing the Win32 API A C LIB Substitute Functions Example................................................................................................49 Interpreting Error and Result Values.....................................................................................................51 Helpful Win32 Programming Tools......................................................................................................52 Dependency Walker..................................................................................................................53 Error Lookup.............................................................................................................................59 Spy++........................................................................................................................................60 Where Do You Go from Here?..............................................................................................................64 Chapter 4: Processing Windows Messages.....................................................................................................65 Overview................................................................................................................................................65 Understanding the Windows Message Types........................................................................................65 Windows Message Handlers Found in the .NET Framework...............................................................68 An Overview of Windows Message Functions.....................................................................................70 SendMessage()..........................................................................................................................70 PostMessage(), PostThreadMessage, and PostQuitMessage().................................................72 SendNotifyMessage()...............................................................................................................77 SendMessageCallback()............................................................................................................78 GetMessage() and PeekMessage()............................................................................................78 RegisterWindowMessage().......................................................................................................79 Creating a Windows Message Handler Example...................................................................................79 Creating the Event....................................................................................................................79 Creating the Windows Message Loop Override.......................................................................80 Creating the Event Handler.......................................................................................................81 Demonstrating the Windows Message Handler........................................................................82 Developing for Thread Safety...................................................................................................83 Where Do You Go from Here?..............................................................................................................84 Chapter 5: Using Callback Functions.............................................................................................................85 Overview................................................................................................................................................85 What Is a Callback Function?................................................................................................................85 Using Callback Functions......................................................................................................................86 An Overview of Callback Function Prototypes........................................................................86 Implementing a Callback from the Managed Environment......................................................87 Implementing a Callback from a Wrapper DLL.......................................................................91 Enumerating Calendar Information Example........................................................................................95 Creating the Macro Wrapper DLL............................................................................................96 Creating the EnumCalendarInfoEx() Function Code...............................................................98 Demonstrating the Calendar Enumeration................................................................................99 Where Do You Go from Here?............................................................................................................101 Part II: Fixing Holes in Standard Applications...........................................................................................102 Chapter 6: Gaining Support for the Console Application..........................................................................103 Overview..............................................................................................................................................103 What Does the .NET Framework Provide?.........................................................................................103 Clearing the Screen Example...............................................................................................................104 Getting Console Information Examples...............................................................................................107 Working with Code Pages......................................................................................................107 Gaining Access to Console Mode Information.......................................................................109 ii Table of Contents Chapter 6: Gaining Support for the Console Application Working with Processes..........................................................................................................113 Changing the Environment Examples.................................................................................................114 Accessing the Environmental Strings.....................................................................................115 Using Cursors and Fonts.........................................................................................................118 Determining the Console Window Title.................................................................................122 Manipulating the Console Screen Buffer................................................................................123 Where Do You Go from Here?............................................................................................................126 Chapter 7: Accessing the Hardware Directly...............................................................................................128 Overview..............................................................................................................................................128 When is Direct Access Required?........................................................................................................128 Serial Port Access Examples...............................................................................................................130 Creating a Serial Stream.........................................................................................................130 Determining the Port Status....................................................................................................136 Parallel Port Access Examples.............................................................................................................143 Creating the Wrapper DLL.....................................................................................................144 Viewing the First Level of Printer Information......................................................................146 Viewing the Second Level of Printer Information..................................................................148 Where Do You Go from Here?............................................................................................................152 Chapter 8: Overcoming Security Issues.......................................................................................................154 Overview..............................................................................................................................................154 An Overview of Windows Security.....................................................................................................154 A Detailed View of the Windows Security API.....................................................................155 An Overview of the Functions................................................................................................161 Using the Access Control Editor..........................................................................................................165 Using the Security Configuration Editor.............................................................................................167 Understanding How .NET Role−Based Security Differs....................................................................169 Looking Up an Account SID Example................................................................................................172 Using the GetFileSecurity() Function Example...................................................................................175 Working with ACEs Example.............................................................................................................178 Where Do You Go from Here?............................................................................................................183 Part III: Fixing Advanced Win32 API Problems.........................................................................................185 Chapter 9: Accessing Windows XP Special Features..................................................................................186 Overview..............................................................................................................................................186 What’s New in Windows XP?.............................................................................................................186 Common Features...................................................................................................................187 Professional Edition Exclusive Features.................................................................................188 Determining the Operating System Version Example.........................................................................189 Working with Theme Support Example..............................................................................................195 Balloon Help Example.........................................................................................................................202 Using NUnit for Automated Testing....................................................................................................207 Where Do You Go from Here?............................................................................................................210 Chapter 10: Using Operating System Special Functions............................................................................211 Overview..............................................................................................................................................211 Accessing Status and Other Information.............................................................................................211 iii Table of Contents Chapter 10: Using Operating System Special Functions Learning How to Avoid Version Compatibility Problems.....................................................213 Determining System Power Status Example..........................................................................215 Creating an Application Shortcut Example.........................................................................................219 Shutting the System Down Remotely Example...................................................................................222 Obtaining Device Capabilities Example..............................................................................................225 File Compression.................................................................................................................................227 Using PC−Lint for C++ Development.................................................................................................228 Where Do You Go from Here?............................................................................................................230 Chapter 11: Accessing the Media Player......................................................................................................232 Overview..............................................................................................................................................232 A Quick Overview of the Media Player..............................................................................................232 Windows Media Player General Operation............................................................................233 Internet Content......................................................................................................................234 Using the Media Library.........................................................................................................235 Displaying a Configuration Dialog Example.......................................................................................236 Playing Sound Files Example..............................................................................................................240 Determining Device Capabilities............................................................................................241 Opening a File.........................................................................................................................244 Playing a File..........................................................................................................................251 Pausing and Stopping a File....................................................................................................253 Required Application Cleanup................................................................................................254 Using IconForge...................................................................................................................................254 Where Do You Go from Here?............................................................................................................257 Chapter 12: Working with Microsoft Management Console.....................................................................258 Overview..............................................................................................................................................258 Using GUIDGen..................................................................................................................................258 The Case for an MMC Snap−In...........................................................................................................260 Creating a Wrapper DLL.....................................................................................................................262 Creating an Interface...............................................................................................................263 Writing the MMC Wrapper DLL............................................................................................266 Developing the MMC Snap−In Example............................................................................................277 Developing an About Dialog..................................................................................................277 Working with IDataObject......................................................................................................279 Creating IComponent and IComponentData..........................................................................282 Testing the Snap−In within MMC..........................................................................................284 Where Do You Go from Here?............................................................................................................286 Part IV: Working with DirectX.....................................................................................................................288 Chapter 13: Creating DirectX Structures and Data Elements...................................................................289 Overview..............................................................................................................................................289 Using the DXDIAG Utility..................................................................................................................289 Learning about DirectX Compatibility...................................................................................290 Viewing the Drivers................................................................................................................292 Working with the DirectX Structures..................................................................................................294 An Overview of the Data Structures.......................................................................................294 Structure Conversion Essentials.............................................................................................297 iv

Description:
If you've begun programming using Microsoft's .NET Framework, you've discovered a lot of new and improved functionality. But, more than likely, you've also discovered a lot of missing functionality. Indeed, a third of the functions supported by the old Win32 API are not yet supported by .NET. Althou
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.