C++/COM string conversion

char* <--> wchar_t*
1. sprintf(desWChars, "%s", srcChars);
   swprintf(desChars, "%S", srcWChars);
2. wchar_t *desWChars = A2W(srcChars); // ATL convention.
   char *desChars = W2A(srcWChars);    // ATL convention.
   Note: A2W&W2A uses "alloca" to alloc memory from stack. So don't release the pointer.
3. Safe way:
   WideCharToMultiByte
4. CW2A pszConverted (bstrSrc);

5. int converted = wcstombs(pChars, pWChars, MB_CUR_MAX);
    int converted = mbstowcs(pWChars, pChars, MB_CUR_MAX);


STL string <--> char*
   char *pChars = stlString.c_str();
   string stlString(pChars)  // Construct Func

char* --> OLECHAR*
1. Simple but Not safe:
   #include <atlconv.h>
   USES_CONVERSION; // This is necessary.
   BSTR str = A2OLE(pChar);
   char *pChar = OLE2A(str); // pChar can't be freed by user. It is in stack.
        // Pay attention to "stack overflow" exception.
2. Safe:
   WideCharToMultiByte

STL wstring <--> BSTR
   std::wstring wstrAnsiVersion (bstrMyBeaster);

BSTR --> _bstr_t
    _bstr_t bstrDes(bstrSrc); // Constructor.
    bstrDes = bstrSrc;       // Operator "=" function
_bstr_t --> BSTR
    BSTR bstrDes = bstrSrc.copy();
    BSTR bstrDes = bstrSrc;   // Operator "="

//
Properties -> Configuration Properties -> General -> Use of ATL.

LPSTR = char*
LPCSTR = const char*
LPWSTR = wchar_t*
LPCWSTR = const wchar_t*
LPTSTR = char* or wchar_t* depending on _UNICODE
LPCTSTR = const char* or const wchar_t* depending on _UNICODE

C Language String Types and Classes

This article deals with the following C/MFC/ATL string types:

String TypeDescription
char/wchar/TCHAR  The C strings for ANSI and Unicode
CStringThe C++/MFC class wrapper for C strings
BSTRThe Visual Basic string type
_bstr_tA C++ class wrapper for the Visual Basic string type
CComBSTRYet another C++ class wrapper for the Visual Basic string type used predominately in ATL code

Demo Project

The demo project is just an MFC dialog-based application with buttons for each type of conversion. It is built using VC++ 6.0. It uses a couple of support functions you may find helpful:

BSTR GetBSTR()
{
  _bstr_t bstr1(_T("This is the test string."));

  BSTR bstr;

  bstr = bstr1.copy();

  return bstr;
}




CComBSTR GetComBSTR()
{
  CComBSTR bstr("This is the test string.");

  return bstr;
}


void CVbsDlg::ShowBSTR(BSTR bstr)
{
  _bstr_t bstrStart(bstr);

  CString s;

  s.Format(_T("%s"), (LPCTSTR)bstrStart);

  AfxMessageBox(s);

}

Conversions

So, let's get to it. Here are the conversion techniques:

    • Converting BSTR to _bstr_t
    • Converting a _bstr_t to BSTR
    • Converting a CComBSTR to BSTR
    • Converting _bstr_t to CComBSTR
    • Converting BSTR to C String

       

      (Note: conversion that only works in Unicode)

        // BSTR to C String
      
        BSTR bstrStart;
      
        bstrStart = GetBSTR();
      
        TCHAR szFinal[255];
      
        // direct conversion from BSTR to LPCTSTR only works
        // in Unicode
        _stprintf(szFinal, _T("%s"), (LPCTSTR)bstrStart);
        AfxMessageBox(szFinal);
      
        _bstr_t bstrIntermediate(bstrStart);    // convert to
                                                // _bstr_t
        CString strFinal;
      
        // you have to go through _bstr_t to have it work in ANSI
        // and Unicode
        _stprintf(szFinal, _T("%s"), (LPCTSTR)bstrIntermediate);
      
        // Or, using MFC
      
        strFinal.Format(_T("%s"), (LPCTSTR)bstrIntermediate);
      
        AfxMessageBox(strFinal);
      

       

      (this works in both ANSI and Unicode)

        _bstr_t bstrStart(_T("This is the test string."));
        TCHAR szFinal[255];
      
        _stprintf(szFinal, _T("%s"), (LPCTSTR)bstrStart);
      
        AfxMessageBox(szFinal);
      

       

      (not possible; must go through _bstr_t)

        // CComBSTR to C String
        CComBSTR bstrStart("This is the test string.");
      
        _bstr_t bstrIntermediate(bstrStart);
      
        TCHAR szFinal[255];
      
        _stprintf(szFinal, _T("%s"),
                  (LPCTSTR)bstrIntermediate);
      
        AfxMessageBox(szFinal);
      

       

      Use a constructor or = operator

        // LPCTSTR to _bstr_t
      
        LPCTSTR szStart = _T("This is the text string");
      
        // Use the constructor
        _bstr_t bstrFinal(szStart);
      
        ShowBSTR(bstrFinal);
      
        // or use = operator
        bstrFinal = szStart;
      
        ShowBSTR(bstrFinal);
      

       

      Use a constructor or CComBSTR::Append function

        // LPCTSTR to CComBSTR
      
        // Use a constructor
      
        LPCTSTR szStart = _T("This is the text string");
      
        // Use the constructor
        CComBSTR bstrFinal(szStart);
      
        ShowBSTR(bstrFinal);
      
        // Or use the Append function
        bstrFinal.Empty();
        bstrFinal.Append(szStart);
      
        ShowBSTR(bstrFinal);
      
    • Converting _bstr_t to C String
    • Converting CComBSTR to LPCTSTR
    • Converting LPCTSTR to _bstr_t
    • Converting LPCTSTR to CComBSTR
      // BSTR to _bst_t
    
      BSTR bstrStart = GetBSTR();
    
      // use the constructor
      _bstr_t bstrFinal(bstrStart);
    
      ShowBSTR(bstrFinal);
    
      // Use the = operator
      bstrFinal = bstrStart;
    
      ShowBSTR(bstrFinal);
    

     

    You may want to get a BSTR from a _bstr_t class.

      // _bstr_t to BSTR
    
      _bstr_t bstrStart(_T("This is the test string."));
    
      BSTR bstrFinish;
    
      // use _bstr_t::copy member function
      bstrFinish = bstrStart.copy();
    
      ShowBSTR(bstrFinish);
    
      // use = operator
      bstrFinish = bstrStart;
    
      ShowBSTR(bstrFinish);
    

     

    You may want to get a BSTR from a CComBSTR class.

      // CComBSTR to BSTR
      CComBSTR bstrStart(_T("This is the test string."));
    
      BSTR bstrFinish;
    
      // use the = operator
      bstrFinish = bstrStart;
    
      ShowBSTR(bstrFinish);
    
      // use the Copy member function
      bstrFinish = bstrStart.Copy();
    
      ShowBSTR(bstrFinish);
    

     

      // _bstr_t to CComBSTR
      _bstr_t bstrStart(_T("This is the test string."));
    
      CComBSTR bstrFinish;
    
      bstrFinish.AppendBSTR(bstrStart);
    
      ShowBSTR(bstrFinish);
    
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值