C++/CLI in Action (Paperback)
暫譯: C++/CLI 實戰 (平裝本)

Nishant Sivakumar

  • 出版商: Manning
  • 出版日期: 2007-04-01
  • 售價: $1,880
  • 貴賓價: 9.5$1,786
  • 語言: 英文
  • 頁數: 416
  • 裝訂: Paperback
  • ISBN: 1932394818
  • ISBN-13: 9781932394818
  • 相關分類: C++ 程式語言
  • 已絕版

買這商品的人也買了...

相關主題

商品描述

Description

Developers initially welcomed Microsofts 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.

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. Youll 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. Written for readers with a working knowledge of C++.

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

 

 

Table of 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
1.6 Boxing and unboxing 38
Implicit boxing in the new syntax 38
Boxing and type-safety 40
Implementation at the MSIL level 41
Assigning null to a boxed value type 43
1.7 Summary 45
2  Getting into the CLI: properties, delegates and arrays   46
2.1 Properties 47
Scalar Properties 48
Indexed properties 55
2.2 Delegates and events 58
Delegates 59
Events 64
2.3 CLI Arrays 68
Basic CLI array concepts 69
Single-dimensional arrays 70
Multidimensional arrays 71
Jagged arrays 72
Arrays as function arguments 74
Returning arrays from functions 75
Parameter arrays 76
Using System::Array methods 77
Array covariance 80
Arrays of non-CLI objects 81
Directly accessing CLI arrays using native pointers 83
2.4 Summary 84
3  More C++/CLI: stack semantics, function overriding, and generic programming   86
3.1 Stack semantics and deterministic destruction 87
The new destructor and finalizer syntaxes 88
Stack semantics 96
Guidelines for using destructors and stack semantics 101
3.2 Function overriding 102
Explicit overriding 103
Renamed overriding 104
Multiple overriding 105
Sealed and abstract functions 106
3.3 Generics and managed templates 108
Why have parameterized types? 108
Generics syntax for classes and functions 110
Constraint mechanism 113
Issues with the constraint mechanism and simple types 116
Comparison with templates 120
Managed templates 124
3.4 Summary 129

Part 2  Mixing managed and native code   131

4  Introduction to mixed-mode programming   133
4.1 Using interior and pinning pointers 135
Interior pointers 136
Pinning pointers 141
4.2 Working with interop mechanisms 147
Accessing a managed library from native code 148
Accessing a native library from managed code 156
4.3 Using mixed types 162
Native types with managed members 162
Managed types with native members 166
4.4 Function pointers and delegates: bridging the gap 173
Using GetFunctionPointerForDelegate 173
Using GetDelegateForFunctionPointer 175
4.5 Summary 177
5  Interoping with native libraries from managed applications   179
5.1 Converting between managed and native types 181
Marshalling native strings 181
Marshalling arrays 184
Simulating a native static array with managed code 185
5.2 Double thunking in mixed-mode function calls 186
5.3 Wrapping a native API and exposing a CLI interface 190
Overview of the native API 191
Writing the CLI wrapper 193
5.4 Exposing an MFC extension DLL to .NET 206
Overview of the MFC extension DLL 207
Writing the managed regular MFC DLL wrapper 208
5.5 Accessing a COM object via a custom RCW 212
The COM object to interop with 212
Writing the custom RCW 215
Using the custom RCW 218
5.6 Writing a single mixed-mode DLL for both managed and native clients 218
Wrapping the System::Object class 220
Writing derived class wrappers 223
5.7 Summary 227

Part 3  Using managed frameworks from native applications   229

6  Interoping Windows Forms with MFC   231
6.1 A simple Windows Forms application 233
6.2 Hosting a Windows Forms control in an MFC dialog 235
6.3 Hosting a Windows Forms control as an MFC view 239
6.4 Giving your MFC apps an Office 2003 style UI 249
6.5 Using a Windows Forms control as an MFC dialog 261
6.6 Using an MFC control in a Windows Forms form 267
The custom MFC control 268
Hosting the MFC control from WinForms 271
Using the wrapped control from a WinForms app 273
6.7 Summary 274
7  Using C++/CLI to target Windows Presentation Foundation applications   276
7.1 What is WPF? 278
Overview of XAML 280
Anatomy of a simple WPF application 283
7.2 Using C++/CLI to write a WPF application 288
Creating a new C++/CLI Avalon project 289
Using procedural code 289
Dynamically loading XAML 294
Deriving from a class in a C# DLL 297
7.3 A brief look at some WPF Graphics features 300
Using brushes and shapes 300
Transformations 304
7.4 Hosting a WPF control in a native C++ application 310
Using a mixed-mode extension DLL 310
Using a mixed-mode application 319
7.5 Hosting a native control in a WPF application 326
7.6 Summary 331
8  Accessing the Windows Communication Foundation with C++/CLI   332
8.1 Hello World with the Windows Communication Foundation 334
8.2 Duplex communication in WCF 338
Creating the service 340
Creating the client 342
8.3 Migrating a native DCOM application to WCF 344
The example DCOM server 346
The native MFC client 348
Writing a WCF proxy service 351
Modifying the MFC client to use WCF 356
Writing a pure WCF service 359
Comparison of the two migration methods 362
8.4 Hosting a WCF service in an IIS server 362
8.5 Summary 366

Appendix   A concise introduction to the .NET Framework   368


index 385

商品描述(中文翻譯)

**描述**

開發者最初對 Microsoft 的 Managed C++ for .NET 表示歡迎,但其扭曲的語法使得使用變得困難。其改進版 C++/CLI 現在提供了一個有效的橋樑,連接原生和管理編程世界。使用這項技術,開發者可以在幾乎不需要重構的情況下,將現有的 C++ 程式和 .NET 應用程式結合起來。從標準 C++ 訪問 .NET 庫,如 Windows Forms、WPF 和 WCF 也同樣簡單。

《C++/CLI 實戰》是一本實用指南,將幫助您為舊有的 C++ 程式注入新生命。本書以簡明的 C++/CLI 教程開始,然後迅速轉向原生/管理代碼互操作和混合模式編程的關鍵主題。您將學會如何利用 GUI 框架,如 Windows Forms 和 WPF,同時保留您的原生 C++ 業務邏輯。本書還涵蓋了訪問 C# 或 VB.NET 組件和庫的方法。適合具備 C++ 基礎知識的讀者。

- 從 C# 或 VB.NET 調用 C++ 庫
- 用於 WPF 和 WCF 的 C++
- 混合模式編程技術
- 從 Managed C++ 遷移到 C++/CLI

**目錄**

前言 xv
致謝 xvii
關於本書 xix

**第一部分 C++/CLI 語言 1**

1. C++/CLI 簡介 3
1.1 C++/CLI 的角色 4
- C++/CLI 能為您做什麼 6
- 新語法背後的理由 8
1.2 C++/CLI 的 Hello World 13
- /clr 編譯器選項 15
- 使用 VC++ 2005 創建 /clr 應用程式 16
1.3 宣告 CLR 類型 18
- 類修飾符 20
- CLI 類型和繼承 22
1.4 處理器:CLI 中指針的等價物 24
- 使用處理器的語法 24
- 追蹤引用 26
1.5 實例化 CLI 類 28
- gcnew 運算子 28
- 建構函數 31
- 複製建構函數 33
- 指派運算子 36
1.6 裝箱和拆箱 38
- 新語法中的隱式裝箱 38
- 裝箱和類型安全 40
- 在 MSIL 層級的實現 41
- 將 null 指派給裝箱的值類型 43
1.7 總結 45

2. 進入 CLI:屬性、委託和數組 46
2.1 屬性 47
- 標量屬性 48
- 索引屬性 55
2.2 委託和事件 58
- 委託 59
- 事件 64
2.3 CLI 數組 68
- 基本 CLI 數組概念 69
- 一維數組 70
- 多維數組 71
- 鋸齒狀數組 72
- 數組作為函數參數 74
- 從函數返回數組 75
- 參數數組 76
- 使用 System::Array 方法 77
- 數組協變 80
- 非 CLI 對象的數組 81
- 使用原生指針直接訪問 CLI 數組 83
2.4 總結 84

3. 更多 C++/CLI:堆疊語義、函數重寫和泛型編程 86
3.1 堆疊語義和確定性銷毀 87
- 新的解構函數和終結器語法 88
- 堆疊語義 96
- 使用解構函數和堆疊語義的指導方針 101
3.2 函數重寫 102
- 明確重寫 103
- 重命名重寫 104
- 多重重寫 105
- 封閉和抽象函數 106
3.3 泛型和管理模板 108
- 為什麼需要參數化類型? 108
- 類和函數的泛型語法 110
- 約束機制 113
- 約束機制和簡單類型的問題 116
- 與模板的比較 120
- 管理模板 124
3.4 總結 129

**第二部分 混合管理和原生代碼 131**

4. 混合模式編程簡介 133
4.1 使用內部指針和固定指針 135
- 內部指針 136
- 固定指針 141
4.2 使用互操作機制 147
- 從原生代碼訪問管理庫 148
- 從管理代碼訪問原生庫 156
4.3 使用混合類型 162
- 具有管理成員的原生類型 162
- 具有原生成員的管理類型 166
4.4 函數指針和委託:彌合差距 173
- 使用 GetFunctionPointerForDelegate 173
- 使用 GetDelegateForFunctionPointer 175
4.5 總結 177

5. 從管理應用程式與原生庫互操作 179
5.1 在管理和原生類型之間轉換 181
- 原生字符串的編組 181
- 數組的編組 184
- 使用管理代碼模擬原生靜態數組 185
5.2 混合模式函數調用中的雙重 thunking 186
5.3 封裝原生 API 並暴露 CLI 接口 190
- 原生 API 概述 191
- 編寫 CLI 包裝器 193
5.4 將 MFC 擴展 DLL 暴露給 .NET 206
- MFC 擴展 DLL 概述 207
- 編寫管理的常規 MFC DLL 包裝器 208