65.9K
CodeProject 正在变化。 阅读更多。
Home

BSTR 和 C 字符串转换指南

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.22/5 (33投票s)

2003年8月22日

2分钟阅读

viewsIcon

467354

downloadIcon

2413

一篇关于在C字符串和各种VB BSTR字符串类型之间进行转换的文章

引言

Windows编程中一个令人困惑的方面是管理Visual Basic风格字符串与C语言风格字符串之间的转换。它并非难于理解,只是细节难以记住,通常不经常做,MSDN文档过于庞大,难以找到问题的答案。但最糟糕的是,你可能会执行一些编译正常的类型转换,但其工作方式并非你预期的那样。这会导致代码无法工作,并且错误难以追踪。经过一些经验后,你就会学会确保你的字符串转换符合你的预期。

C字符串是由以NULL字符结尾的字符数组组成。Visual Basic字符串的不同之处在于字符串的长度位于字符串字符之前。因此,VB字符串知道它自己的长度。此外,所有VB字符串都是Unicode(每个字符16位)。

字符串类型

如果需要BSTR/C字符串转换:

  • 你在使用C/C++进行COM编程
  • 你正在编写多语言应用程序,例如由Visual Basic应用程序访问的C++ DLL。

C语言字符串类型和类

本文涉及以下C/MFC/ATL字符串类型:

  • char/wchar/TCHAR -- ANSI和Unicode的C字符串
  • CString -- C字符串的C++/MFC类包装器
  • BSTR -- Visual Basic字符串类型
  • _bstr_t -- Visual Basic字符串类型的C++类包装器
  • CComBSTR -- 另一个Visual Basic字符串类型的C++类包装器,主要用于ATL代码

演示项目

演示项目只是一个基于MFC对话框的应用程序,其中包含每个类型转换的按钮。它使用VC++ 6.0构建。它使用一些你可能会觉得有用的支持函数。

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);

}

转换

让我们开始吧。以下是转换技术:

将BSTR转换为_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);


将_bstr_t转换为BSTR

你可能想要从_bstr_t类获取一个BSTR

    // _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);

 

将CComBSTR转换为BSTR

你可能想要从CComBSTR类获取一个BSTR

    // 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转换为CComBSTR

    // _bstr_t to CComBSTR
    _bstr_t bstrStart(_T("This is the test string."));

    CComBSTR bstrFinish;  

    bstrFinish.AppendBSTR(bstrStart);

    ShowBSTR(bstrFinish);

将BSTR转换为C字符串

(注意:-仅在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);

将_bstr_t转换为C字符串

(这在ANSI和Unicode中都有效)

    _bstr_t bstrStart(_T("This is the test string.")); 
    TCHAR szFinal[255];

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

    AfxMessageBox(szFinal);
    

将CComBSTR转换为LPCTSTR

(不可能,必须通过_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);

将LPCTSTR转换为_bstr_t

(使用构造函数或=运算符)

    // 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);

将LPCTSTR转换为CComBSTR

使用构造函数或CComBSTR::Append函数

    // 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);

结论

我在演示项目中测试了所有转换。如果你需要尝试其他转换,请下载演示项目以便轻松修改。我相信如果有什么错误,我会听到的!

© . All rights reserved.