CString、TCHAR*、char*转换

<1>CString 转化为 char*
CString->TCHAR*的转化可以用函数GetBuff()
函数原型为:LPTSTR GetBuffer( int nMinBufLength );
TCHAR*->CString的转化
<2>
char 转换TCHAR未写
char*、TCHAR*转换CString
CString str(****)
下面详细写一下其它转换
==========================================================================
evc vc字符串转换处理

1.头文件中要定义宏;  
      #define   UNICODE  
      #define   _UNICODE   
  

char   buf[128];  
  memset(buf,0,128);  
  strcpy(buf,"你好");  
  WCHAR   pCName[128];  
  memset(pCName,0,128);  
  MultiByteToWideChar(CP_THREAD_ACP,MB_USEGLYPHCHARS,buf,strlen(buf),pCName,128);   
   至于WCHAR   转换到CHAR,则用  
  WideCharToMultiByte

//


  2.char转换成wchar
  
      const   char   *pFilePathName   =   "c://aa.dll";  
      int   nLen   =   strlen(pFilePathName)   +   1;  
      int   nwLen   =   MultiByteToWideChar(CP_ACP,   0,   pFilePathName,  
nLen,   NULL,   0);  
   
      TCHAR   lpszFile[256];  
      MultiByteToWideChar(CP_ACP,   0,   pFilePathName,   nLen,   lpszFile,  
nwLen);  
   
  3.wchar转换成char  
        char   *pFilePathName;  
        TCHAR   lpszFile[256];  
      _tcscpy(lpszFile,   _T("c://aa.dll"));  
   
      int   nLen   =   wcslen(wstr)+1;    
      WideCharToMultiByte(CP_ACP,   0,   lpszFile,   nLen,   pFilePathName,  
2*nLen,   NULL,   NULL);
  

char*和CString转换
CString 是一种很特殊的 C++ 对象,它里面包含了三个值:一个指向某个数据缓冲区的指针、一个是该缓冲中有效的字符记数(它是不可存取的,是位于
CString 地址之下的一个隐藏区域)以及一个缓冲区长度。
有效字符数的大小可以是从0到该缓冲最大长度值减1之间的任何数(因为字符串结尾有一个NULL字符)。字符记数和缓冲区长度被巧妙隐藏。

(1) char*转换成CString
  若将char*转换成CString,除了直接赋值外,还可使用CString::Format进行。例如:
         char chArray[] = "Char  test";
         TCHAR * p = _T("Char  test");( 或LPTSTR p = _T("Char  test");)
         CString theString = chArray;
         theString.Format(_T("%s"), chArray);
         theString = p;

(2) CString转换成char*

  若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法:

  方法一,使用强制转换。例如:

       CString theString( (_T("Char test "));
        LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;

  方法二,使用strcpy。例如:

       CString theString( (_T("Char test "));
       LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
        _tcscpy(lpsz, theString);

  需要说明的是,strcpy(或可移值的_tcscpy)的第二个参数是 const wchar_t* (Unicode)或const char*
(ANSI),系统编译器将会自动对其进行转换。

  方法三,使用CString::GetBuffer。
        如果你需要修改 CString 中的内容,它有一个特殊的方法可以使用,那就是 GetBuffer,它的作用是返回一个可写的缓冲指针。
如果你只是打算修改字符或者截短字符串,例如:
       CString s(_T("Char test "));
        LPTSTR p = s.GetBuffer();
        LPTSTR dot = strchr(p, ''.'');
         // 在这里添加使用p的代码
          if(p != NULL)
         *p = _T('/0');
         s.ReleaseBuffer();                     // 使用完后及时释放,以便能使用其它的CString成员函数
         在 GetBuffer 和 ReleaseBuffer 之间这个范围,一定不能使用你要操作的这个缓冲的 CString 对象的任何方法。因为
ReleaseBuffer 被调用之前,该 CString 对象的完整性得不到保障。

CString ,BSTR ,LPCTSTR之间关系和区别

CString是一个动态TCHAR数组,BSTR是一种专有格式的字符串(需要用系统提供的函数来操纵,LPCTSTR只是一个常量的TCHAR指针。

                              CString 是一个完全独立的类,动态的TCHAR数组,封装了 + 等操作符和字符串操作方法。
                              typedef OLECHAR FAR* BSTR;
                              typedef const char * LPCTSTR;

                              vc++中各种字符串的表示法
                              首先char*
                              是指向ANSI字符数组的指针,其中每个字符占据8位(有效数据是除掉最高位的其他7位),这里保持了与传统的C,C++的兼容。
                              LP的含义是长指针(long
                              pointer)。LPSTR是一个指向以‘/0’结尾的ANSI字符数组的指针,与char*可以互换使用,在win32中较多地使用LPSTR。
                              而LPCSTR中增加的‘C’的含义是“CONSTANT”(常量),表明这种数据类型的实例不能被使用它的API函数改变,除此之外,它与LPSTR是等同的。
                              1.LP表示长指针,在win16下有长指针(LP)和短指针(P)的区别,而在win32下是没有区别的,都是32位.所以这里的LP和P是等价的.
                              2.C表示const
                              3.T是什么东西呢,我们知道TCHAR在采用Unicode方式编译时是wchar_t,在普通时编译成char.
                              为了满足程序代码国际化的需要,业界推出了Unicode标准,它提供了一种简单和一致的表达字符串的方法,所有字符中的字节都是16位的值,其数量也可以满足差不多世界上所有书面语言字符的编码需求,开发程序时使用Unicode(类型为wchar_t)是一种被鼓励的做法。
                              LPWSTR与LPCWSTR由此产生,它们的含义类似于LPSTR与LPCSTR,只是字符数据是16位的wchar_t而不是char。
                              然后为了实现两种编码的通用,提出了TCHAR的定义:
                              如果定义_UNICODE,声明如下:
                              typedef wchar_t TCHAR;
                              如果没有定义_UNICODE,则声明如下:
                              typedef char TCHAR;
                              LPTSTR和LPCTSTR中的含义就是每个字符是这样的TCHAR。
                              CString类中的字符就是被声明为TCHAR类型的,它提供了一个封装好的类供用户方便地使用。
                              LPCTSTR:
                                   #ifdef _UNICODE
                                      typedef const wchar_t * LPCTSTR;
                                   #else
                                      typedef const char * LPCTSTR;
                                   #endif
                              VC常用数据类型使用转换详解
                              
                              先定义一些常见类型变量借以说明
                              int i = 100;
                              long l = 2001;
                              float f=300.2;
                              double d=12345.119;
                              char username[]="女侠程佩君";
                              char temp[200];
                              char *buf;
                              CString str;
                              _variant_t v1;
                              _bstr_t v2;
                 一、其它数据类型转换为字符串
                              短整型(int)
                                     itoa(i,temp,10);    
                              //将i转换为字符串放入temp中,最后一个数字表示十进制
                                     itoa(i,temp,2);      //按二进制方式转换
                              长整型(long)
                                     ltoa(l,temp,10);

                 二、从其它包含字符串的变量中获取指向该字符串的指针
                              CString变量
                              str = "2008北京奥运";
                              buf = (LPSTR)(LPCTSTR)str;
                              BSTR类型的_variant_t变量
                              v1 = (_bstr_t)"程序员";
                              buf = _com_util::ConvertBSTRToString((_bstr_t)v1);
                 三、字符串转换为其它数据类型
                              strcpy(temp,"123");
                              短整型(int)
                                    i = atoi(temp);
                              长整型(long)
                                    l = atol(temp);
                              浮点(double)
                                    d = atof(temp);
                   四、其它数据类型转换到CString
                              使用CString的成员函数Format来转换,例如:
                              整数(int)
                                    str.Format("%d",i);
                              浮点数(float)
                                    str.Format("%f",i);
                              字符串指针(char *)等已经被CString构造函数支持的数据类型可以直接赋值
                                    str = username;
                      五、BSTR、_bstr_t与CComBSTR
                              CComBSTR、_bstr_t是对BSTR的封装,BSTR是指向字符串的32位指针。
                              char *转换到BSTR可以这样: BSTR
                              b=_com_util::ConvertStringToBSTR("数据");    
                              //使用前需要加上头文件comutil.h
                              反之可以使用char *p=_com_util::ConvertBSTRToString(b);

                        六、VARIANT 、_variant_t 与 COleVariant
                              VARIANT的结构可以参考头文件VC98/Include/OAIDL.H中关于结构体tagVARIANT的定义。
                              对于VARIANT变量的赋值:首先给vt成员赋值,指明数据类型,再对联合结构中相同数据类型的变量赋值,举个例子:
                              VARIANT va;
                              int a=2001;
                              va.vt=VT_I4;    //指明整型数据
                              va.lVal=a;      //赋值
                              对于不马上赋值的VARIANT,最好先用Void VariantInit(VARIANTARG
                              FAR*
                              pvarg);进行初始化,其本质是将vt设置为VT_EMPTY,下表我们列举vt与常用数据的对应关系:
                              unsigned char bVal; VT_UI1
                              short iVal; VT_I2
                              long lVal;  VT_I4 
                              float fltVal;  VT_R4
                              double dblVal;  VT_R8 
                              VARIANT_BOOL boolVal;  VT_BOOL
                              SCODE scode;  VT_ERROR
                              CY cyVal;  VT_CY
                              DATE date;  VT_DATE
                              BSTR bstrVal;  VT_BSTR
                              IUnknown FAR* punkVal;  VT_UNKNOWN
                              IDispatch FAR* pdispVal;  VT_DISPATCH
                              SAFEARRAY FAR* parray;  VT_ARRAY|*
                              unsigned char FAR* pbVal;  VT_BYREF|VT_UI1
                              short FAR* piVal;  VT_BYREF|VT_I2
                              long FAR* plVal;  VT_BYREF|VT_I4
                              float FAR* pfltVal;  VT_BYREF|VT_R4
                              double FAR* pdblVal; VT_BYREF|VT_R8
                              VARIANT_BOOL FAR* pboolVal; VT_BYREF|VT_BOOL
                              SCODE FAR* pscode;  VT_BYREF|VT_ERROR
                              CY FAR* pcyVal;  VT_BYREF|VT_CY
                              DATE FAR* pdate; VT_BYREF|VT_DATE
                              BSTR FAR* pbstrVal;  VT_BYREF|VT_BSTR
                              IUnknown FAR* FAR* ppunkVal;  VT_BYREF|VT_UNKNOWN
                              IDispatch FAR* FAR* ppdispVal;
                              VT_BYREF|VT_DISPATCH
                              SAFEARRAY FAR* FAR* pparray;  VT_ARRAY|*
                              VARIANT FAR* pvarVal;  VT_BYREF|VT_VARIANT
                              void FAR* byref;  VT_BYREF
                              _variant_t是VARIANT的封装类,其赋值可以使用强制类型转换,其构造函数会自动处理这些数据类型。
                              例如:
                              long l=222;
                              ing i=100;
                              _variant_t lVal(l);
                              lVal = (long)i;
                              COleVariant的使用与_variant_t的方法基本一样,请参考如下例子:
                              COleVariant v3 = "字符串", v4 = (long)1999;
                              CString str =(BSTR)v3.pbstrVal;
                              long i = v4.lVal;

//
  • 本主题演示如何将各种 C++ 字符串类型转换为其他字符串。可以转换的字符串类型包括 char *wchar_t*_bstr_tCComBSTRCStringbasic_stringSystem.String。在所有情况下,在将字符串转换为新类型时,都会创建字符串的副本。对新字符串进行的任何更改都不会影响原始字符串,反之亦然。

    从 char * 转换


    示例

    说明

    此示例演示如何从 char * 转换为上面列出的其他字符串类型。

    // convert_from_char.cpp
    // compile with /clr /link comsuppw.lib
    
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    #include "atlbase.h"
    #include "atlstr.h"
    #include "comutil.h"
    
    using namespace std;
    using namespace System;
    
    int main()
    {
        char *orig = "Hello, World!";
        cout << orig << " (char *)" << endl;
    
        // Convert to a wchar_t*
        size_t origsize = strlen(orig) + 1;
        const size_t newsize = 100;
        size_t convertedChars = 0;
        wchar_t wcstring[newsize];
        mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
        wcscat_s(wcstring, L" (wchar_t *)");
        wcout << wcstring << endl;
    
        // Convert to a _bstr_t
        _bstr_t bstrt(orig);
        bstrt += " (_bstr_t)";
        cout << bstrt << endl;
    
        // Convert to a CComBSTR
        CComBSTR ccombstr(orig);
        if (ccombstr.Append(L" (CComBSTR)") == S_OK)
        {
            CW2A printstr(ccombstr);
            cout << printstr << endl;
        }
    
        // Convert to a CString
        CString cstring(orig);
        cstring += " (CString)";
        cout << cstring << endl;
    
        // Convert to a basic_string
        string basicstring(orig);
        basicstring += " (basic_string)";
        cout << basicstring << endl;
    
        // Convert to a System::String
        String ^systemstring = gcnew String(orig);
        systemstring += " (System::String)";
        Console::WriteLine("{0}", systemstring);
        delete systemstring;
    }

    输出

    Hello, World! (char *)
    Hello, World! (wchar_t *)
    Hello, World! (_bstr_t)
    Hello, World! (CComBSTR)
    Hello, World! (CString)
    Hello, World! (basic_string)
    Hello, World! (System::String)

    从 wchar_t * 转换


    示例

    说明

    此示例演示如何从 wchar_t * 转换为上面列出的其他字符串类型。

    // convert_from_wchar_t.cpp
    // compile with /clr /link comsuppw.lib
    
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    #include "atlbase.h"
    #include "atlstr.h"
    #include "comutil.h"
    
    using namespace std;
    using namespace System;
    
    int main()
    {
        wchar_t *orig = L"Hello, World!";
        wcout << orig << L" (wchar_t *)" << endl;
    
        // Convert to a char*
        size_t origsize = wcslen(orig) + 1;
        const size_t newsize = 100;
        size_t convertedChars = 0;
        char nstring[newsize];
        wcstombs_s(&convertedChars, nstring, origsize, orig, _TRUNCATE);
        strcat_s(nstring, " (char *)");
        cout << nstring << endl;
    
        // Convert to a _bstr_t
        _bstr_t bstrt(orig);
        bstrt += " (_bstr_t)";
        cout << bstrt << endl;
    
        // Convert to a CComBSTR
        CComBSTR ccombstr(orig);
        if (ccombstr.Append(L" (CComBSTR)") == S_OK)
        {
            CW2A printstr(ccombstr);
            cout << printstr << endl;
        }
    
        // Convert to a CString
        CString cstring(orig);
        cstring += " (CString)";
        cout << cstring << endl;
    
        // Convert to a basic_string
        wstring basicstring(orig);
        basicstring += L" (basic_string)";
        wcout << basicstring << endl;
    
        // Convert to a System::String
        String ^systemstring = gcnew String(orig);
        systemstring += " (System::String)";
        Console::WriteLine("{0}", systemstring);
        delete systemstring;
    }

    输出

    Hello, World! (wchar_t *)
    Hello, World! (char *)
    Hello, World! (_bstr_t)
    Hello, World! (CComBSTR)
    Hello, World! (CString)
    Hello, World! (basic_string)
    Hello, World! (System::String)
  • ============================================================
  •          
             

    从 _bstr_t 转换


    示例

    说明

    此示例演示如何从 _bstr_t 转换为上面列出的其他字符串类型。

    // convert_from_bstr_t.cpp
    // compile with /clr /link comsuppw.lib
    
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    #include "atlbase.h"
    #include "atlstr.h"
    #include "comutil.h"
    
    using namespace std;
    using namespace System;
    
    int main()
    {
        _bstr_t orig("Hello, World!");
        wcout << orig << " (_bstr_t)" << endl;
    
        // Convert to a char*
        const size_t newsize = 100;
        char nstring[newsize];
        strcpy_s(nstring, (char *)orig);
        strcat_s(nstring, " (char *)");
        cout << nstring << endl;
    
        // Convert to a wchar_t*
        wchar_t wcstring[newsize];
        wcscpy_s(wcstring, (wchar_t *)orig);
        wcscat_s(wcstring, L" (wchar_t *)");
        wcout << wcstring << endl;
    
        // Convert to a CComBSTR
        CComBSTR ccombstr((char *)orig);
        if (ccombstr.Append(L" (CComBSTR)") == S_OK)
        {
            CW2A printstr(ccombstr);
            cout << printstr << endl;
        }
    
        // Convert to a CString
        CString cstring((char *)orig);
        cstring += " (CString)";
        cout << cstring << endl;
    
        // Convert to a basic_string
        string basicstring((char *)orig);
        basicstring += " (basic_string)";
        cout << basicstring << endl;
    
        // Convert to a System::String
        String ^systemstring = gcnew String((char *)orig);
        systemstring += " (System::String)";
        Console::WriteLine("{0}", systemstring);
        delete systemstring;
    }

    输出

    Hello, World! (_bstr_t)
    Hello, World! (char *)
    Hello, World! (wchar_t *)
    Hello, World! (CComBSTR)
    Hello, World! (CString)
    Hello, World! (basic_string)
    Hello, World! (System::String)

    从 CComBSTR 转换


    示例

    说明

    此示例演示如何从 CComBSTR 转换为上面列出的其他字符串类型。

    // convert_from_ccombstr.cpp
    // compile with /clr /link comsuppw.lib
    
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    #include "atlbase.h"
    #include "atlstr.h"
    #include "comutil.h"
    #include "vcclr.h"
    
    using namespace std;
    using namespace System;
    using namespace System::Runtime::InteropServices;
    
    int main()
    {
        CComBSTR orig("Hello, World!");
        CW2A printstr(orig);
        cout << printstr << " (CComBSTR)" << endl;
    
        // Convert to a char*
        const size_t newsize = 100;
        char nstring[newsize];
        CW2A tmpstr1(orig);
        strcpy_s(nstring, tmpstr1);
        strcat_s(nstring, " (char *)");
        cout << nstring << endl;
    
        // Convert to a wchar_t*
        wchar_t wcstring[newsize];
        wcscpy_s(wcstring, orig);
        wcscat_s(wcstring, L" (wchar_t *)");
        wcout << wcstring << endl;
    
        // Convert to a _bstr_t
        _bstr_t bstrt(orig);
        bstrt += " (_bstr_t)";
        cout << bstrt << endl;
    
        // Convert to a CString
        CString cstring(orig);
        cstring += " (CString)";
        cout << cstring << endl;
    
        // Convert to a basic_string
        wstring basicstring(orig);
        basicstring += L" (basic_string)";
        wcout << basicstring << endl;
    
        // Convert to a System::String
        String ^systemstring = gcnew String(orig);
        systemstring += " (System::String)";
        Console::WriteLine("{0}", systemstring);
        delete systemstring;
    }

    输出

    Hello, World! (CComBSTR)
    Hello, World! (char *)
    Hello, World! (wchar_t *)
    Hello, World! (_bstr_t)
    Hello, World! (CString)
    Hello, World! (basic_string)
    Hello, World! (System::String)

    从 CString 转换


    示例

    说明

    此示例演示如何从 CString 转换为上面列出的其他字符串类型。

    // convert_from_cstring.cpp
    // compile with /clr /link comsuppw.lib
    
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    #include "atlbase.h"
    #include "atlstr.h"
    #include "comutil.h"
    
    using namespace std;
    using namespace System;
    
    int main()
    {
        CString orig("Hello, World!");
        wcout << orig << " (CString)" << endl;
    
        // Convert to a char*
        const size_t newsize = 100;
        char nstring[newsize];
        strcpy_s(nstring, orig);
        strcat_s(nstring, " (char *)");
        cout << nstring << endl;
    
        // Convert to a wchar_t*
        // You must first convert to a char * for this to work.
        size_t origsize = strlen(orig) + 1;
        size_t convertedChars = 0;
        wchar_t wcstring[newsize];
        mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
        wcscat_s(wcstring, L" (wchar_t *)");
        wcout << wcstring << endl;
    
        // Convert to a _bstr_t
        _bstr_t bstrt(orig);
        bstrt += " (_bstr_t)";
        cout << bstrt << endl;
    
        // Convert to a CComBSTR
        CComBSTR ccombstr(orig);
        if (ccombstr.Append(L" (CComBSTR)") == S_OK)
        {
            CW2A printstr(ccombstr);
            cout << printstr << endl;
        }
    
        // Convert to a basic_string
        string basicstring(orig);
        basicstring += " (basic_string)";
        cout << basicstring << endl;
    
        // Convert to a System::String
        String ^systemstring = gcnew String(orig);
        systemstring += " (System::String)";
        Console::WriteLine("{0}", systemstring);
        delete systemstring;
    }

    输出

    Hello, World! (CString)
    Hello, World! (char *)
    Hello, World! (wchar_t *)
    Hello, World! (_bstr_t)
    Hello, World! (CComBSTR)
    Hello, World! (basic_string)
    Hello, World! (System::String)

    从 basic_string 转换


    示例

    说明

    此示例演示如何从 basic_string 转换为上面列出的其他字符串类型。

    // convert_from_basic_string.cpp
    // compile with /clr /link comsuppw.lib
    
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    #include "atlbase.h"
    #include "atlstr.h"
    #include "comutil.h"
    
    using namespace std;
    using namespace System;
    
    int main()
    {
        string orig("Hello, World!");
        cout << orig << " (basic_string)" << endl;
    
        // Convert to a char*
        const size_t newsize = 100;
        char nstring[newsize];
        strcpy_s(nstring, orig.c_str());
        strcat_s(nstring, " (char *)");
        cout << nstring << endl;
    
        // Convert to a wchar_t*
        // You must first convert to a char * for this to work.
        size_t origsize = strlen(orig.c_str()) + 1;
        size_t convertedChars = 0;
        wchar_t wcstring[newsize];
        mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
        wcscat_s(wcstring, L" (wchar_t *)");
        wcout << wcstring << endl;
    
        // Convert to a _bstr_t
        _bstr_t bstrt(orig.c_str());
        bstrt += " (_bstr_t)";
        cout << bstrt << endl;
    
        // Convert to a CComBSTR
        CComBSTR ccombstr(orig.c_str());
        if (ccombstr.Append(L" (CComBSTR)") == S_OK)
        {
            CW2A printstr(ccombstr);
            cout << printstr << endl;
        }
    
        // Convert to a CString
        CString cstring(orig.c_str());
        cstring += " (CString)";
        cout << cstring << endl;
    
        // Convert to a System::String
        String ^systemstring = gcnew String(orig.c_str());
        systemstring += " (System::String)";
        Console::WriteLine("{0}", systemstring);
        delete systemstring;
    }

    输出

    Hello, World! (basic_string)
    Hello, World! (char *)
    Hello, World! (wchar_t *)
    Hello, World! (_bstr_t)
    Hello, World! (CComBSTR)
    Hello, World! (CString)
    Hello, World! (System::String)

    从 System::String 转换


    示例

    说明

    此示例演示如何从 System.String 转换为上面列出的其他字符串类型。

    // convert_from_system_string.cpp
    // compile with /clr /link comsuppw.lib
    
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    #include "atlbase.h"
    #include "atlstr.h"
    #include "comutil.h"
    #include "vcclr.h"
    
    using namespace std;
    using namespace System;
    using namespace System::Runtime::InteropServices;
    
    int main()
    {
        String ^orig = gcnew String("Hello, World!");
        Console::WriteLine("{0} (System::String)", orig);
    
        pin_ptr<const wchar_t> wch = PtrToStringChars(orig);
    
        // Convert to a char*
        size_t origsize = wcslen(wch) + 1;
        const size_t newsize = 100;
        size_t convertedChars = 0;
        char nstring[newsize];
        wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
        strcat_s(nstring, " (char *)");
        cout << nstring << endl;
    
        // Convert to a wchar_t*
        wchar_t wcstring[newsize];
        wcscpy_s(wcstring, wch);
        wcscat_s(wcstring, L" (wchar_t *)");
        wcout << wcstring << endl;
    
        // Convert to a _bstr_t
        _bstr_t bstrt(wch);
        bstrt += " (_bstr_t)";
        cout << bstrt << endl;
    
        // Convert to a CComBSTR
        CComBSTR ccombstr(wch);
        if (ccombstr.Append(L" (CComBSTR)") == S_OK)
        {
            CW2A printstr(ccombstr);
            cout << printstr << endl;
        }
    
        // Convert to a CString
        CString cstring(wch);
        cstring += " (CString)";
        cout << cstring << endl;
    
        // Convert to a basic_string
        wstring basicstring(wch);
        basicstring += L" (basic_string)";
        wcout << basicstring << endl;
    
        delete orig;
    }

    输出

    Hello, World! (System::String)
    Hello, World! (char *)
    Hello, World! (wchar_t *)
    Hello, World! (_bstr_t)
    Hello, World! (CComBSTR)
    Hello, World! (CString)
    Hello, World! (basic_string)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值