
Posted by William Baldwin on November 10, 203 at 08:17:05:
In Reply to: Converting BSTR to CString posted by SRA on July 10, 203 at 01:12:30:
: I want to know the easy method to convert BSTR to CString
: -sr
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
// BSTR to _bst_t
BSTR bstrStart = GetBSTR();
// use the constructor
_bstr_t bstrFinal(bstrStart);
ShowBSTR(bstrFinal);
// Use the = operator
bstrFinal = bstrStart;
ShowBSTR(bstrFinal);
Converting a _bstr_t to BSTR
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);
Converting a CComBSTR to BSTR
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);
Converting _bstr_t to CComBSTR
// _bstr_t to CComBSTR
_bstr_t bstrStart(_T("This is the test string."));
CComBSTR bstrFinish;
bstrFinish.AppendBSTR(bstrStart);
ShowBSTR(bstrFinish);
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);
Converting _bstr_t to C String
(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);
Converting CComBSTR to LPCTSTR
(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);
Converting LPCTSTR to _bstr_t
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);
Converting LPCTSTR to CComBSTR
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);