CComBSTRr  find replace

Posted on 2009-02-10
Last Modified: 2013-11-25

I've a CComBSTR variable as below:
CComBSTR bstrUrl= L"";

Here, how to change the  "https" into "http"  in the above string. No MFC involved so CString cannot be used.

Thanks in advance

Question by:mnopix
    LVL 19

    Expert Comment

    The underlying storage for a CComBSTR is the public member CComBSTR::m_str, which is a BSTR.  Now, a BSTR is typdef'ed as OLECHAR *BSTR, and an OLECHAR* is typdef'ed as a WCHAR.  A WCHAR is a wchar_t, so it follows then that all a CComBSTR is is a wrapper around a wchar_t*.

    Internally, the constructor has the code

    m_str = ::SysAllocString(pSrc);

    where pSrc is the string you passed in.  The destructor, I would assume, calls SysFreeString, passing m_str.  (I cannot prove this since I cannot step into the destructor for some reason).  

    Anyway, that being said, you can see that you can use standard wide string handling API functions to manipulate the member m_str, or, as I did, std::wstring, which is a lot easier.  The only thing you need to watch out for is that you take care of the memory occupied by m_str, if, for example, you increase the length of it.  You could write a wrapper function to do a replace operation, something like the code below.

    void ReplaceInCComBSTR ( CComBSTR& strInput, const std::wstring& strOld, const std::wstring& strNew )
         std::wstring strOutput (  strInput );
         int pos = 0;
         int lpos = 0;
         while ( ( pos = strOutput.find ( strOld, lpos ) ) != string::npos )
               strOutput.replace ( pos, strOld.length(), strNew );
               lpos = pos+1;
    	 // Find and replace is complete; now update the CComBSTR!
    	 ::SysFreeString ( strInput.m_str );
    	 strInput.m_str = ::SysAllocString ( strOutput.c_str() );
    // ...
    	CComBSTR bstrUrl=  L"";
    	ReplaceInCComBSTR ( bstrUrl, L"https:", L"http:" );

    Open in new window

    LVL 19

    Expert Comment

    >> and an OLECHAR* is typdef'ed as a WCHAR

    should be

    and an OLECHAR is typdef'ed as a WCHAR (no pointer type)

    Author Comment

    I'm getting following compile errors:
    abc.cpp(1716) : error C2653: 'string' : is not a class or namespace name
    abc.cpp(1716) : errorC2065: 'npos' : undeclared identifier

    Am I missing some include files?

    Author Comment

    About the Build Environment:
    Windows Server 2003
    Platform Builder for MS Windows CE 5.0
    LVL 19

    Accepted Solution

    OK, you need to #include <string>

    Author Closing Comment

    Thank you very much for your effort. As our project approch was changed we decided keep the 'https' as its.  So I've not verified the last comment.
    LVL 19

    Expert Comment

    No problem, you will find it works :)

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    This article will show, step by step, how to integrate R code into a R Sweave document
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    This video teaches viewers about errors in exception handling.
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now