Click here to Skip to main content
6,295,667 members and growing! (12,600 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Easy way to manipulate BSTR using CString class

By Rejeesh.T.S

This article is about safe and easy way to use BSTR using the CString class.
VC6Win2K, Visual Studio, MFC, Dev
Posted:2 Aug 2001
Updated:25 Sep 2001
Views:99,427
Bookmarked:23 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
35 votes for this article.
Popularity: 4.83 Rating: 3.13 out of 5
2 votes, 25.0%
1

2

3
1 vote, 12.5%
4
5 votes, 62.5%
5

Introduction

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()
{
...
    if(!CString(bStr).Right(3).Compare("man"))
    {

        //Do something

    }
...
}

License

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

Rejeesh.T.S


Member

Occupation: Web Developer
Location: India India

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralHow to manipulate BSTR in non MFC environment? PinmemberRSAK21:23 7 Dec '08  
GeneralRe: How to manipulate BSTR in non MFC environment? PinmemberRejeesh.T.S20:00 5 Jan '09  
GeneralRe: How to manipulate BSTR in non MFC environment? PinmemberKASR120:06 5 Jan '09  
GeneralThe EASY way to convert BSTR to CString [modified] Pinmemberrjklindsay2:58 6 Jul '06  
GeneralCString conversion PinmemberRsh0:49 16 Sep '02  
GeneralRe: CString conversion Pinmemberjhwurmbach2:24 16 Sep '02  
GeneralHoi PinmemberRosh0:32 1 Oct '01  
Generalalternative PinmemberMario M.9:42 3 Aug '01  
GeneralRe: alternative PinmemberRejeesh.T.S18:38 3 Aug '01  
General_bstr_t PinmemberJohn Smith6:06 3 Aug '01  
GeneralRe: _bstr_t PinmemberAnonymous8:30 4 Aug '01  
GeneralRe: _bstr_t PinmemberJames R. Twine10:47 8 Aug '01  
GeneralRe: _bstr_t PinmemberJeremy W13:51 26 Sep '01  
GeneralRe: _bstr_t PinmemberAhmed Sadek21:55 3 Oct '01  
GeneralRe: _bstr_t PinmemberSameer Khan8:54 9 Oct '01  
GeneralRe: _bstr_t PinmemberCarlos Antollini9:43 9 Oct '01  
GeneralRe: _bstr_t Pinmembersglenister113:44 26 Apr '05  
GeneralRe: _bstr_t PinsussAnonymous16:40 19 Jun '05  
GeneralRe: _bstr_t PinmemberSami.Deen5:13 14 Nov '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Sep 2001
Editor: Nishant Sivakumar
Copyright 2001 by Rejeesh.T.S
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project