• <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>
            posts - 17,  comments - 2,  trackbacks - 0


            System::String -> std::string
            A reader asks,
             
            Sender: Erik Brendengen
             
            Is there an easy way to convert from String^ to std::string?
                         Does String ^s = std::string( “bla”).c_str(); work the other way?
             
            This is a FAQ, one that I myself bumped into when I had to pass a System::String retrieved from System::Windows::Forms::TextBox to a native program that expected a std::string.
             
            There are generally two conversion strategies, depending on which side of the managed/native fence you wish to do the conversion work. Here are two methods making use of the PtrToStringChars( String^ ) utility from the vcclr.h header file that is part of Visual C++. Note that these are written in the new C++/CLI syntax – and that these are adapted from an internal FAQ ...
             
            #include <stdlib.h>
            #include <vcclr.h>
            #include <string>
            using namespace System;
             
            bool To_CharStar( String^ source, char*& target )
            {
                pin_ptr<const wchar_t> wch = PtrToStringChars( source );
                int len = (( source->Length+1) * 2);
                target = new char[ len ];
                return wcstombs( target, wch, len ) != -1;
            }
             
            bool To_string( String^ source, string &target )
            {
                pin_ptr<const wchar_t> wch = PtrToStringChars( source );
                int len = (( source->Length+1) * 2);
                char *ch = new char[ len ];
                bool result = wcstombs( ch, wch, len ) != -1;
                target = ch;
                delete ch;
                return result;
            }
             
            As to the second question, does String^ s = ns.c_str() work? Yes.
            Published Wednesday, June 02, 2004 2:56 PM by slippman
            Comments
             
            Are there any plans to, ahem, extend std::string to include constructors and extraction methods for String^, like ATL::CString currently has? 
            As a sidenote, this is not the first time I find myself wanting one or two features from CString in std::string, to make the decision for which of the two classes to use for a native Windows C++ application a bit easier... 
            Either way, I'm really looking forward to the new C++/CLI. Actually, the /clr switch works amazingly well even today.
            June 3, 2004 10:27 AM
             
            In Whidbey we'll be supplying much more direct approach to do this type of marshalling via a library. 

            void F( String^ s ) 

            string s1 = marshal_to<string>( s ); 
            String^ s2 = marshal_to<String^>( s1 ); 


            If you already have a char*, and want to get a System::String from it, all you have to do is this: 

            void F( const char* s ) 

            String^ s1 = gcnew String( s ); 
            }
            June 4, 2004 3:01 PM
             
            as david larsson's ahem indicates, extending the standard ISO-C++ string independent of a standard's body is, i think, unlikely at this point. however, as anson's mail indicates, there is lots of thought being given on how to help users interoperate between the native and managed platforms, and david's request for support in this is not at all unreasonable.
            June 17, 2004 9:26 AM
             
            Yves Dolce -- the conscience of the blogs -- rightly pointed out that they two conversion samples needlessly prolong the pinning of the internal System::Char array of the String, and has proposed the following preferred alternatives, 

            bool To_CharStar( String^ source, char*& target ) 



            int len = (( source->Length+1) * 2); 

            target = new char[ len ]; 

            pin_ptr<const wchar_t> wch = PtrToStringChars( source ); 

            return wcstombs( target, wch, len ) != -1; 





            bool To_string( String^ source, string &target ) 



            int len = (( source->Length+1) * 2); 

            char *ch = new char[ len ]; 

            bool result ; 



            pin_ptr<const wchar_t> wch = PtrToStringChars( source ); 

            result = wcstombs( ch, wch, len ) != -1; 



            target = ch; 

            delete ch; 

            return result; 



            June 17, 2004 12:16 PM
             
            In To_string(), shouldn't 

            delete ch; 

            actually be 

            delete[] ch; 

            Since you're allocating an array, you really should use the array deletion expression...
            July 2, 2004 5:00 AM
             
            you are correct. it is a bad habit for an old dog who was programming with the language before this was added, and of course in current implementations, its absence is actually both non-fatal and possibly more efficient.
            July 7, 2004 11:39 AM
            Anonymous comments are disabled
            posted on 2008-11-06 17:31 BeyondCN 閱讀(4610) 評論(1)  編輯 收藏 引用 所屬分類: .NET
            久久精品国产第一区二区三区| 久久久久久狠狠丁香| 久久精品国产第一区二区| 国产精品天天影视久久综合网| 一本一道久久精品综合| 欧美激情精品久久久久久久九九九| 一本久久精品一区二区| 久久综合香蕉国产蜜臀AV| 国内精品久久久久久久久| 亚洲精品NV久久久久久久久久| 亚洲中文久久精品无码| 人人狠狠综合久久亚洲婷婷| 合区精品久久久中文字幕一区| 国产婷婷成人久久Av免费高清| 精品熟女少妇aⅴ免费久久| 亚洲va中文字幕无码久久不卡| 久久久久久a亚洲欧洲aⅴ| 久久亚洲AV无码精品色午夜麻豆 | 99久久无色码中文字幕人妻| 久久久91精品国产一区二区三区| 久久99精品国产麻豆宅宅| 国产免费福利体检区久久| 精品久久久久久亚洲精品| 午夜精品久久久久久影视riav| 青青草原综合久久大伊人精品| 亚洲色大成网站www久久九| 日韩欧美亚洲综合久久影院Ds| 久久99国产精品99久久| 亚洲AV无码成人网站久久精品大| 亚洲精品WWW久久久久久| 国产福利电影一区二区三区,免费久久久久久久精 | 欧美久久一级内射wwwwww.| 久久国产精品成人影院| 亚洲伊人久久精品影院| 精品久久久久久国产| 中文字幕无码免费久久| 欧美日韩精品久久免费| 久久精品国产日本波多野结衣| yy6080久久| 人妻精品久久无码专区精东影业| 无码人妻少妇久久中文字幕蜜桃|