Download presentation
Presentation is loading. Please wait.
Published byLesley Griffin Modified over 9 years ago
1
Managed C++
2
Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual Studio.NET and the.NET Framework
3
Contents Section 1: Overview Section 2: New features in Visual C++.NET Section 3: Managed extensions for C++ Section 4: Writing managed C++ applications Questions
4
Section 1: Overview Looking back Managed Execution
5
Looking back Object-oriented programming COM+ Most successful component model in history Only viable commercial component platform Enables cross-organization integration and reuse
6
Looking back Microsoft Foundation Classes (MFC) C++ class libraries Encapsulation of the Windows API Defacto standard for Windows development Active Template Library (ATL) C++ templates for COM development
7
What’s wrong with that? COM shows its age DCOM does not function well over the Internet More component based systems, more "DLL Hell" Difficult to implement Not truly language agnostic Makes some assumptions of binary layout "Least-common denominator" type system only Continuously growing API set Inconsistencies in programming model
8
Managed Execution Common Language Runtime (incl. CTS and CLS) IL & Metadata Class Lib Class Loader Managed native Code Execution Compiler JIT Compiler Source Code
9
Section 2: New features in VC++.NET Visual C++.NET Compiler options Linker options New Keywords and types Managed Extensions
10
Visual C++.NET New features Compiler and linker options New preprocessor features Keywords and types Attribute based programming Easy COM programming Event Handling Managed Extensions
11
Managed classesUnmanaged classes Managed Extensions C++ language extensions Access to the.NET Framework MFC / ATL.NET WIN32 API PInvoke COM Interop
12
Compiler options New options in all categories Some samples Debugging Code generation Misc /GS – generate security check /RTC – enables runtime check /Wp64 – detects 64bit portability problems /GL – enables complete program optimization /Wall, /w – enables/disables all warnings
13
/CLR Common Language Runtime (CLR) compilation Creating managed code Application must include Enables Managed Extensions For all functions But not for classes, interfaces, or structs (see __gc) #using using namespace System;
14
Linker options Samples Unmanaged development Managed Extensions /TSAWARE – create terminal server aware application /LTCG – link-time code generation, combination of /GL and /c, enables complete programming optimization /ASSEMBLYMODULE – add an IL module to an assembly /ASSEMBLYRESOURCE – link to a managed resource /NOASSEMBLY – create an IL module
15
Keywords and types New types samples wchar_t - is now a native data type __m64, __m128 - Microsoft specific data types for use with MMX and SSE Keywords (Managed Extensions) __delegate - declares a reference to a method __property - declares a property for a managed class __gc - declares a garbage collected class __event, __abstract, __interface, __value,...
16
Section 3: Managed Extensions Language interoperability Managed code Garbage Collection Exception Handling
17
Language interoperability Managed class types __gc Garbage collected classes __value Value classes __gc __interface Managed interface classes Managed Arrays __gc keyword String literals One string for all! Int MyIntArray __gc[] = new int __gc[100];
18
Interoperability Delegates Object-oriented function pointer __delegate keyword Single- and multicast delegate Properties of managed objects Pseudo data member __property keyword Scalar and indexed properties
19
Garbage Collection Automatic memory management Memory is allocated and freed by the CLR Managed Heap Managed data Access through managed code Declare objects as managed/unmanaged __gc and __nogc keywords #pragma managed/unmanaged
20
GC – in depth Roots Each application has a list of references Used objects in managed heap Objects which are set to null Problems with the collection point of time Sometimes resources have to be freed immediately
21
GC and your destructor Destructor ? Managed objects never have destructors Finalization !... is called automatically Close and Dispose Control over clean-up
22
__gc sample #using using namespace System; __gc class GcInt { public: int ReturnInt() { return 5; } }; int callGcInt() { GcInt* pgci; pgci = new GcInt; return pgci->ReturnInt(); } void main() { Console::WriteLine( callGcInt() ); } No delete !
23
Handling exceptions Structured (SEH) and C++ exception handling Throwing exceptions Pointer to managed objects Try/catch block Like unmanaged code __finally keyword Clean up resources Always (!) executed
24
Section 4: Applications w/ VC++.NET Use cases Migration MFC, ATL, and.NET Managed vs. unmanaged
25
Operational areas Interaction Unmanaged C++ and.NET code Mixing Managed and unmanaged code within one executable Migration from unmanaged C++ code to the.NET Framework
26
Interaction I Accessing.NET Framework from unmanaged code.NET Framework Unmanaged world Managed Extensions CCW
27
The Type Library Exporter Command line tool Takes CLR assembly to generate type library Input:.NET assembly Output:.tlb TlbExp assembly [/out:file] [/silent] [/verbose] [/nologo]
28
Interaction II Accessing a C++ object from the.NET Framework.NET Framework Unmanaged world RCW
29
Command line tool Convert COM type definitions into.NET metadata Input:.tlb,.dll,.odl,.ocx, or.exe Output: metadata DLL The Type Library Importer TlbImp tlblib [/out:file] [/publickey:file] [keyfile:file] [keycontainer:file] [/silent] [/verbose] [/nologo] [/unsafe]
30
Mixing Managed and unmanaged code in one executable Seamless interoperation Fine grained control Problems with Inheritance Pointer
31
Migration Two ways Directly: step-by-step migration Mixing managed and unmanaged code Indirectly: build a wrapper (see interaction) Make your code callable from the.NET Framework
32
Managed vs. unmanaged Code Unmanaged compiler generates executable x86 code Managed 2-step-compilation (IL and JIT) Data Unmanaged Layout by compiler, no garbage collection Managed Layout by CLR, garbage collection
33
ATL, MFC and.NET MFC MFC is not legacy Rich client development WinForms covers MFC GUI ATL New features, like Security classes Simple object creation ATL Server Web Applications and Web Services
34
Summary First insight: Visual C++.NET Managed Code Managed Extensions for C++ Migration and interoperation of managed and unmanaged code Perspectives: Not all code will be managed MFC and ATL are not dead!
35
Questions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.