35
Managed C++

Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Embed Size (px)

Citation preview

Page 1: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Managed C++

Page 2: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 3: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 4: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Section 1: Overview

Looking back

Managed Execution

Page 5: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Looking back

Object-oriented programming

COM+ Most successful component model in history Only viable commercial component platform Enables cross-organization integration and reuse

Page 6: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 7: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 8: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Managed Execution

Common LanguageRuntime(incl. CTS and CLS)

IL &Metadata

ClassLib

Class Loader

ManagednativeCode

Execution

Compiler

JIT Compiler

SourceCode

Page 9: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Section 2: New features in VC++.NET

Visual C++.NET Compiler options Linker options New Keywords and types

Managed Extensions

Page 10: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 11: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Managed classesUnmanaged classes

Managed Extensions

C++ language extensions

Access to the .NET Framework

MFC / ATL .NET

WIN32 API

PInvoke

COM Interop

Page 12: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 13: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

/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 <mscorlib.dll>using namespace System;

Page 14: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 15: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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, ...

Page 16: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Section 3: Managed Extensions

Language interoperability Managed code

Garbage Collection

Exception Handling

Page 17: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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];

Page 18: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 19: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 20: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 21: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

GC and your destructor

Destructor ? Managed objects never have destructors

Finalization ! ... is called automatically

Close and Dispose Control over clean-up

Page 22: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

__gc sample

#using <mscorlib.dll>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 !

Page 23: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 24: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Section 4: Applications w/ VC++.NET

Use cases

Migration

MFC, ATL, and .NET Managed vs. unmanaged

Page 25: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 26: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Interaction I

Accessing .NET Framework from unmanaged code

.NET FrameworkUnmanaged world

ManagedExtensions

CCW

Page 27: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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]

Page 28: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Interaction II

Accessing a C++ object from the .NET Framework

.NET Framework

Unmanaged world

RCW

Page 29: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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]

Page 30: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Mixing

Managed and unmanaged code in one executable Seamless interoperation Fine grained control

Problems with Inheritance Pointer

Page 31: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 32: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 33: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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

Page 34: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

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!

Page 35: Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual

Questions