Visual C++.NET Developers Guide
Total Page:16
File Type:pdf, Size:1020Kb
Visual C++ .NET Developer’s Guide John Paul Mueller McGraw-Hill/Osborne 2600 Tenth Street Berkeley, California 94710 U.S.A. To arrange bulk purchase discounts for sales promotions, premiums, or fund-raisers, please contact McGraw-Hill/Osborne at the above address. For information on translations or book distributors outside the U.S.A., please see the International Contact Information page immediately following the index of this book. Copyright © 2002 by The McGraw-Hill Companies. All rights reserved. Printed in the United States of America. Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. 1234567890 CUS CUS 01987654321 Book p/n 0-07-213262-0 and CD p/n 0-07-213282-5 parts of ISBN 0-07-213281-7 Publisher Brandon A. Nordin Vice President & Associate Publisher Scott Rogers Acquisitions Editor Ann Sellers Project Editor Katie Conley Acquisitions Coordinator Tim Madrid Technical Editor Bill Burris Copy Editor Carl Wikander Proofreader Carol Burbo Indexer Irv Hershman Computer Designers Tara A. Davis, Lauren McCarthy Illustrators Michael Mueller, Greg Scott, Lyssa Wald Cover Illustration Eliot Bergman Cover Series Design Greg Scott This book was composed with Corel VENTURA™ Publisher. Information has been obtained by McGraw-Hill/Osborne from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill/Osborne, or others, McGraw-Hill/Osborne does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information. This book is dedicated to Micah Schlobohm. I appreciate her kindness, thoughtfulness, and desire to help. She’s the kind of friend that more people should have, but unfortunately don’t. About the Author John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 53 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 SOAP developer guide, a small business and home office networking guide, and a Windows 2000 Performance, Tuning, and Optimization book. His technical editing skills have helped over 25 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 is 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 making glycerin soap, 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. Acknowledgments Thanks to my wife, Rebecca, for working with me to get this book completed. 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. Bill Burris 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 appreciate the time he devoted to checking my code for accuracy. Bill 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 Ann Sellers, Timothy Madrid, Katie Conley, Carl Wikander, and the rest of the production staff at McGraw-Hill/Osborne for their assistance in bringing this book to print. I especially appreciate Ann’s patience when things didn’t go exceptionally well. Tim provided me with many thought-provoking messages and was always willing to talk with me when I needed help. Introduction Unless you’ve been hiding in a cave in a remote part of the earth, Microsoft has inundated you with news of .NET by now. Microsoft’s marketing machine is working overtime, as usual, to ensure you don’t miss their latest and greatest product. If you listen to the Microsoft hype, it seems that they expect everyone to drop billions (trillions?) of lines of code and adopt .NET tomorrow. What the hype doesn’t mention is that adopting .NET completely means starting from scratch. The real world is a different place than the fantasyland of Microsoft hype. In the real world, developers have to maintain existing code at the lowest possible cost and still produce new applications in record time. The task seems impossible when you have two completely different technologies to develop these applications. On the one hand, you have the realm of MFC and the Win32 API. On the other hand, you have the new .NET Framework. Which do you choose for a given task? Answering the question of which technology to use is one of the biggest problems this book will tackle. We’ll discuss how to use the old, the new, and, most importantly, the mixed environments of Visual C++ .NET. Knowing when .NET can actually help you create an application faster is the key to managing application development in an environment where you have two different architectures to consider. Microsoft’s .NET Framework is an exciting new technology for a developer looking for every productivity enhancement available. My purpose in writing this book is to help you balance the usefulness of this new technology against the need to maintain existing code. By the time you complete this book, you’ll not only know how to work with .NET to create some relatively complex applications, but you’ll better understand when .NET is a good choice for application development. What’s in This Book Visual C++ .NET Developer’s Guide contains a mix of theory and programming examples, with a heavy emphasis on the programming examples. You’ll find a mix of Win32, MFC, ATL, and .NET code within the book. In some cases, I’ll show you how to mix an existing technology with a new one—Visual C++ .NET is definitely a transitional language, one that will help you move from Win32 application development to .NET. Here’s a brief overview of the six parts of this book. Part I—Visual C++ in General This part of the book introduces you to some of the new features in Visual C++ .NET. We’ll also discuss some basic programming principles. You’ll learn how to create various types of applications. Most of the code in this part is unmanaged. However, this part includes some managed code examples that show how you’d create the same result as an unmanaged counterpart using the .NET Framework. You’ll also learn some advanced coding processes in this part of the book. We’ll discuss threads in Chapter 3, and I’ll show you how to create two types of threads. The graphics programming examples in Chapter 4 include both static graphics and animated graphics using GIFs. Chapter 5 will help you understand the intricacies of Active Directory, while Chapter 6 shows how to create components using both ATL and MFC. Part II—Visual C++ .NET and Database Management Database management is an essential part of every business today. Chapter 7 of this part tells you about the various technologies and indicates when you can best use them to your advantage. We also look at how to create and use DSNs. Chapter 8 is the unmanaged coding example for this part. You’ll learn how to use OLE-DB to create a basic database application that includes a form view, printing, and search routines. This section of the book also tells you how to get around certain problems with the Visual C++ .NET environment. For example, Visual C++ .NET doesn’t ship with all of the controls found in Visual Studio 6. Some of your applications might require these controls, so I show how to install them. Unfortunately, some controls won’t work even if you do install them, and I show you how to get around some of these problem areas. Chapter 9 is the managed coding example for this part. We discuss ODBC .NET in this chapter. Unfortunately, ODBC .NET wasn’t ready in time for the book, so you won’t see a coding example. We’ll create a managed example using ADO .NET that includes use of the new DataGrid control (among others). This section also shows how to create a print routine and other database application basics.