Solved

Need to add mulltiple BSTR strings using C++

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to programmatically differentiate between C and Java 10 200
Eclipse. How to move up/down source folders 4 88
Dynamically allocate memory 9 60
Least Squares Curve Fitting 4 81
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

831 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