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

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)  編輯 收藏 引用 所屬分類: CPlusPlusManage 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

搜索

  •  

積分與排名

  • 積分 - 1819317
  • 排名 - 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>
              91久久视频| 欧美国产成人在线| 999在线观看精品免费不卡网站| 亚洲一区欧美激情| 国产精品theporn| 亚洲精选一区| 亚洲第一色在线| 久久偷窥视频| 一色屋精品亚洲香蕉网站| 午夜精品久久久久久久久久久久久 | 久久成人精品视频| 亚洲视屏在线播放| 国产精品v日韩精品v欧美精品网站| 亚洲人精品午夜| 亚洲国产欧洲综合997久久| 久久综合久久综合久久综合| 韩日欧美一区二区| 老鸭窝毛片一区二区三区| 午夜精品久久久久久久99水蜜桃 | 亚洲国产岛国毛片在线| 开元免费观看欧美电视剧网站| 亚洲欧美一区二区精品久久久| 欧美日韩在线视频一区二区| 这里只有精品电影| 99热免费精品在线观看| 欧美日韩中字| 国产美女诱惑一区二区| 久久久www成人免费无遮挡大片| 午夜亚洲福利| 亚洲高清视频在线| 亚洲激情第一页| 欧美日韩日日骚| 午夜久久久久| 久久精品免视看| 亚洲日本va午夜在线电影| 亚洲人成网站777色婷婷| 欧美日韩在线播放一区| 久久爱www| 久久天堂成人| 在线视频日本亚洲性| 亚洲欧美国产高清| 精品成人国产| 一本久久综合| 亚洲成人在线| 日韩一级大片| 极品日韩久久| 99精品视频免费| 国产综合自拍| 一本色道88久久加勒比精品| 国产永久精品大片wwwapp| 亚洲高清在线精品| 国产精品日本一区二区| 美女日韩在线中文字幕| 欧美三级电影一区| 久久久综合精品| 欧美日韩一区二区在线| 久久男人av资源网站| 欧美精品国产| 免费欧美电影| 国产精品一区二区三区四区五区| 久久综合久久综合九色| 欧美视频在线观看免费网址| 蜜桃久久av| 国产欧美日韩不卡免费| 久久综合九色综合欧美就去吻| 欧美日韩国产三区| 久久一综合视频| 欧美日韩高清在线观看| 久久久久久久一区二区三区| 欧美成人四级电影| 久久久久一区二区三区| 国产精品久久久久久超碰| 最新精品在线| 日韩视频不卡中文| 久久综合久色欧美综合狠狠| 欧美中日韩免费视频| 国产精品成人一区二区三区夜夜夜| 免播放器亚洲一区| 国产一区二区在线观看免费| 亚洲一区二区三区精品在线观看| 亚洲精品久久嫩草网站秘色| 久久国产毛片| 久久精品国产99国产精品澳门| 欧美日本中文| 亚洲精品综合精品自拍| 91久久精品日日躁夜夜躁国产| 久久成人精品无人区| 久久久久国产免费免费| 国产一区二区三区精品欧美日韩一区二区三区 | 久久亚洲精品网站| 久久久久久网| 国产有码在线一区二区视频| 午夜一区在线| 欧美影院视频| 国内伊人久久久久久网站视频| 亚洲永久免费观看| 中文在线不卡| 国产精品久久久久久久免费软件 | 一区二区日韩伦理片| 欧美大胆人体视频| 亚洲韩日在线| 亚洲欧美另类国产| 国产精品影视天天线| 亚洲欧美制服另类日韩| 久久国产精品99国产精| 激情久久久久| 欧美二区在线| 99成人在线| 久久99伊人| 在线观看精品| 欧美黄色影院| 亚洲一区二区视频在线| 久久国产精品久久国产精品| 国产日韩精品一区观看 | 欧美电影免费观看大全| 亚洲精品国产精品久久清纯直播| av不卡免费看| 国产一区二区三区久久悠悠色av | 亚洲欧美精品一区| 国内精品免费午夜毛片| 女人天堂亚洲aⅴ在线观看| 亚洲激情在线观看| 欧美在线观看网址综合| 亚洲高清在线| 国产精品入口尤物| 老司机一区二区| 一区二区三区黄色| 久久中文字幕导航| 一区二区三区四区五区在线| 国产农村妇女精品| 欧美在线网站| 亚洲美女黄网| 毛片av中文字幕一区二区| 亚洲视频www| 国产精品伊人日日| 久久久久久**毛片大全| 最近看过的日韩成人| 亚洲在线免费观看| 91久久久久久国产精品| 国产精品黄页免费高清在线观看| 久久精品国产清高在天天线| 一区二区国产精品| 亚洲国产女人aaa毛片在线| 一区二区久久久久久| 在线观看不卡av| 国产日韩欧美成人| 欧美系列精品| 欧美激情欧美激情在线五月| 久久高清国产| 午夜精品国产更新| 一区二区欧美精品| 亚洲精品老司机| 欧美激情在线免费观看| 久久只有精品| 久久婷婷国产综合国色天香| 亚洲欧美日韩成人高清在线一区| 亚洲人成网站色ww在线| 在线精品一区| 影音先锋久久| 狠狠色2019综合网| 国产日韩欧美综合精品| 欧美激情一区二区三区四区 | 亚洲乱码国产乱码精品精天堂| 狠狠噜噜久久| 一区二区在线观看av| 国产片一区二区| 国产日韩在线看片| 国产美女精品| 国内精品一区二区| 激情另类综合| 亚洲福利免费| 91久久在线观看| 99国内精品久久久久久久软件| 噜噜噜在线观看免费视频日韩| 欧美伊人久久久久久久久影院 | 亚洲三级影院| 亚洲欧美国产va在线影院| 久久久久91| 亚洲精品一区在线观看香蕉| 亚洲综合色在线| 欧美国产欧美亚州国产日韩mv天天看完整 | 久久一区二区三区超碰国产精品 | 亚洲一区中文字幕在线观看| 久久精品99国产精品日本| 欧美黑人国产人伦爽爽爽| 一本色道久久综合亚洲精品不 | 中文亚洲字幕| 久久最新视频| 国产精品视频免费一区| 最近看过的日韩成人| 欧美在线观看网站| 亚洲精品久久久蜜桃| 性色av一区二区三区红粉影视| 欧美成人综合网站| 国产专区精品视频| 午夜精品久久| 亚洲精品日韩久久| 麻豆9191精品国产| 国产亚洲精品激情久久| 亚洲一区二区少妇|