Solved

Need to add mulltiple BSTR strings using C++

Posted on 2008-06-16
3
1,449 Views
Last Modified: 2013-12-14
Hi,
I need to add multiple bstr strings using C++, I am aware to add bstr we can:

int lenBuffer;
BSTR bstr1,bstr2, bstr3,;
//assign some values to bstr's
lenBuffer = SysStringLen(bstr1) + SysStringLen(bstr2) + SysStringLen(bstr3);
BSTR bstrQuery;
bstrQuery = SysAllocStringLen(L"",lenBuffer+1);
wcscat_s(bstrQuery,lenBuffer+1,bstr1);
wcscat_s(bstrQuery,lenBuffer+1,bstr2);
wcscat_s(bstrQuery,lenBuffer+1,bstr3);

is there any better way of doing it, I have around 6 bstr strings(or more) to concatenate, whats the best possible way of doing it?

TIA

0
Comment
Question by:LearningCpp
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 21795707
The '_bstr_t' helper class (http://msdn.microsoft.com/en-us/library/zthfhkd6(VS.80).aspx) has an 'operator+()' which comes in handy here:

 

#include <comdef.h>
 

BSTR bstr1,bstr2, bstr3,;
 

_bstr_t _bstr1(bstr1);

_bstr_t _bstr2(bstr2);

_bstr_t _bstr3(bstr3);
 

_bstr_t res = _bstr1 + _bstr2 + _bstr3;
 

BSTR bstrQuery = res.copy();

Open in new window

0
 

Author Closing Comment

by:LearningCpp
ID: 31467694
What is the significance of adding:
#include
Will this work in pure win32 environment, currently I am developing in VS IDE 2005. but will have to port only to pure win32 api environment
0
 
LVL 86

Expert Comment

by:jkr
ID: 21796019
Yes, it will.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

759 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

20 Experts available now in Live!

Get 1:1 Help Now