• <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>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {C#基礎(chǔ)}

            CLI/C++中混合類的使用


            一 混合類

            所謂混合類是指CLI/C++中native的Class中可以包含CLR對象,CLR的class也可以包含Naitve的對象。

            1)native的class中包含CLR對象,必須通過gcroot<>或auto_gcroot<>。
            2)CLR中的class中包含native的對象,必須是指針,也可以使用高手寫的CAutoNativePtr智能指針。


            注意:C#中不能調(diào)用CLI/C++中的Native的class。同樣Native C++中也不能調(diào)用CLI/C++中的Ref的class。

            二 實例

             高手的CAutoNativePtr類:

            /***
                CAutoNativePtr - A smart pointer for using native objects in managed code.

                Author    :    Nishant Sivakumar
                Email    :    voidnish@gmail.com    
                Blog    :    
            http://blog.voidnish.com
                Web        :    
            http://www.voidnish.com     

                You may freely use this class as long as you include
                this copyright. 
                
                You may freely modify and use this class as long
                as you include this copyright in your modified version. 

                This code is provided "as is" without express or implied warranty. 
                
                Copyright ?Nishant Sivakumar, 2006.
                All Rights Reserved.
            **
            */


            #pragma once

            template
            <typename T> ref class CAutoNativePtr
            {
            private:
                T
            * _ptr;

            public:
                CAutoNativePtr() : _ptr(nullptr)
                
            {
                }


                CAutoNativePtr(T
            * t) : _ptr(t)
                
            {
                }


                CAutoNativePtr(CAutoNativePtr
            <T>% an) : _ptr(an.Detach())
                
            {
                }


                template
            <typename TDERIVED> 
                    CAutoNativePtr(CAutoNativePtr
            <TDERIVED>% an) : _ptr(an.Detach())
                
            {
                }


                
            !CAutoNativePtr()
                
            {    
                    delete _ptr;
                }


                
            ~CAutoNativePtr()
                
            {
                    
            this->!CAutoNativePtr();
                }


                CAutoNativePtr
            <T>% operator=(T* t)
                
            {
                    Attach(t);
                    
            return *this;
                }


                CAutoNativePtr
            <T>% operator=(CAutoNativePtr<T>% an)
                
            {
                    
            if(this != %an)
                        Attach(an.Detach());
                    
            return *this;
                }


                template
            <typename TDERIVED> 
                    CAutoNativePtr
            <T>% operator=(CAutoNativePtr<TDERIVED>% an)
                
            {
                    Attach(an.Detach());
                    
            return *this;
                }


                
            static T* operator->(CAutoNativePtr<T>% an)
                
            {
                    
            return an._ptr;
                }


                
            static operator T*(CAutoNativePtr<T>% an)
                
            {
                    
            return an._ptr;
                }


                T
            * Detach()
                
            {
                    T
            * t = _ptr;
                    _ptr 
            = nullptr;
                    
            return t;
                }


                
            void Attach(T* t)
                
            {
                    
            if(t)
                    
            {    
                        
            if(_ptr != t)
                        
            {
                            delete _ptr;
                            _ptr 
            = t;
                        }

                    }

                    
            else
                    
            {
            #ifdef _DEBUG
                        
            throw gcnew Exception(
                            
            "Attempting to Attach() a nullptr!");
            #endif
                    }
                    
                }


                
            void Destroy()
                
            {
                    delete _ptr;
                    _ptr 
            = nullptr;
                }

            }
            ;

            測試實例之CLI/C++文件:
            // MixedNativeAndCLIDLL.h

            #pragma once
            #include 
            <string>
            #include 
            <iostream>
            #include 
            <gcroot.h>
            #include 
            <msclr/auto_gcroot.h>

            #include 
            "AutoNative.h"

            using namespace System;

            namespace MixedNativeAndCLIDLL {

                
            public class NativeClass
                
            {
                
            public:
                    
            int *pX;    
                    NativeClass()
            {pX = new int(10);}
                    
            ~NativeClass()
                    
            {
                        
            if(pX != NULL)
                        
            {
                            delete pX;
                            pX 
            = NULL;
                        }

                    }
                    
                }
            ;

                
            public ref class RefClass
                
            {
                
            public:
                    
            int x;    
                    RefClass()
            {x = 20;}
                }
            ;

                
            public class MixedClass0
                
            {
                    
            public:
                        NativeClass nativeClass;
                        
            //RefClass refClass; // error c3265 and error c3149
                        gcroot<RefClass^> refClass1;

                        std::
            string nativeStr;
                        
            //System::String refStr; // error c3265 and error c3149
                        gcroot<System::String^> refStr1;

                        MixedClass0()
                        
            {
                            refClass1 
            = gcnew RefClass();
                            refStr1 
            = gcnew System::String("i am a native class mixed some clr members.\n");
                        }

                        
            ~MixedClass0()
                        
            {            
                            delete refClass1;
                            delete refStr1;
                        }


                        
            void PrintSelf()
                        
            {
                            System::Console::WriteLine(
            "my name is MixedClass0");
                            System::Console::WriteLine(refClass1
            ->x);
                            System::Console::WriteLine(refStr1);
                        }

                }
            ;

                
            public class MixedClass1
                
            {
                    
            public:
                        NativeClass nativeClass;
                        
            //RefClass refClass; // error c3265 and error c3149
                        msclr::auto_gcroot<RefClass^> refClass1;

                        std::
            string nativeStr;
                        
            //System::String refStr; // error c3265 and error c3149
                        msclr::auto_gcroot<System::String^> refStr1;

                        MixedClass1()
                        
            {
                            refClass1 
            = gcnew RefClass();
                            refStr1 
            = gcnew System::String("i am a native class with some clr members.\n");
                        }

                        
            ~MixedClass1()
                        
            {
                            
            // no need to delete.
                        }
                    

                        
            void PrintSelf()
                        
            {
                            System::Console::WriteLine(
            "my name is MixedClass1");
                            System::Console::WriteLine(refClass1
            ->x);
                            System::Console::WriteLine(refStr1);
                        }

                }
            ;

                
            public ref class MixedClass2
                
            {
                    
            public:
                        
            //NativeClass nativeClass; // error c4368
                        NativeClass * nativeClass1;
                        RefClass
            ^ refClass; 
                        
                        
            //std::string nativeStr; // error c4368
                        std::string *nativeStr1;
                        System::String
            ^ refStr; //     

                        MixedClass2()
                        
            {
                            nativeClass1 
            = new NativeClass();
                            nativeStr1 
            = new std::string("i am a clr class with some native members.\n");
                        }

                        
            ~MixedClass2()
                        
            {
                            delete nativeClass1;
                            delete nativeStr1;
                        }

                        
            !MixedClass2(){}

                        
            void PrintSelf()
                        
            {
                            System::Console::WriteLine(
            "my name is MixedClass2");
                            std::cout
            <<*(nativeClass1->pX)<<std::endl;
                            std::cout
            <<*nativeStr1<<std::endl;                
                        }

                }
            ;
                
                
            public ref class MixedClass3
                
            {
                    
            public:
                        
            //NativeClass nativeClass; // error c4368
                        CAutoNativePtr<NativeClass> nativeClass1;
                        RefClass
            ^ refClass; 
                        
                        
            //std::string nativeStr; // error c4368
                        CAutoNativePtr<std::string> nativeStr1;
                        System::String
            ^ refStr; //     

                        MixedClass3()
                        
            {
                            nativeClass1 
            = new NativeClass();
                            nativeStr1 
            = new std::string("i am a clr class with some native members.\n");
                        }

                        
            ~MixedClass3(){}
                        
            !MixedClass3(){}

                        
            void PrintSelf()
                        
            {
                            System::Console::WriteLine(
            "my name is MixedClass3");
                            std::cout
            <<*(nativeClass1->pX)<<std::endl;
                            std::cout
            <<*nativeStr1<<std::endl;                
                        }

                }
            ;
            }


            測試實例之C#調(diào)用文件:
            using System;
            using System.Collections.Generic;
            using System.Text;

            namespace CsharpTest
            {
                
            class Program
                
            {
                    
            static void Main(string[] args)
                    
            {
                        MixedNativeAndCLIDLL.MixedClass0 mixedClass0 
            = new MixedNativeAndCLIDLL.MixedClass0();
                        
            //mixedClass0.PrintSelf();
                        MixedNativeAndCLIDLL.MixedClass1 mixedClass1 = new MixedNativeAndCLIDLL.MixedClass1();
                        
            //mixedClass1.PrintSelf();
                        MixedNativeAndCLIDLL.MixedClass2 mixedClass2 = new MixedNativeAndCLIDLL.MixedClass2();
                        mixedClass2.PrintSelf();
                        MixedNativeAndCLIDLL.MixedClass3 mixedClass3 
            = new MixedNativeAndCLIDLL.MixedClass3();
                        mixedClass3.PrintSelf();
                    }

                }

            }


            三 代碼下載

            http://m.shnenglu.com/Files/mzty/MixedNativeAndCLITest.rar

            posted on 2007-12-24 17:47 夢在天涯 閱讀(10598) 評論(3)  編輯 收藏 引用 所屬分類: CPlusPlus 、C#/.NETManage c++ /CLI

            評論

            # re: CLI/C++中混合類的使用 2007-12-24 19:00 阿里

            CAutoNativePtr類 好像出自一本講述c++/CLI的書
            今年才出版。  回復(fù)  更多評論   

            # re: CLI/C++中混合類的使用 2007-12-24 22:47 天下無雙

            嗯,很有用。  回復(fù)  更多評論   

            # re: CLI/C++中混合類的使用 2007-12-25 08:38 夢在天涯

            CAutoNativePtr這個來自C++/CLI in Action一書!  回復(fù)  更多評論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804159
            • 排名 - 5

            最新評論

            閱讀排行榜

            亚洲美日韩Av中文字幕无码久久久妻妇 | 久久久久99这里有精品10| 性欧美大战久久久久久久| 久久精品国产99国产电影网| 亚洲午夜无码久久久久| 久久99这里只有精品国产| 亚洲欧美久久久久9999| 久久一区二区三区99| 久久国产视频99电影| 久久国产精品99精品国产987| 国产精品免费福利久久| 精品国产VA久久久久久久冰| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 国产精品久久一区二区三区| 亚洲综合精品香蕉久久网| 波多野结衣久久| 久久精品人人做人人爽电影| 欧美日韩精品久久久免费观看| 久久强奷乱码老熟女网站| 亚洲精品国产综合久久一线| 少妇人妻综合久久中文字幕| 日本五月天婷久久网站| av色综合久久天堂av色综合在| 日本久久久久亚洲中字幕| 精品综合久久久久久97超人| 99久久精品国产毛片| 久久天天躁狠狠躁夜夜av浪潮 | 久久亚洲视频| 亚洲国产综合久久天堂| 亚洲综合伊人久久综合| 免费精品99久久国产综合精品| 97久久精品无码一区二区天美| 999久久久免费精品国产| 久久久久四虎国产精品| 久久99精品久久久久久噜噜| 日韩中文久久| 色欲综合久久中文字幕网| 狠狠狠色丁香婷婷综合久久五月| 国产精久久一区二区三区| 漂亮人妻被中出中文字幕久久| 久久精品国产亚洲AV无码娇色|