C++/CLI in Action C++/CLI in Action NISHANT SIVAKUMAR MANNING Greenwich (74° w. long.) For online information and ordering of this and other Manning books, please go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. Sound View Court 3B Fax: (609) 877-8256 Greenwich, CT 06830 Email: [email protected] ©2007 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books they publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. Copyeditor: Tiffany Taylor Sound View Court 3B Typesetter: Denis Dalinnik Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-932394-81-8 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 11 10 09 08 07 To my loving wife Smitha, who put up with my long hours of work on the book and constantly supplied me with coffee to keep me alert and awake. Without her presence, patience, and support, this book would not have been possible. brief contents PART 1 THE C++/CLI LANGUAGE ........................................ 1 1 ■ Introduction to C++/CLI 3 2 ■ Getting into the CLI: properties, delegates and arrays 46 3 ■ More C++/CLI: stack semantics, function overriding, and generic programming 86 PART 2 MIXING MANAGED AND NATIVE CODE .................. 131 4 ■ Introduction to mixed-mode programming 133 5 ■ Interoping with native libraries from managed applications 179 PART 3 USING MANAGED FRAMEWORKS FROM NATIVE APPLICATIONS .......................................... 229 6 ■ Interoping Windows Forms with MFC 231 7 ■ Using C++/CLI to target Windows Presentation Foundation applications 276 8 ■ Accessing the Windows Communication Foundation with C++/CLI 332 vii contents preface xv acknowledgments xvii about this book xix PART 1 THE C++/CLI LANGUAGE..................................... 1 1 Introduction to C++/CLI 3 1.1 The role of C++/CLI 4 What C++/CLI can do for you 6 ■ The rationale behind the new syntax 8 1.2 Hello World in C++/CLI 13 The /clr compiler option 15 ■ Using VC++ 2005 to create a /clr application 16 1.3 Declaring CLR types 18 Class modifiers 20 ■ CLI types and inheritance 22 1.4 Handles: the CLI equivalent to pointers 24 Syntax for using handles 24 ■ Tracking references 26 1.5 Instantiating CLI classes 28 The gcnew operator 28 ■ Constructors 31 Copy constructors 33 ■ Assignment operators 36 ix