C++ - Apress - Expert C++ CLI;.NET for Visual C++ Programmers - Apr 2007.pdf

(2619 KB) Pobierz
25154423 UNPDF
Expert C++/CLI:
.NET for Visual C++
Programmers
Marcus Heege
25154423.003.png 25154423.004.png
Expert C++/CLI: .NET for Visual C++ Programmers
Copyright © 2007 by Marcus Heege
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-13: 978-1-59059-756-9
ISBN-10: 1-59059-756-7
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: James Huddleston
Technical Reviewer: Stanley Lippman
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick,
Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Jeff Pepper, Paul Sarknas,
Dominic Shakeshaft, Jim Sumser, Matt Wade
Project Manager: Elizabeth Seymour
Copy Edit Manager: Nicole Flores
Copy Editor: Damon Larson
Assistant Production Director: Kari Brooks-Copony
Production Editor: Lori Bring
Compositor: Gina Rexrode
Proofreader: Patrick Vincent
Indexer: Brenda Miller
Artist: April Milne
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 orders-ny@springer-sbm.com , or
visit http://www.springeronline.com .
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 info@apress.com , or visit http://www.apress.com .
The information in this book is distributed on an “as is” basis, without warranty. Although every precau-
tion 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 http://www.apress.com in the Source Code/
Download section.
25154423.005.png
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
CHAPTER 1
Why C++/CLI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
CHAPTER 2
Managed Types, Instances, and Memory . . . . . . . . . . . . . . . . . . . . . . . . 11
CHAPTER 3
Writing Simple .NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
CHAPTER 4
Assemblies, Metadata, and Runtime Services . . . . . . . . . . . . . . . . . . 49
CHAPTER 5
Defining Managed Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
CHAPTER 6
Special Member Functions and Resource Management . . . . . . . . 117
CHAPTER 7
Using C++/CLI to Extend Visual C++ Projects
with Managed Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
CHAPTER 8
Mixing the Managed and the Native Type System . . . . . . . . . . . . . . 173
CHAPTER 9
Managed-Unmanaged Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
CHAPTER 10 Wrapping Native Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
CHAPTER 11 Reliable Resource Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
CHAPTER 12 Assembly Startup and Runtime Initialization . . . . . . . . . . . . . . . . . . . 279
APPENDIX A Programmatically Updating the .NET Security Policy . . . . . . . . . . . 303
APPENDIX B Measuring the Performance of Thunks . . . . . . . . . . . . . . . . . . . . . . . . 307
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
iii
25154423.006.png
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
CHAPTER 1
Why C++/CLI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Extending C++ with .NET Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is .NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
What Is C++/CLI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Building C++/CLI Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Object File Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Interaction Between Managed and Unmanaged Code . . . . . . . . . . . . . . . . . 6
DLLs with Managed Entry Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Compilation Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Wrapping Native Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
CHAPTER 2 Managed Types, Instances, and Memory . . . . . . . . . . . . . . . . . . 11
System::Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Custom CTS Type Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Managed Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Managed Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Tracking Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Values and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Value Types and Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Boxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Unboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
System::String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Managed Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Managed Array Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Iterating Through an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Managed Arrays of Tracking Handles . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
v
25154423.001.png
vi
CONTENTS
CHAPTER 3 Writing Simple .NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Referencing Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Assembly References in Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . 32
Assemblies and Type Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Avoiding Naming Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Stream-Based IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Text IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Reading and Writing Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Managed Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
try...finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Web Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Casting Managed Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Managed Debug Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Configuration Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
CHAPTER 4 Assemblies, Metadata, and Runtime Services . . . . . . . . . . . . 49
Assemblies and Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Assembly Manifests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Metadata APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Assembly Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Assembly Loading and Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
The Global Assembly Cache (GAC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Version Redirections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Manual Assembly Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Consuming Metadata for Types and Type Members at Runtime . . . . . . . . 59
Dynamic Type Instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Runtime Information About Type Members . . . . . . . . . . . . . . . . . . . . . 63
Dynamic Member Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Access to Private Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
System::Runtime::Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
25154423.002.png
Zgłoś jeśli naruszono regulamin