CComBSTRr  find replace

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

I've a CComBSTR variable as below:
CComBSTR bstrUrl= L"https://www.experts-exchange.com/";

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
  • 4
  • 3
LVL 19

Expert Comment

ID: 23610090
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"https://www.experts-exchange.com/";
	ReplaceInCComBSTR ( bstrUrl, L"https:", L"http:" );

Open in new window

LVL 19

Expert Comment

ID: 23610099
>> 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

ID: 23614713
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?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

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

Accepted Solution

mrwad99 earned 500 total points
ID: 23620402
OK, you need to #include <string>

Author Closing Comment

ID: 31545385
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

ID: 23631001
No problem, you will find it works :)

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 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