Click here to Skip to main content
15,397,981 members
Articles / Desktop Programming / MFC
Posted 21 Aug 2003


98 bookmarked

Guide to BSTR and C String Conversions

Rate me:
Please Sign up or sign in to vote.
4.22/5 (35 votes)
21 Aug 20032 min read
An article on converting to/from C strings and various VB BSTR string types


One of the confusing aspects of Windows programming is managing the conversion of Visual Basic style strings to/from C language style strings.  It isn't that it is so difficult, it is just difficult to remember the details, it is usually not done often, the MSDN documentation so voluminous that it is difficult to find answers to your questions.  But the worst part is that you could perform some typecast that compiles fine, but doesn't work the way you expect.  This results code that doesn't work, and the bugs are hard to track down.  After some experience, you learn to make sure your string conversions are doing what you expect.

C strings are arrays of characters terminated by a NULL character.  Visual Basic strings differ in that the length of the string preceded the characters in the string.  So a VB string knows it's own length.  In addition, all VB strings are Unicode (16 bits per character).

String Types

BSTR/C String conversions are required if:

  • You are doing COM programming in C/C++
  • You are writing multiple language applications, such as C++ DLL's accessed by Visual Basic applications. 

C Language String Types and Classes

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

  • 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 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_t bstr1(_T("This is the test string."));
    BSTR bstr;

    bstr = bstr1.copy();

    return bstr;

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




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


// Use the = operator
bstrFinal = bstrStart;


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


// use = operator
bstrFinish = bstrStart;


Converting a CComBSTR to BSTR

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

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

BSTR bstrFinish;

// use the = operator
bstrFinish = bstrStart;


// use the Copy member function
bstrFinish = bstrStart.Copy();


 Converting _bstr_t to CComBSTR

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

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

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


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


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


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


// or use = operator
bstrFinal = szStart;


 Converting LPCTSTR to CComBSTR

Use a constructor or CComBSTR::Append function


// Use a constructor

LPCTSTR szStart = _T("This is the text string");

// Use the constructor
CComBSTR bstrFinal(szStart);


// Or use the Append function



Well I tested all of the conversion in the demo project.  If you need to try others, download the demo for easy modification.  I am sure I will hear if there are any mistakes!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Robert Pittenger, MCPD-EAD
President Starpoint Software Inc.
United States United States
Bob Pittenger is founder and President of Starpoint Software Inc. He holds a B.A. degree from Miami University, M.S. and Ph.D. degrees from Purdue University, and an MBA from Xavier University. He has been programming since 1993, starting with Windows application development in C++/MFC and moving to C# and .NET around 2005 and is a .NET Microsoft Certified Professional Developer.

Bob is the author of two books:
Billionaire: How the Ultra-Rich Built Their Fortunes Through Good and Evil and What You Can Learn from Them
Wealthonomics: The Most Important Economic and Financial Concepts that Can Make You Rich Fast.
Visit for more information.

Comments and Discussions

QuestionWhat about CString to - well anything? Pin
Dave Midgley21-Jun-16 3:27
MemberDave Midgley21-Jun-16 3:27 
QuestionHow to convert BSTR to Char [] Pin
wachira anjana22-Aug-13 22:34
Memberwachira anjana22-Aug-13 22:34 
AnswerRe: How to convert BSTR to Char [] Pin
wachira anjana22-Aug-13 22:49
Memberwachira anjana22-Aug-13 22:49 
Generalchar array to BSTR Pin
Anandi.VC2-Apr-08 5:31
MemberAnandi.VC2-Apr-08 5:31 
QuestionWill GetComBSTR() crash? Pin
zli9818-Sep-06 12:44
Memberzli9818-Sep-06 12:44 
AnswerRe: Will GetComBSTR() crash? Pin
Birendar10-Jan-11 1:25
MemberBirendar10-Jan-11 1:25 
GeneralBSTR to char* [modified] Pin
Xaoc_MK27-Jul-06 3:53
MemberXaoc_MK27-Jul-06 3:53 
QuestionLPCSTR ??? Pin
ajay _tiwari12-Jul-06 20:49
Memberajay _tiwari12-Jul-06 20:49 
AnswerRe: LPCSTR ??? Pin
cooldanny16-Jan-07 3:48
Membercooldanny16-Jan-07 3:48 
AnswerRe: LPCSTR ??? Pin
Birendar10-Jan-11 1:46
MemberBirendar10-Jan-11 1:46 
QuestionBSTR ? Pin
ajay _tiwari12-Jul-06 20:48
Memberajay _tiwari12-Jul-06 20:48 
AnswerRe: BSTR ? Pin
hhhandy24-Oct-06 8:59
Memberhhhandy24-Oct-06 8:59 
GeneralRe: BSTR ? Pin
Kuryn17-Dec-07 12:46
MemberKuryn17-Dec-07 12:46 
AnswerRe: BSTR ? Pin
Birendar10-Jan-11 1:48
MemberBirendar10-Jan-11 1:48 
GeneralMemory Leaks Pin
mart lewis31-May-05 21:44
Membermart lewis31-May-05 21:44 
Generalstring to BSTR Pin
anindya123459-Dec-04 0:23
Memberanindya123459-Dec-04 0:23 
GeneralRe: string to BSTR Pin
Phaneendra_K6-Mar-06 22:40
MemberPhaneendra_K6-Mar-06 22:40 
GeneralRe: string to BSTR Pin
Phaneendra_K6-Mar-06 23:14
MemberPhaneendra_K6-Mar-06 23:14 
GeneralBSTR to LPTSTR Pin
Ross Oliver15-Sep-04 20:35
sussRoss Oliver15-Sep-04 20:35 
GeneralRe: BSTR to LPTSTR Pin
Habeeballah Hasnoddin10-Aug-09 8:51
MemberHabeeballah Hasnoddin10-Aug-09 8:51 
Generalcant compile Pin
andrewcw22-Apr-04 10:12
Memberandrewcw22-Apr-04 10:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.