Click here to Skip to main content
11,922,603 members (31,191 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked

Easy way to manipulate BSTR using CString class

, 25 Sep 2001
Rate this:
Please Sign up or sign in to vote.
This article is about safe and easy way to use BSTR using the CString class.


When we do COM or ActiveX programming, we often have to cope with BSTR strings. Even thought there are APIs for BSTR manipulation, it is a bit difficult to safely manage the BSTR string. This article presents an easy way to manage the BSTR string using the CString class.

Managing BSTR is difficult

We usually use conversion macros like OLE2A, A2BSTR etc. But before using these macros, we should specify USES_CONVERSION macro at the beginning of the function in order to avoid compiler errors. We should also include the atlconv.h file to use these macros. It is also difficult to directly do operations like appending, searching etc. in a BSTR string.

Easy way

There is an easy way to manipulate BSTR string using the MFC CString class. CString constructor accepts the LPCWSTR. LPCWSTR is nothing but the unsigned short*. i.e. the BSTR. Apart from this the operator = is overloaded in CString class to support assignment operation. CString class also contains a member function AllocSysString() which returns the BSTR string corresponding to the CString object. Use the API SysFreeString to free the BSTR string when we no longer need the BSTR string.

Converting a BSTR to CString object sample:

void f(BSTR bStr)
    CString csStr = bStr; //or csStr(bStr);
    //Now we can do whatever we want with this csStr	

Converting a CString object to BSTR:

void SomeFunction()
    CString csStr = "Hello World!";

    BSTR bStr = csStr.AllocSysString();

    f(bStr) //maniplate the BSTR

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

Using temporary objects:

We can also use temporary objects of the CString class as follows. Suppose we want to compare the right 3 characters of a BSTR with another string, we can do as shown in the below function by using a temporary CString object.

void SomeFun()

        //Do something


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

Technical Lead
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow to manipulate BSTR in non MFC environment? Pin
RSAK7-Dec-08 21:23
memberRSAK7-Dec-08 21:23 
AnswerRe: How to manipulate BSTR in non MFC environment? Pin
Rejeesh.T.S5-Jan-09 20:00
memberRejeesh.T.S5-Jan-09 20:00 
GeneralRe: How to manipulate BSTR in non MFC environment? Pin
KASR15-Jan-09 20:06
memberKASR15-Jan-09 20:06 
GeneralThe EASY way to convert BSTR to CString [modified] Pin
rjklindsay6-Jul-06 2:58
memberrjklindsay6-Jul-06 2:58 
GeneralCString conversion Pin
Rsh16-Sep-02 0:49
memberRsh16-Sep-02 0:49 
GeneralRe: CString conversion Pin
jhwurmbach16-Sep-02 2:24
memberjhwurmbach16-Sep-02 2:24 
GeneralHoi Pin
Rosh1-Oct-01 0:32
memberRosh1-Oct-01 0:32 
Generalalternative Pin
Mario M.3-Aug-01 9:42
memberMario M.3-Aug-01 9:42 
GeneralRe: alternative Pin
Rejeesh.T.S3-Aug-01 18:38
memberRejeesh.T.S3-Aug-01 18:38 
General_bstr_t Pin
John Smith3-Aug-01 6:06
memberJohn Smith3-Aug-01 6:06 
GeneralRe: _bstr_t Pin
Anonymous4-Aug-01 8:30
memberAnonymous4-Aug-01 8:30 
GeneralRe: _bstr_t Pin
James R. Twine8-Aug-01 10:47
memberJames R. Twine8-Aug-01 10:47 
GeneralRe: _bstr_t Pin
Jeremy W26-Sep-01 13:51
memberJeremy W26-Sep-01 13:51 
GeneralRe: _bstr_t Pin
Ahmed Sadek3-Oct-01 21:55
memberAhmed Sadek3-Oct-01 21:55 
GeneralRe: _bstr_t Pin
Sameer Khan9-Oct-01 8:54
memberSameer Khan9-Oct-01 8:54 
GeneralRe: _bstr_t Pin
Carlos Antollini9-Oct-01 9:43
memberCarlos Antollini9-Oct-01 9:43 
GeneralRe: _bstr_t Pin
sglenister126-Apr-05 13:44
membersglenister126-Apr-05 13:44 
GeneralRe: _bstr_t Pin
Anonymous19-Jun-05 16:40
sussAnonymous19-Jun-05 16:40 
GeneralRe: _bstr_t Pin
Sami.Deen14-Nov-07 5:13
memberSami.Deen14-Nov-07 5:13 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.1 | Last Updated 26 Sep 2001
Article Copyright 2001 by Rejeesh.T.S
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid