반응형

C Language String Types and Classes

String Type

Description

char/wchar/TCHAR The C strings for ANSI and Unicode
CString The C++/MFC class wrapper for C strings
BSTR The Visual Basic string type
_bstr_t A C++ class wrapper for the Visual Basic string type
CComBSTR Yet another C++ class wrapper for the Visual Basic string type used predominately in ATL code

 

Demo Code

 

BSTR to CString and CString to BSTR

 

void BSTRtoCString(BSTR bStr){
    CString csStr =bStr;
}

void CStringtoBSTR()
{
    CString csStr = NULL;

    csStr = "Hello BelieveIT!";

    BSTR bStr = csStr.AllocSysString();

    BSTRtoCString(bStr);

    ::SysFreeString(bStr); //finished using the BSTR

}

 

Base Code

BSTR GetBSTR()
{
    _bstr_t bstrt(_T("This is the test string!"));

    BSTR bstr;

    bstr = bstrt.copy();

    return bstr;
}

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

    return bstr;

}

void ShowBSTR(BSTR bstr)
{
    _bstr_t bstrt(bstr);

    CString cstr;

    cstr.Format(_T("%s"),(LPCTSTR)bstrt);

    AfxMessageBox(cstr);

}

 

Conversions Code

 

_bstr_t BSTRto_bstr_t()
{
    BSTR bstr = GetBSTR();

    _bstr_t bstrFinal(bstr);

    ShowBSTR(bstrFinal);

    bstrFinal = bstr;

    return bstrFinal;
}

 

BSTR _bstr_ttoBSTR()
{
    _bstr_t bstr(_T("This is Converting string"));

    BSTR bstrFinish;

    bstrFinish = bstr.copy();

    ShowBSTR(bstrFinish);

    bstrFinish = bstr;

    return bstrFinish;

}

 

BSTR CComBSTRtoBSTR()
{
    CComBSTR bstr(_T("This is Converting string"));

    BSTR bstrFinish;

    bstrFinish = bstr;

    ShowBSTR(bstrFinish);

    bstrFinish = bstr.Copy();

    return bstrFinish;

}

 

CComBSTR _bstr_ttoCComBSTR()
{
    _bstr_t bstr(_T("This is Converting string"));

    CComBSTR bstrFinish;

    bstrFinish.AppendBSTR(bstr);

    return bstrFinish;

}

 

CString BSTRtoCString()
{
    //conversion that only works in Unicode
    BSTR bstr;

    bstr = GetBSTR();

    TCHAR szFinal[255] = {0,};

    _stprintf(szFinal, _T("%s"), (LPCTSTR)bstr);

    AfxMessageBox(szFinal);

    _bstr_t bstrIntermediate(bstr);

    CString strFinal;

    _stprintf(szFinal, _T("%s"), (LPCTSTR)bstrIntermediate);

    strFinal.Format(_T("%s"),(LPCTSTR)bstrIntermediate);

    return strFinal;

}

 

CString _bstr_ttoCString()
{
    _bstr_t bstr(_T("This is Converting string"));

    TCHAR szFinal[255] = {0,};

    _stprintf(szFinal,_T("%s"),(LPCTSTR)bstr);

    return szFinal;
}

 

LPCTSTR CComBSTRtoLPCTSTR()
{

    CComBSTR bstr("This is Converting string");

    _bstr_t bstrIntermediate(bstr);

    TCHAR szFinal[255] = {0,};

    _stprintf(szFinal, _T("%s"),(LPCTSTR)bstrIntermediate);

    return szFinal;
}

 

_bstr_t LPCTSTRto_bstr_t()
{
    LPCTSTR szStart = _T("This is Converting string");

    _bstr_t bstrFinal(szStart);

    ShowBSTR(bstrFinal);

    bstrFinal = szStart;

    return bstrFinal;
}

 

CComBSTR LPCTSTRtoComBSTR()
{
    LPCTSTR szStart = _T("This is Converting string");

    CComBSTR bstrFinal(szStart);

    ShowBSTR(bstrFinal);

    bstrFinal.Empty();

    bstrFinal.Append(szStart);

    return bstrFinal;
}

 

[ Reference ]

http://blog.naver.com/mismir?Redirect=Log&logNo=40013446819
http://www.codeproject.com/Articles/1282/Easy-way-to-manipulate-BSTR-using-CString-class

반응형

+ Recent posts