Tải bản đầy đủ (.pdf) (961 trang)

pro visual c++-cli and the .net 2.0 platform (2006)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (23.06 MB, 961 trang )

Pro Visual C++/CLI and
the .NET 2.0 Platform
■■■
Stephen R. G. Fraser
Fraser_640-4Front.fm Page i Friday, November 18, 2005 3:42 PM
Pro Visual C++/CLI and the .NET 2.0 Platform
Copyright © 2006 by Stephen R.G. Fraser
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN: 1-59059-640-4
Library of Congress Cataloging-in-Publication data is available upon request.
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Ewan Buckingham
Technical Reviewer: Don Reamey
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore,
Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Project Managers: Laura Cheu, Richard Dal Porto
Copy Edit Manager: Nicole LeClerc
Copy Editors: Freelance Editorial Services, Ami Knox, Liz Welch
Assistant Production Director: Kari Brooks-Copony
Production Editor: Katie Stence
Compositor: Susan Gilnert
Proofreader: Elizabeth Berry
Indexer: John Collin
Artist: April Milne
Interior Designer: Van Winkle Design Group


Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail
, or
visit
.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA
94710. Phone 510-549-5930, fax 510-549-5939, e-mail
, or visit .
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at
in the Source Code section.
You will need to answer questions pertaining to this book in order to successfully download the code.
Fraser_640-4Front.fm Page ii Friday, November 18, 2005 3:42 PM
To my wife, Sarah, and my daughter, Shaina, my energy and happiness.
Fraser_640-4Front.fm Page iii Friday, November 18, 2005 3:42 PM
Fraser_640-4Front.fm Page iv Friday, November 18, 2005 3:42 PM
v
Contents at a Glance
Foreword by Stanley B. Lippman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv
PART 1 ■ ■ ■ The C++/CLI Language
■CHAPTER 1 Overview of the .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
■CHAPTER 2 C++/CLI Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

■CHAPTER 3 Object-Oriented C++/CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
■CHAPTER 4 Advanced C++/CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
PART 2 ■ ■ ■ .NET Framework Development
in C++/CLI
■CHAPTER 5 The .NET Framework Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . 193
■CHAPTER 6 Integrated XML Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
■CHAPTER 7 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
■CHAPTER 8 Input, Output, and Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
■CHAPTER 9 Basic Windows Forms Applications . . . . . . . . . . . . . . . . . . . . . . . . . . 309
■CHAPTER 10 Advanced Windows Forms Applications . . . . . . . . . . . . . . . . . . . . . . 377
■CHAPTER 11 Graphics Using GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
■CHAPTER 12 ADO.NET and Database Development . . . . . . . . . . . . . . . . . . . . . . . . 515
■CHAPTER 13 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
■CHAPTER 14 Windows Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
■CHAPTER 15 Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
■CHAPTER 16 Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Fraser_640-4Front.fm Page v Friday, November 18, 2005 3:42 PM
vi
■CONTENTS AT A GLANCE
■CHAPTER 17 Network Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
■CHAPTER 18 Assembly Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
■CHAPTER 19 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
PART 3 ■ ■ ■ Unsafe/Unmanaged C++/CLI
■CHAPTER 20 Unsafe C++ .NET Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
■CHAPTER 21 Advanced Unsafe or Unmanaged C++ .NET Programming . . . . . 825
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847
Fraser_640-4Front.fm Page vi Friday, November 18, 2005 3:42 PM
vii
Contents
Foreword by Stanley B. Lippman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv
PART 1 ■ ■ ■ The C++/CLI Language
■CHAPTER 1 Overview of the .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . 3
What Is .NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
What Is the .NET Framework? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
.NET Programming Advantages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
A Closer Look at the .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Common Language Runtime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Common Type System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Common Language Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
.NET Application Development Realms . . . . . . . . . . . . . . . . . . . . . . . 21
.NET Framework Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
■CHAPTER 2 C++/CLI Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The Obligatory “Hello World!” Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Variables and C++/CLI Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Declaring Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Variable Name Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Predefined Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
User-Defined Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Boxing and Unboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Type Modifiers and Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Fraser_640-4Front.fm Page vii Friday, November 18, 2005 3:42 PM

cafac74dd2d083cbec0906b66fcd56b1
viii
■CONTENTS
Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Numeric Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Comparisons and Logical Operators. . . . . . . . . . . . . . . . . . . . . . . . . . 62
Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Conditional Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Comma Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Address of, Reference, and Indirection Operators . . . . . . . . . . . . . . 66
Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Flow Control Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
if Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Looping Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
for each Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Skipping Loop Iterations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Breaking Out of a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Passing Arguments to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

Returning Values from a Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Passing Arguments to the main() Function . . . . . . . . . . . . . . . . . . . . 80
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
■CHAPTER 3 Object-Oriented C++/CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Object-Oriented Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Applying Objects to Software Development. . . . . . . . . . . . . . . . . . . . 87
Fraser_640-4Front.fm Page viii Friday, November 18, 2005 3:42 PM
■CONTENTS
ix
ref class/struct Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Declaring ref classes and structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Using the ref class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Member Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Member Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Member Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Nested ref classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Type Casting Between Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Abstract ref classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
■CHAPTER 4 Advanced C++/CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Preprocessor Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Defining Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Conditional Directives. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Include Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

Using Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Multifile Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Header Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Source Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Building Assemblies from Multifile Libraries . . . . . . . . . . . . . . . . . . 149
Assembly Referencing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Function Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Class Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Template Specialization and Partial Specialization. . . . . . . . . . . . . 159
Template Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Basics of Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
.NET Framework Base Class: Exception Classes . . . . . . . . . . . . . . 168
Throwing ApplicationExceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Rethrowing Exceptions and Nested try Blocks . . . . . . . . . . . . . . . . 172
Catching Multiple Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Catching All Previously Uncaught Exceptions . . . . . . . . . . . . . . . . . 176
Executing Code Regardless of an Exception . . . . . . . . . . . . . . . . . . 177
Fraser_640-4Front.fm Page ix Friday, November 18, 2005 3:42 PM
x
■CONTENTS
Delegates and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Delegates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
PART 2 ■ ■ ■ .NET Framework Development

in C++/CLI
■CHAPTER 5 The .NET Framework Class Library . . . . . . . . . . . . . . . . . . . . . . 193
Library Organizational Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Library Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
System::Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
System::Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
System::Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
System::Diagnostics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
System::DirectoryServices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
System::Drawing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
System::EnterpriseServices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
System::Globalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
System::IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
System::IO::Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
System::Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
System::Net. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
System::Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
System::Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
System::Runtime::InteropServices . . . . . . . . . . . . . . . . . . . . . . . . . . 208
System::Runtime::Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
System::Runtime::Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
System::Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
System::Threading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
System::Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
System::Windows::Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
System::Xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Microsoft::Win32. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Fraser_640-4Front.fm Page x Friday, November 18, 2005 3:42 PM

■CONTENTS
xi
■CHAPTER 6 Integrated XML Documentation . . . . . . . . . . . . . . . . . . . . . . . . . 217
The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
The Triple Slash Comment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Adding Triple Slash Comment to Your Code . . . . . . . . . . . . . . . . . . 219
Generating XML Documentation Files . . . . . . . . . . . . . . . . . . . . . . . 220
Viewing Integrated XML Documentation in IntelliSense. . . . . . . . . 222
Documentation Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Functionality Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Formatting Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Reference Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Documentation Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
■CHAPTER 7 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
IEnumerable, IEnumerator, and for each . . . . . . . . . . . . . . . . . . . . . . . . . 243
Standard Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
ArrayList. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
BitArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Hashtable and SortedList. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Queue and Stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Specialized Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
ListDictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
StringCollection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
StringDictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
NameValueCollection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Generic Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
List<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
LinkedList<T>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Queue<T> and Stack<T>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

Dictionary<K,V>, SortedDictionary<K,V> . . . . . . . . . . . . . . . . . . . . 273
Collection<T> and ReadOnlyCollection<T> . . . . . . . . . . . . . . . . . . 278
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
■CHAPTER 8 Input, Output, and Serialization . . . . . . . . . . . . . . . . . . . . . . . . . 279
File System Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Managing the File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Opening Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
The Open Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
I/O Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Fraser_640-4Front.fm Page xi Friday, November 18, 2005 3:42 PM
xii
■CONTENTS
Serialization of Managed Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Setting Up Classes for Serialization . . . . . . . . . . . . . . . . . . . . . . . . . 302
BinaryFormatter vs. SoapFormatter . . . . . . . . . . . . . . . . . . . . . . . . . 304
Serialization Using BinaryFormatter . . . . . . . . . . . . . . . . . . . . . . . . . 304
Serialization Using SoapFormatter . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
■CHAPTER 9 Basic Windows Forms Applications . . . . . . . . . . . . . . . . . . . . . 309
Win Forms Are Not MFC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
“Hello World!” Win Form Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Customizing the Form Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Handling Win Form Delegates and Events . . . . . . . . . . . . . . . . . . . . . . . . 319
Adding Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
The Label Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
The Button Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
The Text Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
The Selection Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

■CHAPTER 10 Advanced Windows Forms Applications . . . . . . . . . . . . . . . . 377
ImageList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
ListView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Container Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
TabControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
SplitContainer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Strips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
ToolStripContainer and ToolStripPanel. . . . . . . . . . . . . . . . . . . . . . . 402
ToolStripManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
ToolStrip. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
StatusStrip. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
MenuStrip and ContextMenuStrip. . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Bells and Whistles Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
PictureBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
MonthCalendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
ErrorProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
NotifyIcon. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Fraser_640-4Front.fm Page xii Friday, November 18, 2005 3:42 PM
■CONTENTS
xiii
Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Custom Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Common .NET Framework–Provided Dialog Boxes . . . . . . . . . . . . 442
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
■CHAPTER 11 Graphics Using GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
What Is GDI+? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
A Quick Look at the GDI+ Namespaces . . . . . . . . . . . . . . . . . . . . . . 446
“Hello World!” GDI+ Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447

OnPaint vs. PaintEventHandler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
The Graphics Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Graphics Class Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Disposing of Resources with Deterministic Cleanup . . . . . . . . . . . 455
Rendering Outside of the Paint Event. . . . . . . . . . . . . . . . . . . . . . . . 455
The Invalidate Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
GDI+ Coordinate Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Common Utility Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Point and PointF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Size and SizeF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Rectangle and RectangleF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Region . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Drawing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Custom Colors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Named Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Pens and Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Pens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Rendering Prebuilt Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Drawing Your Own Shapes and Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Advanced GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Scrollable Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Optimizing GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Double Buffering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Fraser_640-4Front.fm Page xiii Friday, November 18, 2005 3:42 PM
xiv

■CONTENTS
■CHAPTER 12 ADO.NET and Database Development . . . . . . . . . . . . . . . . . . . 515
What Is ADO.NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Building a Database with Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . 517
Creating a New Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Adding and Loading Tables and Views to a Database . . . . . . . . . . 519
Building Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Managed Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Connected ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Using Simple Connected ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Using Connected ADO.NET with Transactions. . . . . . . . . . . . . . . . . 539
Disconnected ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
The Core Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Creating a Table Manually in Code. . . . . . . . . . . . . . . . . . . . . . . . . . 548
Developing with Disconnected ADO.NET . . . . . . . . . . . . . . . . . . . . . 549
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
■CHAPTER 13 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
What Is XML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
The .NET Framework XML Implementations . . . . . . . . . . . . . . . . . . . . . . 560
Forward-Only Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Reading from an XML File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Validating an XML File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
Writing a New XML Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
Updating an Existing XML File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
Working with DOM Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
Reading a DOM Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Updating a DOM Tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Writing XmlNodes in a DOM Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Navigating with XPathNavigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Basic XPathNavigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594

XPathNavigator Using XPath Expressions . . . . . . . . . . . . . . . . . . . . 596
XML and ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Fraser_640-4Front.fm Page xiv Friday, November 18, 2005 3:42 PM
cafac74dd2d083cbec0906b66fcd56b1
■CONTENTS
xv
■CHAPTER 14 Windows Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
What Are Windows Services? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Architecture of Windows Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Service Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Service Control Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
Service Configuration Application. . . . . . . . . . . . . . . . . . . . . . . . . . . 608
The ServiceProcess Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Creating Windows Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Auto-generated Windows Service. . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Customizing the Windows Service . . . . . . . . . . . . . . . . . . . . . . . . . . 615
Installing and Uninstalling Windows Services . . . . . . . . . . . . . . . . . . . . . 621
Managing Windows Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Services Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
Custom Service Control Application . . . . . . . . . . . . . . . . . . . . . . . . . 626
Debugging Windows Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Attaching the Debugger to the Windows Service . . . . . . . . . . . . . . 631
A Special Main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
■CHAPTER 15 Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
What Are Web Services? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Components of a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
Communication Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
Description Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637

Discovery Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637
The Web Services Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
A Simple Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
Accessing a Web Service Using HTTP POST . . . . . . . . . . . . . . . . . . 646
Accessing a Web Service Using SOAP . . . . . . . . . . . . . . . . . . . . . . . 647
Debugging a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
Passing Data Using a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
Using Web Service GUI Designer Tool . . . . . . . . . . . . . . . . . . . . . . . 652
Returning a DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
Inserting, Updating, and Deleting Rows in a DataSet. . . . . . . . . . . 654
Authors DataSet Processing Web Service Client. . . . . . . . . . . . . . . 655
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
Fraser_640-4Front.fm Page xv Friday, November 18, 2005 3:42 PM
xvi
■CONTENTS
■CHAPTER 16 Multithreaded Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
What Is Multithreaded Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
Basic .NET Framework Class Library Threading . . . . . . . . . . . . . . . . . . . 662
Thread State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Thread Priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
Using Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
Starting Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
Getting a Thread to Sleep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Aborting Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Joining Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
Interrupting, Suspending, and Resuming Threads . . . . . . . . . . . . . 675
Using ThreadPools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
The ThreadStatic Attribute. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
The Interlocked Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682

The Monitor Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
The Mutex Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
The ReaderWriterLock Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
■CHAPTER 17 Network Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
The Network Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
Connection-Oriented Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696
The TCP Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696
The TCP Client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
Connectionless Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
UDP Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
UDP Client Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
Using Connect() with UDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
Socket Helper Classes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712
TcpListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712
TcpClient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
TCP Helper Class Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
UdpClient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Changing Socket Options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719
Fraser_640-4Front.fm Page xvi Friday, November 18, 2005 3:42 PM
■CONTENTS
xvii
Asynchronous Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
Accepting Connections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
Connecting to a Connection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
Disconnecting from a Connection. . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Sending a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724
Receiving a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724
Asynchronous TCP Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728

■CHAPTER 18 Assembly Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
Examining Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730
Dynamically Invoking or Late-Binding Objects . . . . . . . . . . . . . . . . 735
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
Creating a Custom Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Implementing a Custom Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
Using a Custom Attribute. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743
Shared Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
The Global Assembly Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
Adding Assemblies to the GAC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
The Shared Assembly’s Strong Name . . . . . . . . . . . . . . . . . . . . . . . 748
Re-signing an Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
Signcoded Digital Signature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
Versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749
No DLL Hell Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
Application Configuration Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
Creating Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756
Embedding Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
Accessing Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
Globalization and Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
The Globalization Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
The Localization Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
Building a Multicultural Windows Application . . . . . . . . . . . . . . . . . 767
Building a Multicultural Console Application . . . . . . . . . . . . . . . . . . 770
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
Fraser_640-4Front.fm Page xvii Friday, November 18, 2005 3:42 PM
xviii
■CONTENTS

■CHAPTER 19 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
The Security Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Role-Based Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776
Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776
Principal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Working with Identities and Principals . . . . . . . . . . . . . . . . . . . . . . . 778
Securing Your Code Using Roles. . . . . . . . . . . . . . . . . . . . . . . . . . . . 780
Code Access Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
Permissions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
Policy Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
Code Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785
Evidence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790
Securing Your Code Using CAS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
PART 3 ■ ■ ■ Unsafe/Unmanaged C++/CLI
■CHAPTER 20 Unsafe C++ .NET Programming . . . . . . . . . . . . . . . . . . . . . . . . . 805
What Is Unsafe Code? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
Why Do We Still Need Unsafe Code? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806
Creating Unsafe Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807
The Managed and Unmanaged #pragma Directives. . . . . . . . . . . . 807
Unmanaged Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
Unmanaged Classes/Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
Including the vcclr.h File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
■CHAPTER 21 Advanced Unsafe or Unmanaged
C++ .NET Programming
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
P/Invoke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
Calling DLLs without P/Invoke. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826

Using P/Invoke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
Data Marshaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
MarshalAsAttribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
Marshaling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
Marshaling Ref and Value Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 835
Fraser_640-4Front.fm Page xviii Friday, November 18, 2005 3:42 PM
■CONTENTS
xix
Accessing COM Components from .NET . . . . . . . . . . . . . . . . . . . . . . . . . 837
Interop Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
Creating the Interop Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
Invoking the Interop Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
Handling COM Object Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843
Late Binding a COM Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847
Fraser_640-4Front.fm Page xix Friday, November 18, 2005 3:42 PM
Fraser_640-4Front.fm Page xx Friday, November 18, 2005 3:42 PM
xxi
Foreword by Stanley B. Lippman
It is with great satisfaction that I introduce you to Stephen’s excellent new book, Pro Visual C++/CLI
and the .NET 2.0 Platform, the first detailed treatment of what has been standardized under ECMA as
C++/CLI. Of course, any text, no matter how excellent, is itself incomplete, like a three-walled room.
The fourth wall, in this case, is you, the reader. You complete the text by exercising the code samples,
poking around with them, and finally writing your own code. That’s really the only way to develop a
deep understanding of this stuff. But having an experienced guide to step you through the hazards of
any new language is priceless, and this is what Stephen’s text accomplishes. I cannot recommend it
too highly.
With Stephen’s indulgence, I would like to give you a short overview of the ideas behind the
language’s original design and place it in the context of the design and evolution of C++ itself. The

first question people ask is, “So what is C++/CLI?”
C++/CLI is a self-contained, component-based dynamic programming language that, like C#
or Java, is derived from C++. Unlike those languages, however, we have worked hard to integrate
C++/CLI into ISO-C++, using the historical model of evolving the C/C++ programming language to
support modern programming paradigms. Historically, one can say that C++/CLI is to C++ as C++ is
to C. More generally, one can view the evolution leading to C++/CLI in the following historical context:
• BCPL (Basic Computer Programming Language)
• B (Ken Thompson, original Unix work )
• C (Dennis Ritchie, adding type and control structure to B )
• C with Classes (~1979)
• C84 (~1984)
• Cfront, release E (~1984, to universities)
• Cfront, release 1.0 (1985, to the world )—20th birthday !!!
• Multiple/Virtual Inheritance Programming (~1988) (MI)
• Generic Programming (~1991) (Templates)
• ANSI C++/ISO-C++ (~1996)
• Dynamic Component Programming (~2005) (C++/CLI)
C++/CLI represents a tuple. The first term, C++, refers of course to the C++ programming
language invented by Bjarne Stroustrup at Bell Laboratories. It supports a static object model that is
optimized for the speed and size of its executables. It does not support runtime modification of the
program other than, of course, heap allocation. It allows unlimited access to the underlying machine, but
very little access to the types active in the running program, and no real access to the associated infra-
structure of that program.
The third term, CLI, refers to the Common Language Infrastructure, a multitiered architecture
supporting a dynamic component programming model. In many ways, this represents a complete
reversal of the C++ object model. A runtime software layer, the virtual execution system, runs between
the program and the underlying operating system. Access to the underlying machine is fairly
Fraser_640-4Front.fm Page xxi Friday, November 18, 2005 3:42 PM
cafac74dd2d083cbec0906b66fcd56b1
xxii

■FOREWORD BY STANLEY B. LIPPMAN
constrained. Access to the types active in the executing program and the associated program infra-
structure—both as discovery and construction—is supported.
The second term, slash (/), represents a binding between C++ and the CLI.
So, a first approximation of an answer as to “What is C++/CLI?” is to say that it is a binding of the
static C++ object model with the dynamic component object model of the CLI. In short, it is how
we do .NET programming using C++ rather than, say, C# or Visual Basic. Like C# and the CLI itself,
C++/CLI is undergoing standardization under ECMA (and eventually under ISO).
The common language runtime (CLR) is the implementation of the CLI that is platform specific
to the Windows operating system. Similarly, Visual C++ 2005 is our implementation of C++/CLI.
So, as a second approximation of an answer, I would say that C++/CLI integrates the .NET
programming model within C++ in the same way as, back at Bell Laboratories, we integrated generic
programming using templates within the then existing C++. In both cases, both your investment in
an existing C++ code base and in your existing C++ expertise are preserved. This was an essential
baseline requirement of the design of C++/CLI.
What Does Learning C++/CLI Involve?
There are three aspects in the design of a CLI language that hold across all languages: (1) a mapping
of language-level syntax to the underlying Common Type System (CTS); (2) the choice of a level of
detail to expose the underlying CLI infrastructure to the direct manipulation of the programmer;
and, (3) the choice of additional functionality to provide over that supported directly by the CLI.
A fourth element of designing a CLI extension to an existing language, such as C++ or Ada, requires a
fourth aspect: (4) that of integrating the managed and native type systems. We’ll briefly look at an
example of each in turn.
How Does C++/CLI Map to the CTS?
One aspect of programming C++/CLI is learning the underlying Common Type System, which
includes three general class types:
1. A polymorphic reference type that is used for all class inheritance
2. A nonpolymorphic value type that is used for implementing concrete types requiring
runtime efficiency such as the numeric types
3. An abstract interface type that is used for defining a set of operations common to a set of

either reference or value types that implement the interface
This design aspect, the mapping of the CTS to a set of built-in language types, is common across
all CLI languages, although of course the syntax varies in each CLI language. So, for example, in C#,
one writes
abstract class Shape { } // C#
to define an abstract Shape base class from which specific geometric objects are to be derived,
while in C++/CLI one writes
ref class Shape abstract { }; // C++/CLI
to indicate the exact same underlying CLI reference type. The two declarations are represented
exactly the same in the underlying CIL. Similarly, in C#, one writes
struct Point2D { } // C#
Fraser_640-4Front.fm Page xxii Friday, November 18, 2005 3:42 PM
■FOREWORD BY STANLEY B. LIPPMAN
xxiii
to define a concrete Point2D class, while in C++/CLI one writes
value class Point2D { }; // C++/CLI
The family of class types supported with C++/CLI represents an integration of the CTS with the
native facilities, of course, and that determined our choice of syntax. For example:
class native {};
value class V {};
ref class R {};
interface class I {};
The CTS also supports an enumeration class type that behaves somewhat differently from the
native enumeration, and we provide support for both of those as well:
enum native { fail, pass };
enum class CLIEnum : char { fail, pass};
Similarly, the CTS supports its own array type that again behaves differently from the native
array. And again we provide support for both:
int native[] = { 1,1,2,3,5,8 };
array<int>^ managed = { 1,1,2,3,5,8 };

It is not true to think of any one CLI language as closer to or more nearly a mapping to the under-
lying CTS than is another. Rather, each CLI language represents a view into the underlying CTS
object model.
What Level of Detail of the CLI Does
C++/CLI Expose?
The second design aspect reflects the level of detail of the underlying CLI implementation model to
incorporate into the language. How does one go about determining this? Essentially, we need to ask
these questions:
• What are the kinds of problems the language is likely to be tasked to solve? We must make sure
the language has the tools necessary to do this.
• What are the kinds of programmers the language is likely to attract?
Let’s look at an example: the issue of value types occurring on the managed heap. Value types
can find themselves on the managed heap in a number of circumstances:
•Implicit boxing
• We assign an object of a value type to an Object.
• We invoke a virtual method through a value type that is not overridden.
• When a value type serves as a member of a reference class type
• When a value type is being stored as the element type of a CLI array
Fraser_640-4Front.fm Page xxiii Friday, November 18, 2005 3:42 PM
xxiv
■FOREWORD BY STANLEY B. LIPPMAN
The design question a CLI language has to ask is, “Should we allow the programmer to manipulate
the address of a value type of this sort?”
What are the issues?
Any object located on the managed heap is subject to relocation during the compaction phase
of a sweep of the garbage collector. Any pointers to that object must be tracked and updated by the
runtime; the programmer has no way to manually track it herself. Therefore, if we were to allow the
programmer to take the address of a value type potentially resident on the managed heap, we would
need to introduce a tracking form of pointer in addition to the existing native pointer.
What are the trade-offs to consider? On the one hand, simplicity and safety.

• Directly introducing support in the language for one or a family of tracking pointers makes it
a more complicated language. By not supporting this, we expand the available pool of
programmers by requiring less sophistication.
• Allowing the programmer access to these ephemeral value types increases the possibility of
programmer error—she may purposely or by accident do bad things to the memory. By not
supporting this, we create a potentially safer runtime environment.
On the other hand, efficiency and flexibility.
• Each time we assign the same Object with a value type, a new boxing of the value occurs.
Allowing access to the boxed value type allows in-memory update, which may provide signif-
icant performance
• Without a form of tracking pointer, we cannot iterate over a CLI array using pointer arithmetic.
This means that the CLI array cannot participate in the STL iterator pattern and work with the
generic algorithms. Allowing access to the boxed value type allows significant design flexibility.
We chose in C++/CLI to provide a collection of addressing modes that handle value types on the
managed heap.
int ival = 1024;
// int^ provides a tracking handle for
// direct read/write access to a boxed value type
int^ boxedi = ival;
array<int>^ ia = gcnew array<int>{1,1,2,3,5,8};
// interior_ptr<T> supports indexing into the GC heap
interior_ptr<int> begin = &ia[0];
value struct smallInt { int m_ival; } si;
pin_ptr<int> ppi = &si.m_ival;
We imagine the C++/CLI programmer to be a sophisticated system programmer tasked with
providing infrastructure and organizationally critical applications that serve as the foundation over
which a business builds its future. She must address both scalability and performance concerns and
must therefore have a system-level view into the underlying CLI. The level of detail of a CLI language
reflects the face of its programmer.
Complexity is not in itself a negative quality. Human beings, for example, are more complicated

than single-cell bacteria, but that is, I think we all agree, not a bad thing. When the expression of a
simple concept is complicated, that is a bad thing. In C++/CLI, we have tried to provide an elegant
expression to a complex subject matter.
Fraser_640-4Front.fm Page xxiv Friday, November 18, 2005 3:42 PM

×