?
Solved

Need to add mulltiple BSTR strings using C++

Posted on 2008-06-16
3
Medium Priority
?
1,525 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 375 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

777 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