Solved

Need to add mulltiple BSTR strings using C++

Posted on 2008-06-16
3
1,483 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copying WordPress Pages 5 87
Using Diagnostic tools in VS2015: Unresoved allocations 19 104
Eclipse Neon start with Admin account only 6 143
Angular JS Route 3 83
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

808 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