C++/CLI in Action
Nishant Sivakumar
  • April 2007
  • ISBN 9781932394818
  • 416 pages

“... a great resource, an outstanding job, a must-read...”

Ayman B. Shoukry, VC++ Team, Microsoft Corporation

C++/CLI in Action is a practical guide that will help you breathe new life into your legacy C++ programs. The book begins with a concise C++/CLI tutorial. It then quickly moves to the key themes of native/managed code interop and mixed-mode programming. You'll learn to take advantage of GUI frameworks like Windows Forms and WPF while keeping your native C++ business logic. The book also covers methods for accessing C# or VB.NET components and libraries.

Table of Contents detailed table of contents

preface

acknowledgments

about this book

Part 1 The C++/CLI Language

1. Introduction to C++/CLI

1.1. The role of C++/CLI

1.1.1. What C++/CLI can do for you

1.1.2. The rationale behind the new syntax

1.2. Hello World in C++/CLI

1.2.1. The /clr compiler option

1.2.2. Using VC++ 2005 to create a /clr application

1.3. Declaring CLR types

1.3.1. Class modifiers

1.3.2. CLI types and inheritance

1.4. Handles: the CLI equivalent to pointers

1.4.1. Syntax for using handles

1.4.2. Tracking references

1.5. Instantiating CLI classes

1.5.1. The gcnew operator

1.5.2. Constructors

1.5.3. Copy constructors

1.5.4. Assignment operators

1.6. Boxing and unboxing

1.6.1. Implicit boxing in the new syntax

1.6.2. Boxing and type-safety

1.6.3. Implementation at the MSIL level

1.6.4. Assigning null to a boxed value type

1.7. Summary

2. Getting into the CLI: properties, delegates and arrays

2.1. Properties

2.1.1. Scalar Properties

2.1.2. Indexed properties

2.2. Delegates and events

2.2.1. Delegates

2.2.2. Events

2.3. CLI Arrays

2.3.1. Basic CLI array concepts

2.3.2. Single-dimensional arrays

2.3.3. Multidimensional arrays

2.3.4. Jagged arrays

2.3.5. Arrays as function arguments

2.3.6. Returning arrays from functions

2.3.7. Parameter arrays

2.3.8. Using System::Array methods

2.3.9. Array covariance

2.3.10. Arrays of non-CLI objects

2.3.11. Directly accessing CLI arrays using native pointers

2.4. Summary

3. More C++/CLI: stack semantics, function overriding, and generic programming

3.1. Stack semantics and deterministic destruction

3.1.1. The new destructor and finalizer syntaxes

3.1.2. Stack semantics

3.1.3. Guidelines for using destructors and stack semantics

3.2. Function overriding

3.2.1. Explicit overriding

3.2.2. Renamed overriding

3.2.3. Multiple overriding

3.2.4. Sealed and abstract functions

3.3. Generics and managed templates

3.3.1. Why have parameterized types?

3.3.2. Generics syntax for classes and functions

3.3.3. Constraint mechanism

3.3.4. Issues with the constraint mechanism and simple types

3.3.5. Comparison with templates

3.3.6. Managed templates

3.4. Summary

Part 2 Mixing managed and native code

4. Introduction to mixed-mode programming

4.1. Using interior and pinning pointers

4.1.1. Interior pointers

4.1.2. Pinning pointers

4.2. Working with interop mechanisms

4.2.1. Accessing a managed library from native code

4.2.2. Accessing a native library from managed code

4.3. Using mixed types

4.3.1. Native types with managed members

4.3.2. Managed types with native members

4.4. Function pointers and delegates: bridging the gap

4.4.1. Using GetFunctionPointerForDelegate

4.4.2. Using GetDelegateForFunctionPointer

4.5. Summary

5. Interoping with native libraries from managed applications

5.1. Converting between managed and native types

5.1.1. Marshalling native strings

5.1.2. Marshalling arrays

5.1.3. Simulating a native static array with managed code

5.2. Double thunking in mixed-mode function calls

5.3. Wrapping a native API and exposing a CLI interface

5.3.1. Overview of the native API

5.3.2. Writing the CLI wrapper

5.4. Exposing an MFC extension DLL to .NET

5.4.1. Overview of the MFC extension DLL

5.4.2. Writing the managed regular MFC DLL wrapper

5.5. Accessing a COM object via a custom RCW

5.5.1. The COM object to interop with

5.5.2. Writing the custom RCW

5.5.3. Using the custom RCW

5.6. Writing a single mixed-mode DLL for both managed and native clients

5.6.1. Wrapping the System::Object class

5.6.2. Writing derived class wrappers

5.7. Summary

Part 3 Using managed frameworks from native applications

6. Interoping Windows Forms with MFC

6.1. A simple Windows Forms application

6.2. Hosting a Windows Forms control in an MFC dialog

6.3. Hosting a Windows Forms control as an MFC view

6.4. Giving your MFC apps an Office 2003 style UI

6.5. Using a Windows Forms control as an MFC dialog

6.6. Using an MFC control in a Windows Forms form

6.6.1. The custom MFC control

6.6.2. Hosting the MFC control from WinForms

6.6.3. Using the wrapped control from a WinForms app

6.7. Summary

7. Using C++/CLI to target Windows Presentation Foundation applications

7.1. What is WPF?

7.1.1. Overview of XAML

7.1.2. Anatomy of a simple WPF application

7.2. Using C++/CLI to write a WPF application

7.2.1. Creating a new C++/CLI Avalon project

7.2.2. Using procedural code

7.2.3. Dynamically loading XAML

7.2.4. Deriving from a class in a C# DLL

7.3. A brief look at some WPF Graphics features

7.3.1. Using brushes and shapes

7.3.2. Transformations

7.4. Hosting a WPF control in a native C++ application

7.4.1. Using a mixed-mode extension DLL

7.4.2. Using a mixed-mode application

7.5. Hosting a native control in a WPF application

7.6. Summary

8. Accessing the Windows Communication Foundation with C++/CLI

8.1. Hello World with the Windows Communication Foundation

8.2. Duplex communication in WCF

8.2.1. Creating the service

8.2.2. Creating the client

8.3. Migrating a native DCOM application to WCF

8.3.1. The example DCOM server

8.3.2. The native MFC client

8.3.3. Writing a WCF proxy service

8.3.4. Modifying the MFC client to use WCF

8.3.5. Writing a pure WCF service

8.3.6. Comparison of the two migration methods

8.4. Hosting a WCF service in an IIS server

8.5. Summary

Appendix A: A concise introduction to the .NET Framework

index

About the Technology

Developers initially welcomed Microsoft's Managed C++ for .NET, but the twisted syntax made it difficult to use. Its much-improved replacement, C++/CLI, now provides an effective bridge between the native and managed programming worlds. Using this technology, developers can combine existing C++ programs and .NET applications with little or no refactoring. Accessing .NET libraries like Windows Forms, WPF, and WCF from standard C++ is equally easy.

What's inside

  • Call C++ libraries from C# or VB.NET
  • C++ for WPF and WCF
  • Mixed-mode programming techniques
  • Move from Managed C++ to C++/CLI

About the reader

Written for readers with a working knowledge of C++.

About the author

Nishant Sivakumar has extensive experience with Visual C++, MFC, C#, and the .NET Framework. He has been a Microsoft Visual C++ MVP since 2002 and maintains an MVP tips and tricks website and a popular blog. Nish is in charge of several products for The Code Project. He also authored Extending MFC Applications with the .NET Framework. Nish lives with his wife in Toronto, Ontario.


Buy
  • combo $49.99 pBook + PDF
  • eBook $39.99 PDF only

FREE domestic shipping on three or more pBooks

“... explains the C++/CLI language and shows how it can be used.”

James Johnson,

“... a great reference ... a must-have in any C++ programmer’s bookshelf.”

Michael L. Taylor, C# MVP

“... absolutely the only book you’ll need to learn this powerful new .NET language ... a lifesaver... .”

Christian Graus, Code Project