Expert Visual C++/CLI: .NET for Visual C++ Programmers

Marcus Heege

C++/CLI is Microsoft’s latest extension to C++ that targets the heart of .NET 2.0, the common language runtime. Expert Visual C++/CLI is written by visual C++ MVP Marcus Heege, who examines the core of the C++/CLI language.

  • Published on Jun 11 2007
  • Pages 352
  • Downloaded 47
  • Type PDF
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 8.1k
  • 0


C++/CLI is Microsoft’s latest extension to C++ that targets the heart of .NET 2.0, the common language runtime. Expert Visual C++/CLI is written by visual C++ MVP Marcus Heege, who examines the core of the C++/CLI language. He explains both how the language elements work and how Microsoft intends them to be used.

Even if you’re new to C++/CLI but are planning to migrate to it from another language, this book will ground you in the core language elements and give you the confidence to explore further and migrate effectively. It provides concise, yet in-depth coverage of all major C++/CLI features: short code examples succinctly illustrate syntax and concepts, and more elaborate examples show how C++/CLI should be used.

Author Information

Marcus Heege has over 20 years of experience developing software for Microsoft languages and platforms, since MS-DOS 1.0. He is a course author and instructor for DevelopMentor, where he’s most recently developed their “Essential C++/CLI—Building and Migrating Applications and Components with C++/CLI” seminar. He also serves as a troubleshooter and mentor for many professional software developers.

Marcus blogs about C++ and .NET topics at and has written dozens of articles for a wide variety of Internet sites. Marcus is an MVP for Visual C++ and has been a Microsoft Certified Solution Developer and Microsoft Certified Trainer since 1997.

What is in this book



About the Author

About the Technical Reviewer



CHAPTER 1 - Why C++/CLI?


Extending C++ with NET Features                                 

What Is NET?                                                   

What Is C++/CLI?                                                 

Building C++/CLI Applications                                     

Object File Compatibility                                          

Interaction Between Managed and Unmanaged Code                 

DLLs with Managed Entry Points                                   

Compilation Models                                              

Wrapping Native Libraries                                         



CHAPTER 2 - Managed Types, Instances, and Memory



Primitive Types                                                 

Custom CTS Type Definitions                                     

Managed Memory                                               

Managed Heap                                             

Tracking Handles                                           

Values and Objects                                              

Value Types and Reference Types                                 




Managed Arrays                                                 

Managed Array Initialization                                 

Iterating Through an Array                                   

Managed Arrays of Tracking Handles                          



CHAPTER 3 - Writing Simple NET Applications


Referencing Assemblies                                          

Assembly References in Visual Studio                         

Assemblies and Type Identity                                      

Avoiding Naming Conflicts                                        

Command-Line Arguments                                       

Stream-Based IO                                                

Text IO                                                     

Reading and Writing Binary Data                             

Managed Exception Handling                                     


Web Requests                                                   

Casting Managed Types                                          

Managed Debug Helpers                                         

Configuration Files                                              



CHAPTER 4 - Assemblies, Metadata, and Runtime Services


Assemblies and Metadata                                        

Assembly Manifests                                        

Metadata APIs                                                   

Assembly Identity                                               

Assembly Loading and Deployment                               

The Global Assembly Cache (GAC)                            

Version Redirections                                         

Manual Assembly Loading                                   

Consuming Metadata for Types and Type Members at Runtime       

Dynamic Type Instantiation                                  

Runtime Information About Type Members                      

Dynamic Member Access                                   

Access to Private Members                                  





CHAPTER 5 - Defining Managed Types


Type Visibility                                                   

Friend Assemblies                                          

Value Type Definitions                                            

Managed Enums                                                

Type Member Visibility                                           

Visibility and Naming Conventions                            


Default Arguments Are Not Supported                         

const Methods Are Not Supported                            


Bye-Bye const                                              

Type Initialization                                                


Inheritance and Versioning                                  

Virtual Functions                                            

Overriding Interface Members                                    

Interfaces Are Immutable                                        

Has-A Relationships                                             


Handling Events of a Component                            

Defining Custom Component Classes                             

Defining Custom Events                                         

Event Handler Delegates                                    

Nontrivial Events                                          



CHAPTER 6 - Special Member Functions and Resource Management


Object Construction                                             

Virtual Functions Called on an Object During Construction Time

Order of Calls to Dependent Constructors                     

Object Destruction                                             

Disposing Objects                                          

Cleanup for Automatic Variables                                  

Obtaining a Tracking Handle from an Implicitly Dereferenced Variable

Automatic Disposal of Fields                                     

Access to Disposed Objects                                      

Requirements for Destructors of Managed Types                


auto_handle and cleanup                                   

Copy Constructors and Assignment Operators                     



CHAPTER 7 - Using C++/CLI to Extend Visual C++ Projects with Managed Code


Up-Front Considerations                                        

Which Compilation Model Should You Choose?                     

Load-Time Dependencies to Other DLLs                      

Why Should You Use /clr:pure at All?                         

Compilation Models and NET Security                       

Adapting the Security Policy for Assemblies Using C++/CLI Interoperability

Compatibility with Other Compiler Switches                       

Managed Compilation and the C/C++ Runtime Library         

Managed Compilation and Exception Handling (/EHa)          

Features Incompatible with C++/CLI                         

Reviewing Compilation Models                                   

Step by Step                                                   

Step 1: Modifying Settings at the Project Level                

Step 2: Creating a Second Precompiled Header               

Step 3: Building and Testing                                

Step 4: Adding Additional Source Files Compiled with /clr       

Step 5: Compiling Existing Files with /clr Only If Necessary     

Handling Exceptions Across Managed-Unmanaged Boundaries      

Mapping SEH Exceptions to NET Exceptions                  

Catching C++ Exceptions                                  

Catching Managed Exceptions in Native Code                 

General Hints for Mixed Compilation                               

Avoid #pragma (un)managed                               

Automatic Choice of Compilation Model: Avoid Warning 4793!  

Predefined Macros for Compilation Models                   

Compilation Models and Templates                           



CHAPTER 8 - Mixing the Managed and the Native Type System


Using Native Types in Managed Code                             

Using C Structures in Managed Code                              

Using C++ Classes in Managed Code                             

String Literals                                                 

Passing Managed Memory to a Native Function                    

Converting Between Managed and Native Strings                   

Mixing the Type Systems When Defining Data Members             

Referring to Managed Objects in C++ Classes                     

Other Uses of gcroot and auto_gcroot                        

General Hints Regarding gcroot and auto_gcroot               

Reducing the Overhead of gcroot and auto_gcroot             

Handling Events in Native Classes                                

Internals of the Delegate Map                               



CHAPTER 9 - Managed-Unmanaged Transitions


Interoperability, Metadata, and Thunks                            

Calling Managed Functions from Unmanaged Code                 

Interoperability Metadata for Unmanaged-to- Managed Transitions

Default Calling Conventions                                 

Implicit Optimizations of Native-to-Managed Transitions            

Native and Managed Callers                                

Managed Callers Only                                      

Calling Native Functions from Managed Code                      

Calling Local Native Functions from Managed Code            

Calling Native Functions Imported from DLLs                 

Calling C++ Classes Across Managed-Unmanaged Boundaries      

Passing Native-Managed Boundaries with Function Pointers         

Passing Native-Managed Boundaries with Virtual Function Calls      

Virtual Functions and Double Thunking                       

Performance of Thunks                                         

Optimizing Thunks                                             


Be Aware of Implicit GetLastError-Caching Optimizations       

Generic Thunks and P/Invoke Type Marshaling                     



CHAPTER 10 - Wrapping Native Libraries


Up-Front Considerations                                        

Should You Implement Wrapper Types in a Separate DLL or Integrate Them into the Native Library Project?

Which Features of the Native Library Should Be Exposed?      

Language Interoperability                                       

Wrapping C++ Classes                                         

Mapping Native Types to CLS-Compliant Types                 

Mapping C++ Exceptions to Managed Exceptions             

Mapping Arguments of Managed Array Types to Native Types   

Mapping Other Non-Primitive Arguments                     

Supporting Inheritance and Virtual Functions                  

General Recommendations                                      

Simplify Wrappers Right from the Start                       

Be Aware of the NET Mentality                             



CHAPTER 11 - Reliable Resource Management


Wrapping Native Resources                                     

Limits of IDisposable::Dispose                                   

Garbage Collection and Last-Chance Cleanup                      

What Should a Finalizer Clean Up?                               

Finalization Issue 1: Timing                                      

When Is a Reference on the Stack a Root Reference?          

Reproducing the Finalization Timing Problem                 

Preventing Objects from Being Finalized During P/Invoke Calls  

Finalization Issue 2: Graph Promotion                             

Prioritizing Finalization                                          

Finalization Issue 3: Asynchronous Exceptions                     








CHAPTER 12 - Assembly Startup and Runtime Initialization


Application Startup                                             

CLR Startup                                               

Loading the Application Assembly                                

CRT Initialization in /clr[:pure] Assemblies                         

Linking the CRT in Mixed-Code Assemblies                        

The Module Constructor                                        

The Managed Entry Point                                        

DLL Startup                                                   

CRT Initialization in /clr DLLs                                

Custom Startup Logic and Load-Time Deadlocks              

Initialization of Global and Static Variables                    

DLLs and Module Constructors                              

Initialization Timing Problems                               

CRT Initialization in /clr:pure DLLs                           


APPENDIX A - Programmatically Updating the NET Security Policy


APPENDIX B  - Measuring the Performance of Thunks




Mindcracker Network accepts no liability for providing the customer with the hard copy of the book purchased. Mindcracker is not responsible for any dispute relating to the above. All eBooks listed in this section are in "PDF" Format. Contact [email protected] for further assistant.