C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

A first look at C++/CLI(C++的管理扩展)

Introduction

When Microsoft brought out the Managed Extensions to C++ with VS.NET 7, C++ programmers accepted it with mixed reactions. While most people were happy that they could continue using C++, nearly everyone was unhappy with the ugly and twisted syntax offered by Managed C++. Microsoft obviously took the feedback it got very seriously and they decided that the MC++ syntax wasn't going to be much of a success.

On October 6th 2003, the ECMA announced the creation of a new task group to oversee development of a standard set of language extensions to create a binding between the ISO standard C++ programming language and Common Language Infrastructure (CLI). It was also made known that this new set of language extensions will be known as the C++/CLI standard, which will be supported by the VC++ compiler starting with the Whidbey release (VS.NET 2005).

Problems with the old syntax

  • Ugly and twisted syntax and grammar - All those double underscores weren't exactly pleasing to the eye.
  • Second class CLI support - Compared to C# and VB.NET, MC++ used contorted workarounds to provide CLI support, for e.g. it didn't have a for-each construct to enumerate .NET collections.
  • Poor integration of C++ and .NET - You couldn’t use C++ features like templates on CLI types and you couldn’t use CLI features like garbage collection on C++ types.
  • Confusing pointer usage - Both unmanaged C++ pointers and managed reference pointers used the same * based syntax which was quite confusing because __gc pointers were totally different in nature and behavior from unmanaged pointers.
  • The MC++ compiler could not produce verifiable code

What C++/CLI gives us?

  • Elegant syntax and grammar -This gave a natural feel for C++ developers writing managed code and allowed a smooth transition from unmanaged coding to managed coding. All those ugly double underscores are gone now.
  • First class CLI support - CLI features like properties, garbage collection and generics are supported directly. And what's more, C++/CLI allows jus to use these features on native unmanaged classes too.
  • First class C++ support - C++ features like templates and deterministic destructors work on both managed and unmanaged classes. In fact C++/CLI is the only .NET language where you can *seemingly* declare a .NET type on the stack or on the native C++ heap.
  • Bridges the gap between .NET and C++ - C++ programmers won't feel like a fish out of water when they attack the BCL
  • The executable generated by the C++/CLI compiler is now fully verifiable.

Hello World

				using
				namespace System;

void _tmain()
{
    Console::WriteLine("Hello World");
}

Well, that doesn't look a lot different from old syntax, except that now you don't need to add a reference to mscorlib.dll because the Whidbey compiler implicitly references it whenever you compile with /clr (which now defaults to /clr:newSyntax).

Handles

One major confusion in the old syntax was that we used the * punctuator with unmanaged pointers and with managed references. In C++/CLI Microsoft introduces the concept of handles.

				void _tmain()
{
    //The ^ punctuator represents a handle
    String^ str = "Hello World";
    Console::WriteLine(str);
}

The ^ punctuator (pronounced as cap) represents a handle to a managed object. According to the CLI specification a handle is a managed object reference. Handles are the new-syntax equivalent of __gc pointers in the MC++ syntax. Handles are not to be confused with pointers and are totally different in nature from pointers.

How handles differ from pointers?

  • Pointers are denoted using the * punctuator while handles are denoted using the ^ punctuator.
  • Handles are managed references to objects on the managed heap, pointers just point to a memory address.
  • Pointers are stable and GC cycles do not affect them, handles might keep pointing to different memory locations based on GC and memory compactions.
  • For pointers, the programmer must deleteexplicitly or else suffer a leak. For handles delete is optional.
  • Handles are type-safe while pointers are most definitely not. You cannot cast a handle to a void^.
  • Just as a new returns a pointer, a gcnew returns a handle.

Instantiating CLR objects

				void _tmain()
{
    String^ str = gcnew String("Hello World");
    Object^ o1 = gcnew Object();
    Console::WriteLine(str);
}

The gcnew keyword is used to instantiate CLR objects and it returns a handle to the object on the CLR heap. The good thing about gcnew is that it allows us to easily differentiate between managed and unmanaged instantiations.

Basically, the gcnew keyword and the ^ operator offer just about everything you need to access the BCL. But obviously you'd need to create and declare your own managed classes and interfaces.

Declaring types

CLR types are prefixed with an adjective that describes what sort of type it is. The following are examples of type declarations in C++/CLI :-

  • CLR types
    • Reference types
      • refclass RefClass{...};
      • refstruct RefClass{...};
    • Value types
      • value class ValClass{...};
      • value struct ValClass{...};
    • Interfaces
      • interfaceclass IType{...};
      • interfacestruct IType{...};
    • Enumerations
      • enumclass Color{...};
      • enumstruct Color{...};
  • Native types
    • class Native{...};
    • struct Native{...};
				using
				namespace System;

interfaceclass IDog
{
    void Bark();
};

refclass Dog : IDog
{
public:
    void Bark()
    {
        Console::WriteLine("Bow wow wow");
    }
};

void _tmain()
{
    Dog^ d = gcnew Dog();
    d->Bark();
}

There, the syntax is now so much more neater to look at than the old-syntax where the above code would have been strewn with double-underscored
 keywords like __gc and __interface.

Boxing/Unboxing

Boxing is implicit (yaay!) and type-safe. A bit-wise copy is performed and an Object is created on the CLR heap. Unboxing is explicit - just do a reinterpret_cast and then dereference.

				void _tmain()
{
    int z = 44;
    Object^ o = z; //implicit boxingint y = *reinterpret_cast<int^>(o); //unboxing

    Console::WriteLine("{0} {1} {2}",o,z,y);

    z = 66; 
    Console::WriteLine("{0} {1} {2}",o,z,y);
}

// Output// 44 44 44// 44 66 44

The Objecto is a boxed copy and does not actually refer the int value-type which is obvious from the output of the second Console::WriteLine.

When you box a value-type, the returned object remembers the original value type.

				void _tmain()
{
    int z = 44;
    float f = 33.567;

    Object^ o1 = z; 
    Object^ o2 = f; 

    Console::WriteLine(o1->GetType());
    Console::WriteLine(o2->GetType());    
}

// Output// System.Int32// System.Single

Thus you cannot try and unbox to a different type.

				void _tmain()
{
    int z = 44;
    float f = 33.567;

    Object^ o1 = z; 
    Object^ o2 = f;

    int y = *reinterpret_cast<int^>(o2);//System.InvalidCastExceptionfloat g = *reinterpret_cast<float^>(o1);
//System.InvalidCastException }

If you do attempt to do so, you'll get a System.InvalidCastException. Talk about perfect type-safety! If you look at the IL generated, you'll see the MSIL box instruction in action. For example :-

				void Box2()
{
    float y=45;
    Object^ o1 = y;
}

gets compiled to :-

.maxstack  1
.locals (float32 V_0, object V_1)

  ldnull
  stloc.1ldc.r4     45.
  stloc.0ldloc.0
  box   [mscorlib]System.Single
  stloc.1ret

According to the MSIL docs, "The box instruction converts the ‘raw’ valueType (an unboxed value type) into an instance of type Object (of type O). This is accomplished by creating a new object and copying the data from valueType into the newly allocated object."

Further reading

Conclusion

Alright, so why would anyone want to use C++/CLI when they can use C#, J# and that VB thingie for writing .NET code? Here are the four reasons I gave during my talk at DevCon 2003 in Trivandrum (Dec 2003).

  • Compile existing C++ code to IL (/clr magic)
  • Deterministic destruction
  • Native interop support that outmatches anything other CLI languages can offer
  • All those underscores in MC++ are gone ;-)

About Nishant Sivakumar



Editor
Site Builder
Nish is a real nice guy living in Toronto who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he has moved to Toronto so he can enjoy the cold winter and get to play in some snow.

He works for The Code Project and handles the Dundas MFC products Ultimate Toolbox, Ultimate Grid and Ultimate TCP/IP that are sold exclusively through The Code Project Storefront. He frequents the CP discussion forums when he is not coding, reading or writing. Nish hopes to visit at least three dozen countries before his human biological mechanism stops working (euphemism used to avoid the use of the d-word here), and regrets that he hasn't ever seen snow until now (will be rectified this December). Oh btw, it must be mentioned that normally Nish is not inclined to speak about himself in the 3rd person.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com

Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.

Click here to view Nishant Sivakumar's online profile.



 

C++的管理扩展

简介

C++管理扩展是一组语言扩展,它帮助Microsoft Visual C++开发人员为微软.NET编写应用程序。

管理扩展是有用的,如果你:

  • 希望提高开发人员使用C++编写.NET应用程序的生产率
  • 要分阶段地将一大段代码从非管理C++中移植到.NET平台上
  • 想从.NET Framework应用程序中使用已有的非管理C++组件。
  • 想从非管理C++中使用.NET Framework组件
  • 在同一应用程序中混合非管理C++代码和.NET代码

C++管理扩展为开发人员定位.NET Framework提供了无比的灵活性。传统的非管理C++和管理C++代码可以自由地混合在一个
应用程序中。用管理扩展编写的应用程序可以利用两种代码的优点。使用管理扩展,现有组件可以方便地封装到.NET组件中,
在与.NET集成的同时保留原有投资。

什么是管理扩展?
扩展允许你在C++中编写在.NET Framework控制下运行的管理(或.NET)类。(非管理C++类运行在传统的微软基于Windows?
的环境中。)一个管理类是一个内置的.NET类,可以完全利用.NET Framework。

管理扩展是Visual C++开发系统的新关键字和属性。它们允许开发人员决定哪些类或函数编译为管理或非管理代码。
这些部分然后就可以平滑地与其它部分或外部库交互。

管理扩展也用于在C++源代码中表示.NET类型和概念。这就允许开发人员容易地编写.NET应用程序,而无需编写额外代码。

主要使用环境

  • 将现有代码平滑地移植到 .NET
    如果你在C++代码上有大量投资,管理扩展将帮你将它们平滑地转移到.NET平台中。因为你可以在一个应用程序--
  • 甚至是同一文件中混合管理和非管理代码,你可以用很长时间转移代码,一个组件接一个组件地转换到.NET中。
  • 或你可以继续在非管理C++中编写组件,以利用该语言的强大功能和灵活性,只用管理扩展编写少量的高性能的
  • 封装器(它使你的代码可以从.NET组件中调用)。
  • 从 .NET语言中访问C++组件
    管理扩展允许你从任何.NET语言中调用C++类。你需要用扩展编写简单的封装器,它将你的C++类和方法暴露为
  • 管理类。封装器是完全的管理类,可以从任何.NET语言中调用。封装器类是作为了管理类与非管理C++类间的
  • 映射层。它简单地将方法调用直接传递到非管理类中。管理扩展可用于调用任何内置的动态链接库(DLL)及
  • 内置类。
  • 从内置代码中访问.NET 类
    使用管理扩展,你可以创建并从C++代码中直接调用.NET类。你可以编写将.NET组件当作任何其它管理C++类的
  • C++代码。你可以使用.NET Framework中内置的COM调用.NET类。你使用COM还是使用管理扩展访问.NET组件
  • 要依赖于你的工程。在一些情况下,利用现有的COM支持是最好的选择。在另一些情况下,使用管理扩展可能
  • 会增加性能和开发者的生产率。
  • 在同一可执行文件中的管理和内置代码
    Visual C++编译器能在管理和非管理上下文中自动而透明的翻译数据、指针和指令流。这个过程是允许管理扩展
  • 无缝地与非管理代码交互的过程。开发人员能够控制什么样的数据和代码可以管理。选择每个类或函数是管理
  • 还是非管理的能力为开发人员提供了更大的灵活性。一些代码或数据类型在非管理环境中执行得要比较好。
  • 另一方面,管理代码由于如碎片收集和类库等特性,它提高了开发人员的生产率。现有非管理代码可以一次
  • 一部分地转化为管理代码,因此保留了已有的投资。

posted on 2005-12-29 15:14 梦在天涯 阅读(987) 评论(2)  编辑 收藏 引用 所属分类: Manage c++ /CLI

评论

# re: A first look at C++/CLI 2006-08-17 17:10 梦在天涯

c++\CLI全新的语法,结合了native c++ 和.net的大部分功能:(VS2005支持c++\CLI,例如建立visual c++下的CLR Console Project)

Elegant syntax and grammar -This gave a natural feel for C++ developers writing managed code and allowed a smooth transition from unmanaged coding to managed coding. All those ugly double underscores are gone now.
First class CLI support - CLI features like properties, garbage collection and generics are supported directly. And what's more, C++/CLI allows jus to use these features on native unmanaged classes too.
First class C++ support - C++ features like templates and deterministic destructors work on both managed and unmanaged classes. In fact C++/CLI is the only .NET language where you can *seemingly* declare a .NET type on the stack or on the native C++ heap.
Bridges the gap between .NET and C++ - C++ programmers won't feel like a fish out of water when they attack the BCL
The executable generated by the C++/CLI compiler is now fully verifiable.
  回复  更多评论   

# re: A first look at C++/CLI(C++的管理扩展) 2006-08-18 10:14 梦在天涯

Managed c++ VS2002支持,

c++\CLI 全新的语法,vs2005支持,

他们不是同一个东西.  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1783918
  • 排名 - 5

最新评论

阅读排行榜