青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

使用c++\CLI實現c++托管與非托管混合編程

Mixing Native and Managed Types in C++

Wow, its turning into a busy month. I just haven’t had any time to blog despite having a number of interesting topics to cover. I’ll try to get a few of them written soon. Here’s a topic from one of my regular readers.

The CLR naturally supports mixing managed and native method calls allowing you to easily call native functions from managed functions (which are of course natively compiled before execution) and visa versa. This is all largely transparent in C++. What’s not as transparent is how to mix managed and native types. The reason is that there is a greater distinction between native and managed types compared with function calls. Beside obvious differences such as those introduced by different calling conventions and virtual invocation, function calls aren’t all that different. Types however require a bit more help from the programmer/compiler since native and managed types can have very different characteristics. This is very evident in C# as you often need to decorate native type definitions with all kinds of attributes to control memory layout and marshalling. Fortunately for the C++ programmer the compiler takes care of much of this when you define or include native type definitions such as those found in the various Windows header files, but the programmer is still responsible for telling the compiler just how those types are to be used.

Visual C++ provides many of the building blocks for mixing native and managed types but in some cases you need to write a little code to help it along. Fortunately C++/CLI is very capable. Let’s consider a few different scenarios.

Embed Simple Managed Type in Native Type

Since the CLR needs to keep track of every single instance of a managed type in a process, storing some kind of reference/pointer/handle to a managed object in a native type is not directly supported since instances of native types can be allocated in any region of memory and cast to all kinds of foreign data types that would be completely opaque to the CLR and its services. Instead you need to register such occurrences with the CLR so that it is aware of these “native” references to managed types. This is achieved with the use of the GCHandle type. Internally GCHandle manages a static table of (native) pointers that are used to lookup the objects in the managed heap. Of course using GCHandle directly from C++ can be quite tedious. It’s a CLS compliant value type which means native pointers are represented by IntPtr values. It also does not preserve static type information so static_casts are inevitable. Fortunately Visual C++ ships with the gcroot native template class that provides a strongly-typed interface over the GCHandle type.

#include <vcclr.h>
?
ref struct ManagedType
{
??? void HelloDotNet()
??? {
??????? Console::WriteLine("Hello .NET");
??? }
};
?
struct NativeType
{
??? ManagedType m1;????????? // Error!
???
??? ManagedType^ m2;???????? // Error!
???
??? gcroot<ManagedType^> m3; // OK
};
?
void main()
{
??? NativeType native;
??? native.m3 = gcnew ManagedType;
?
??? native.m3->HelloDotNet();
}

As you can see, gcroot provides a “smart” pointer for storing handles in native types. It may be smart but it does not provide automatic cleanup of resources. Specifically, the gcroot destructor makes no attempt to dispose of the managed object’s resources.

Embed Managed Resource in Native Type

Enter the auto_gcroot class. This native template class wraps a gcroot and provides transfer-of-ownership semantics for managed objects stored in native types. If you’re looking for a point of reference, think of the auto_ptr template class from the Standard C++ Library which does the same thing for native pointers. The auto_gcroot destructor takes care of “deleting” the handle which results in the object’s IDisposable::Dispose method (if any) being called.

#include <msclr\auto_gcroot.h>
?
ref struct ManagedType
{
??? void HelloDotNet()
??? {
??????? Console::WriteLine("Hello .NET");
??? }
?
??? ~ManagedType()
??? {
??????? Console::WriteLine("dtor");

??????? // Compiler implements Dispose pattern...
??? }
};
?
struct NativeType
{
??? msclr::auto_gcroot<ManagedType^> m3; // OK
};
?
void main()
{
??? NativeType native;
??? native.m3 = gcnew ManagedType;
?
??? native.m3->HelloDotNet();
}

The NativeType destructor (provided by the compiler) will automatically call the auto_gcroot destructor which will delete the managed object resulting in its destructor being called through its compiler generated Dispose method.

Embed Native Type in Managed Type

Now let’s turn things around. Let’s say we want to store a native type as a member of a managed type. The challenge is that the only native type the CLR really supports within managed types is a native pointer. C# programmers use IntPtr but that is only because IntPtr is the CLS compliant way of representing a native pointer and C# tries really hard to remain CLS compliant. The CLR fully supports storing native pointers without losing type information.

struct NativeType
{
};
?
ref struct ManagedType
{
??? NativeType n1; // Error!

??? NativeType* n2; // OK
};

That’s great except that now we have a resource management issue. Recall that C++ does not have the separation of memory and resource management evident in the CLR. The native object pointed to by the ManagedType member needs to be deleted. Here is one solution.

ref struct ManagedType
{
??? NativeType* n2; // OK
?
??? ~ManagedType()
??? {
??????? if (0 != n2)
??????? {
??????????? delete n2;
??????????? n2 = 0;
??????? }
??? }
};

Now the ManagedType has a Dispose implementation that will faithfully delete the native object. But this can become tedious and error prone very quickly. A better solution is to use some kind of “automatic” approach. Fortunately C++/CLI support by-value semantics for members so all we need is a managed auto-pointer template class. With such a class the ManagedType becomes really simple.

ref struct ManagedType
{
??? AutoPtr<NativeType> n2; // OK
};

ManagedType stores a pointer to a native object and its destructor automatically deletes the object. Woohoo!

The C++ compiler really takes care of a lot of boilerplate code. If you’re not sure just how much code the compiler is taking care of for you then?take a look at the compiled assembly in a disassembler.

Although Visual C++ does not provide a managed AutoPtr class, it is reasonably simple to write one. Here is a basic implementation.

template <typename T>
ref struct AutoPtr
{
??? AutoPtr() : m_ptr(0)
??? {
??????? // Do nothing
??? }
??? AutoPtr(T* ptr) : m_ptr(ptr)
??? {
??????? // Do nothing
??? }
??? AutoPtr(AutoPtr<T>% right) : m_ptr(right.Release())
??? {
??????? // Do nothing
??? }
??? ~AutoPtr()
??? {
??????? if (0 != m_ptr)
??????? {
??????????? delete m_ptr;
??????????? m_ptr = 0;
??????? }
??? }
??? T& operator*()
??? {
??????? return *m_ptr;
??? }
??? T* operator->()
??? {
??????? return m_ptr;
??? }
??? T* Get()
??? {
??????? return m_ptr;
??? }
??? T* Release()
??? {
??????? T* released = m_ptr;
??????? m_ptr = 0;
??????? return released;
??? }
??? void Reset()
??? {
??????? Reset(0);
??? }
??? void Reset(T* ptr)
??? {
??????? if (0 != m_ptr)
??????? {
??????????? delete m_ptr;
??????? }
??????? m_ptr = ptr;
??? }
private:
??? T* m_ptr;
};

In a future post I may?provide a few realistic examples of mixing native and managed code, but I hope this introduction has given you a few ideas on how to mix native and managed code and types effectively in C++.


來自: http://weblogs.asp.net/kennykerr/archive/2005/07/12/Mixing-Native-and-Managed-Types-in-C_2B002B00_.aspx

posted on 2006-08-16 14:23 夢在天涯 閱讀(3770) 評論(0)  編輯 收藏 引用 所屬分類: CPlusPlus 、Manage c++ /CLI

公告

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

搜索

  •  

積分與排名

  • 積分 - 1819327
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              中日韩在线视频| 亚洲人成久久| 午夜精品视频网站| 国产精品你懂的| 亚洲欧美日韩精品综合在线观看| 99re热这里只有精品视频| 欧美日韩伦理在线| 午夜精品区一区二区三| 欧美在线视频二区| 亚洲精品一线二线三线无人区| 欧美大片91| 欧美午夜不卡| 久久免费视频网站| 欧美精品97| 午夜久久影院| 免费精品99久久国产综合精品| 一区二区不卡在线视频 午夜欧美不卡在| 亚洲日本久久| 国产视频欧美| 91久久精品一区| 国产精品永久入口久久久| 老司机一区二区三区| 欧美理论在线| 久久久精品国产一区二区三区| 久久嫩草精品久久久久| 一本一本a久久| 久久不射中文字幕| 中日韩男男gay无套| 欧美一区二区三区久久精品茉莉花 | 亚洲精品一区二区三区不| 国产精品国产自产拍高清av王其| 久久久综合视频| 欧美日韩在线精品| 免费h精品视频在线播放| 国产精品成人免费精品自在线观看| 欧美在线在线| 欧美四级剧情无删版影片| 蜜臀99久久精品久久久久久软件| 欧美日韩妖精视频| 男女av一区三区二区色多| 国产精品久久久爽爽爽麻豆色哟哟| 欧美大胆a视频| 国产主播精品在线| 一本色道久久综合狠狠躁的推荐| 精品1区2区| 亚洲欧美一区二区视频| 一本色道久久综合亚洲二区三区| 久久久天天操| 久久精品电影| 国产精品视频yy9299一区| 亚洲品质自拍| 亚洲福利视频一区二区| 欧美一区国产在线| 欧美影视一区| 国产精品永久免费在线| 国产精品99久久久久久久vr | 欧美日韩妖精视频| 亚洲成在人线av| 亚洲国产精品成人一区二区| 欧美在线欧美在线| 久久精品99国产精品酒店日本| 国产精品超碰97尤物18| 99亚洲视频| 亚洲一区自拍| 国产精品五月天| 亚洲在线免费视频| 欧美中文字幕视频在线观看| 国产精品每日更新| 午夜精品免费在线| 久久久久久久999| 狠狠色丁香久久婷婷综合_中| 午夜精品久久久久久久久| 久久都是精品| 黄色亚洲网站| 久久久亚洲一区| 亚洲福利在线看| 一区二区三区产品免费精品久久75| 欧美精品二区三区四区免费看视频| 91久久一区二区| 亚洲专区一二三| 国产午夜久久| 麻豆国产精品一区二区三区 | 美日韩免费视频| 亚洲福利一区| 欧美日韩高清免费| 一区二区三区**美女毛片| 午夜精品99久久免费| 国产日韩成人精品| 久久夜色精品国产欧美乱| 亚洲国产精品一区二区尤物区| 亚洲精品影院| 国产精品乱码人人做人人爱| 欧美一区二区三区播放老司机 | 一区二区三区四区蜜桃| 国产精品福利在线观看| 欧美中文字幕| 亚洲国产中文字幕在线观看| 亚洲尤物在线视频观看| 韩国在线视频一区| 欧美人与禽性xxxxx杂性| 亚洲免费在线视频| 欧美韩日一区二区三区| 亚洲你懂的在线视频| 在线精品观看| 国产精品久久久久久久app| 久久久av毛片精品| 99在线热播精品免费| 久久久久久久久久看片| 夜夜嗨av一区二区三区网站四季av| 欧美午夜无遮挡| 久久久久久久久蜜桃| 亚洲伦理在线观看| 卡一卡二国产精品| 性色av一区二区三区| 亚洲精品在线看| 激情综合色综合久久| 欧美亚州韩日在线看免费版国语版| 久久精品夜色噜噜亚洲a∨| 国产精品99久久99久久久二8| 美国成人直播| 久久精品1区| 亚洲综合精品一区二区| 亚洲精选91| 亚洲国产另类久久精品| 国产一区二区精品久久99| 欧美日韩午夜激情| 欧美成人午夜激情在线| 久久久久高清| 久久精品91久久久久久再现| 亚洲无限乱码一二三四麻| 亚洲激情在线观看| 亚洲大胆在线| 毛片一区二区三区| 久久亚洲色图| 久久琪琪电影院| 久久久久久尹人网香蕉| 久久精品国产77777蜜臀| 亚洲欧美国产精品专区久久| 亚洲小说欧美另类婷婷| 一区二区久久| 中日韩高清电影网| 亚洲视频观看| 亚洲欧美日韩精品久久亚洲区| 中文精品视频一区二区在线观看| 日韩视频中文| 一区二区三区四区五区精品视频| 亚洲免费高清| 正在播放日韩| 亚洲男同1069视频| 亚洲欧美日韩精品久久| 欧美一二区视频| 久久久999精品| 免费久久精品视频| 亚洲第一黄色| 一本色道久久综合| 亚洲欧美在线免费观看| 午夜日韩激情| 久久久一区二区| 欧美精品成人| 国产精品美女主播| 国产一区二区看久久| 一区二区在线观看av| 亚洲激情黄色| 亚洲欧美第一页| 久久视频精品在线| 欧美激情va永久在线播放| 亚洲区一区二| 亚洲欧美激情精品一区二区| 欧美伊人久久大香线蕉综合69| 久久久精品午夜少妇| 欧美久久精品午夜青青大伊人| 欧美午夜理伦三级在线观看| 国产精品永久免费观看| 亚洲黄色成人| 午夜精品久久久久久久蜜桃app | 久久九九精品99国产精品| 另类天堂av| 亚洲精品在线看| 羞羞答答国产精品www一本| 久久综合图片| 国产精品高清在线观看| 黄色国产精品| 制服丝袜亚洲播放| 另类图片国产| 一区二区三区欧美| 久久国产精品99久久久久久老狼| 欧美成人一区二区在线| 国产精品揄拍500视频| 亚洲区免费影片| 久久精品国产清自在天天线 | 久久久久久久尹人综合网亚洲| 欧美国产第一页| 亚洲欧美日韩综合国产aⅴ| 卡通动漫国产精品| 国产欧美日韩三级| 夜夜嗨av一区二区三区中文字幕| 久久精品人人爽| 这里只有精品视频| 欧美国产日韩a欧美在线观看| 国产午夜一区二区三区|