Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed...

Preview:

Citation preview

Managed C++

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

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

Section 1: Overview

Looking back

Managed Execution

Looking back

Object-oriented programming

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

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

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

Managed Execution

Common LanguageRuntime(incl. CTS and CLS)

IL &Metadata

ClassLib

Class Loader

ManagednativeCode

Execution

Compiler

JIT Compiler

SourceCode

Section 2: New features in VC++.NET

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

Managed Extensions

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

Managed classesUnmanaged classes

Managed Extensions

C++ language extensions

Access to the .NET Framework

MFC / ATL .NET

WIN32 API

PInvoke

COM Interop

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

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

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

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

Section 3: Managed Extensions

Language interoperability Managed code

Garbage Collection

Exception Handling

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

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

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

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

GC and your destructor

Destructor ? Managed objects never have destructors

Finalization ! ... is called automatically

Close and Dispose Control over clean-up

__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 !

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

Section 4: Applications w/ VC++.NET

Use cases

Migration

MFC, ATL, and .NET Managed vs. unmanaged

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

Interaction I

Accessing .NET Framework from unmanaged code

.NET FrameworkUnmanaged world

ManagedExtensions

CCW

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]

Interaction II

Accessing a C++ object from the .NET Framework

.NET Framework

Unmanaged world

RCW

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]

Mixing

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

Problems with Inheritance Pointer

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

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

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

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!

Questions

Recommended