• <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++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            C++與.NET中基礎(chǔ)類型的對(duì)應(yīng)及轉(zhuǎn)化


            前言:為了介紹C#寫(xiě)界面,C++寫(xiě)算法的快捷交互開(kāi)發(fā)方式,首先介紹c++,C#內(nèi)部的DLL,COM調(diào)用.

            一,基礎(chǔ)類型



            二,字符類型

            /***

            Author 
            - Nishant Sivakumar
            Copyright(C) 
            - Nishant Sivakumar 2005

            ***/

            #pragma once

            #include 
            <wtypes.h>

            #include 
            <vector>
            #include 
            <string>

            #include 
            <vcclr.h>

            using namespace System;
            using namespace System::Runtime::InteropServices;

            namespace StringUtilities
            {
                
            //Class StringConvertorBase
                
            //Serves as the abstract base class for StringConvertor and implements
                
            //default initialization and clean-up code.
                ref class StringConvertorBase abstract
                {
                protected:
                    StringConvertorBase()
                    {
                        InternalInit();    
                    }    

                    ~StringConvertorBase()
                    {
                        InternalCleanUp();
                    }

                    
            //While the finalizer is included to take care of cases
                    
            //where the destructor does not get called, it's highly inefficient
                    //and ruins the purpose of the class if the class is not used
                    
            //with deterministic destruction. It's recommended that you either use
                    //the auto-variable non-handle declaration format or explicitly
                    
            //call delete on the StringConvertor object after use.
                    !StringConvertorBase()
                    {
                        InternalCleanUp();

            #ifdef _DEBUG
                        throw gcnew Exception(
            "Finalizer should not have got called");
            #
            else
                        Diagnostics::Trace::WriteLine(
            "Finalizer should not have got called");
            #endif
                    }

                    std::vector
            < gcroot<IntPtr> >* m_vec_hglobal;
                    std::vector
            < gcroot<IntPtr> >* m_vec_bstr;

                    
            //Frees the allocated unmanaged memory
                    void InternalCleanUp()
                    {
                        
            for each(gcroot<IntPtr> ptr in *m_vec_hglobal)
                        {
                            Marshal::FreeHGlobal(ptr);
                        }
                        m_vec_hglobal
            ->clear();
                        delete m_vec_hglobal;

                        
            for each(gcroot<IntPtr> ptr in *m_vec_bstr)
                        {
                            Marshal::FreeBSTR(ptr);
                        }
                        m_vec_bstr
            ->clear();
                        delete m_vec_bstr;
                    }

                    void InternalInit()
                    {
                        m_vec_hglobal 
            = new std::vector< gcroot<IntPtr> >(); 
                        m_vec_bstr 
            = new std::vector< gcroot<IntPtr> >(); 
                    }
                };

                
            //Class StringConvertor
                
            //General purpose convertor class for System::String (both from and to)
                ref class StringConvertor : StringConvertorBase
                {
                protected:
                    
            String^ m_String; //The internal System::String object

                
            public
                    
            //Various constructors each taking a different type as argument
                    StringConvertor(
            String^ s) : m_String(s)
                    {
                        
            if(m_String == nullptr)
                            throw gcnew Exception(
            "You need to pass a non-null String");            
                    }

                    StringConvertor(
            const char* s)
                    {
                        
            if(s == nullptr)
                            throw gcnew Exception(
            "You need to pass a non-null char*");
                        m_String 
            = gcnew String(s);
                    }

                    StringConvertor(
            const __wchar_t* s)
                    {
                        
            if(s == nullptr)
                            throw gcnew Exception(
            "You need to pass a non-null __wchar_t*");
                        m_String 
            = gcnew String(s);
                    }

                    StringConvertor(
            array<Char>^ s)
                    {
                        
            if(s == nullptr)
                            throw gcnew Exception(
            "You need to pass a non-null Char array");
                        m_String 
            = gcnew String(s);
                    }

                    StringConvertor(BSTR s)
                    {
                        
            if(s == nullptr)
                            throw gcnew Exception(
            "You need to pass a non-null BSTR");
                        m_String 
            = gcnew String(s);
                    }

                    StringConvertor(std::
            string s)
                    {
                        m_String 
            = gcnew String(s.c_str());
                    }

                    StringConvertor(std::wstring s)
                    {
                        m_String 
            = gcnew String(s.c_str());
                    }

                    
            //Methods

                    virtual 
            String^ ToString() override
                    {
                        return m_String;
                    }

                    
            //Operators

                    operator 
            String^()
                    {
                        return m_String;
                    }

                    
            //Properties    

                    
            //Returns an interior pointer to the underlying string. The
                    
            //pointer is of type const Char so you can only read through it.
                    
            property interior_ptr<const Char> InteriorConstCharPtr
                    {
                        interior_ptr
            <const Char> get()
                        {
                            return PtrToStringChars(m_String);            
                        }
                    }

                    
            //Returns an interior pointer to the underlying string that can be
                    
            //written to. Use with extreme care, as you are directly editing the 
                    
            //System::String's internal character buffer.
                    property interior_ptr<Char> InteriorCharPtr
                    {
                        interior_ptr
            <Char> get()
                        {
                            return const_cast
            < interior_ptr<Char> >(InteriorConstCharPtr);             
                        }
                    }

                    
            //Generic character mapping to use with LPTSTR.
                    
            //Since it's a #define, intellisense won't show it.

            #ifdef _UNICODE 
                #define NativeTCharPtr NativeWideCharPtr
            #
            else
                #define NativeTCharPtr NativeCharPtr
            #endif

                    
            //Returns a char* (allocated on the native heap)
                    
            property char* NativeCharPtr
                    {
                        char
            * get()
                        {
                            IntPtr ptr 
            = Marshal::StringToHGlobalAnsi(m_String);
                            
            if(ptr != IntPtr::Zero)
                            {
                                m_vec_hglobal
            ->push_back(ptr);
                                return reinterpret_cast
            <char*>(static_cast<void*>(ptr));
                            }
                            
            else 
                                return nullptr;            
                        }
                    }

                    
            //Returns a __wchar_t* (allocated on the native heap)
                    
            property __wchar_t* NativeWideCharPtr
                    {
                        __wchar_t
            * get()
                        {
                            IntPtr ptr 
            = Marshal::StringToHGlobalUni(m_String);
                            
            if(ptr != IntPtr::Zero)
                            {
                                m_vec_hglobal
            ->push_back(ptr);
                                return reinterpret_cast
            <__wchar_t*>(static_cast<void*>(ptr));
                            }
                            
            else 
                                return nullptr;
                        }
                    }

                    
            //Returns a CLI array of Chars
                    
            property array<Char>^ CharArray
                    {
                        
            array<Char>^ get()
                        {
                            return m_String
            ->ToCharArray();
                        }
                    }

                    
            //Returns a BSTR allocated natively (unmanaged heap)
                    
            property BSTR BSTRCopy
                    {
                        BSTR 
            get()
                        {
                            IntPtr ptr 
            = Marshal::StringToBSTR(m_String);
                            
            if(ptr != IntPtr::Zero)
                            {
                                m_vec_bstr
            ->push_back(ptr);
                                return reinterpret_cast
            <BSTR>(static_cast<void*>(ptr));
                            }
                            
            else 
                                return nullptr;    
                        }
                    }

                    
            //Returns an std::string object
                    
            property std::string STLAnsiString
                    {
                        std::
            string get()
                        {
                            IntPtr ptr 
            = Marshal::StringToHGlobalAnsi(m_String);
                            
            if(ptr != IntPtr::Zero)
                            {
                                std::
            string tmp(reinterpret_cast<char*>(static_cast<void*>(ptr)));
                                Marshal::FreeHGlobal(ptr);
                                return tmp;                    
                            }
                            return std::
            string();
                        }
                    }

                    
            //Returns an std::wstring object
                    
            property std::wstring STLWideString
                    {
                        std::wstring 
            get()
                        {
                            IntPtr ptr 
            = Marshal::StringToHGlobalUni(m_String);
                            
            if(ptr != IntPtr::Zero)
                            {
                                std::wstring tmp(reinterpret_cast
            <__wchar_t*>(static_cast<void*>(ptr)));
                                Marshal::FreeHGlobal(ptr);
                                return tmp;                    
                            }
                            return std::wstring();
                        }
                    }
                };
            }



            三,其他

            對(duì)于COM,在托管代碼和非托管代碼之間進(jìn)行的所有調(diào)用都必須滿足各自編程模型強(qiáng)加的要求。托管和非托管編程模型在很多方面是不同的。下表顯示了每個(gè)模型的定義特征。

            特征 非托管模型 托管模型

            代碼模型

            基于接口

            基于對(duì)象

            標(biāo)識(shí)

            GUID

            強(qiáng)名稱

            錯(cuò)誤處理機(jī)制

            HRESULT

            異常

            類型兼容性

            二進(jìn)制標(biāo)準(zhǔn)

            類型標(biāo)準(zhǔn)

            類型定義

            類型庫(kù)

            元數(shù)據(jù)

            類型安全

            非類型安全

            可選安全

            版本控制

            不可變的

            靈活的


            COM類型對(duì)應(yīng):
            COM 值類型 COM 引用類型 系統(tǒng)類型

            bool

            bool *

            System.Int32

            charsmall

            char *small *

            System.SByte

            short

            short *

            System.Int16

            longint

            long *int *

            System.Int32

            Hyper

            hyper *

            System.Int64

            unsigned charbyte

            unsigned char *byte *

            System.Byte

            wchar_tunsigned short

            wchar_t *unsigned short *

            System.UInt16

            unsigned longunsigned int

            unsigned long *unsigned int *

            System.UInt32

            unsigned hyper

            unsigned hyper *

            System.UInt64

            float

            float *

            System.Single

            double

            double *

            System.Double

            VARIANT_BOOL

            VARIANT_BOOL *

            System.Boolean

            void*

            void **

            System.IntPtr

            HRESULT

            HRESULT *

            System.Int16System.IntPtr

            SCODE

            SCODE *

            System.Int32

            BSTR

            BSTR *

            System.String

            LPSTR[string, ...] char *

            LPSTR *

            System.String

            LPWSTR[string, …] wchar_t *

            LPWSTR *

            System.String

            VARIANT

            VARIANT *

            System.Object

            DECIMAL

            DECIMAL *

            System.Decimal

            DATE

            DATE *

            System.DateTime

            GUID

            GUID *

            System.Guid

            CURRENCY

            CURRENCY *

            System.Decimal

            IUnknown *

            IUnknown **

            System.Object

            IDispatch *

            IDispatch **

            System.Object

            SAFEARRAY( type )

            SAFEARRAY( type ) *

            type []



            四,參考:
                    http://www.codeproject.com/managedcpp/StringConvertor.asp

                    http://www.voidnish.com/articles/ShowArticle.aspx?code=StringConvertor

            posted on 2007-05-29 13:55 夢(mèng)在天涯 閱讀(5205) 評(píng)論(3)  編輯 收藏 引用 所屬分類: CPlusPlusManage c++ /CLI

            評(píng)論

            # re: C++與.NET中基礎(chǔ)類型的對(duì)應(yīng)及轉(zhuǎn)化 2007-06-04 14:38 看圖軟件

            好東西餓,支持  回復(fù)  更多評(píng)論   

            # re: C++與.NET中基礎(chǔ)類型的對(duì)應(yīng)及轉(zhuǎn)化 2007-06-04 23:22 夢(mèng)在天涯

            非托管 C 語(yǔ)言類型 托管類名 說(shuō)明
            HANDLE void* System.IntPtr 32 位
            BYTE unsigned char S ystem.Byte 8 位
            SHORT short System.Int16 16 位
            WORD unsigned short System.UInt16 16 位
            INT int System.Int32 32 位
            UINT unsigned int System.UInt32 32 位
            LONG long System.Int32 32 位
            BOOL bool System.BOOL 8 位
            DWORD unsigned long System.UInt32 32 位
            ULONG unsigned long System.UInt32 32 位
            CHAR char System.Char 用 ANSI 修飾。
            LPSTR char* System.String 或 System.StringBuilder 用 ANSI 修飾。
            LPCSTR Const char* System.String 或 System.StringBuilder 用 ANSI 修飾。
            LPWSTR wchar_t* System.String 或 System.StringBuilder 用 Unicode 修飾。
            LPCWSTR Const wchar_t* System.String 或 System.StringBuilder 用 Unicode 修飾。
            FLOAT Float System.Single 32 位
            DOUBLE Double System.Double 64 位
              回復(fù)  更多評(píng)論   

            # re: C++與.NET中基礎(chǔ)類型的對(duì)應(yīng)及轉(zhuǎn)化 2007-06-05 09:41 夢(mèng)在天涯

            msdn 上:
            BOOL
            long
            System.Int32
            32 位
              回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804434
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            香蕉久久av一区二区三区| 日本精品久久久久中文字幕| 三级韩国一区久久二区综合| 久久97久久97精品免视看秋霞| 日韩美女18网站久久精品| 亚洲人成精品久久久久| 久久人人爽人人爽人人片av高请| 色综合久久无码五十路人妻| 久久久久久久亚洲Av无码| 久久99精品久久久久久不卡| 亚洲色婷婷综合久久| 狠狠人妻久久久久久综合蜜桃| 狠狠色丁香婷婷久久综合| 久久免费小视频| 久久精品国产亚洲AV久| 国产高潮久久免费观看| 午夜精品久久久久久久久| 久久精品成人免费观看97| 91久久婷婷国产综合精品青草| 免费精品久久久久久中文字幕| 国内精品久久久久伊人av| 热久久最新网站获取| 久久精品亚洲男人的天堂| 国产精品久久久久9999| 色婷婷久久综合中文久久蜜桃av| 夜夜亚洲天天久久| 97久久精品无码一区二区 | 丁香久久婷婷国产午夜视频| 一本一本久久a久久综合精品蜜桃| 久久91精品久久91综合| jizzjizz国产精品久久| 亚洲综合伊人久久大杳蕉| 狠狠久久综合| 日本精品久久久久影院日本| 精品国产青草久久久久福利| 国产精品99久久不卡| 国产精品99久久久久久董美香 | 亚洲国产成人久久综合区| 免费精品久久久久久中文字幕| 久久久久亚洲AV综合波多野结衣| 久久精品无码一区二区三区日韩 |