?
Solved

How to concatenate BSTR strings in ATL

Posted on 2003-03-16
8
Medium Priority
?
2,969 Views
Last Modified: 2013-11-25
I have written a VC++ DLL using ATL and I want to use a Visual Basic client to pass 2 properties, FirstName and Surname. Then invoke a method in my VC++ COM class which will just concatenate the 2 strings together and return in a 3rd property called FullName, with a space in the middle, ie.

  FirstName = "Fred"
  Surname = "Smith"
  FullName = "Fred Smith"

I have created 2 properties, FirstName and Surname in my class, with types BSTR. But even after converting the unicode to an ansi using WideCharToMultiByte, I still haven't figured out how to add the 2 together and return FullName.

Any clues please...
0
Comment
Question by:jonatec
[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
8 Comments
 
LVL 3

Expert Comment

by:elcapitan
ID: 8146673
Use the wrappern class _bstr_t defined in <comdef.h>
--EC--
0
 
LVL 3

Expert Comment

by:elcapitan
ID: 8146678
Should be wrapper class and not as I wrote. :-)
--EC--
0
 
LVL 1

Expert Comment

by:vadik
ID: 8146916
I think you need something like that:

     USES_CONVERSION;
     BSTR bstrFirst = SysAllocString(A2COLE("FirstName"));
     BSTR bstrSecond = SysAllocString(A2COLE("SecondName"));
     BSTR bstrFullName = NULL;

     CComBSTR bstrRes;
     
     bstrRes = bstrFirst;
     bstrRes += bstrSecond;

     bstrFullName = bstrRes.Detach();

Actually is dirty code (with memory leaks) because I don't understand whay are you need exactly...  Sorry for my English...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
cprot earned 500 total points
ID: 8148622
//in the .h file, add:
char m_sFirstName[30];
char m_sSurName[30];
char m_sFullName[60];

//Use the following implementation code:
get_FirstName(BSTR *pVal)
{
  CComBSTR cbsBuf(m_sFirstName);
  *pVal = cbsBuf.Copy();
}
put_FirstName( newVal)
{
  wcstombs(m_sFirstName, newVal, 30);
}

get_LastName(BSTR *pVal)
{
  CComBSTR cbsBuf(m_sLastName);
  *pVal = cbsBuf.Copy();
}
put_LastName( newVal)
{
  wcstombs(m_sLastName, newVal, 30);
}

get_FullName(BSTR *pVal)
{
  CComBSTR cbsBuf(m_sFullName);
  *pVal = cbsBuf.Copy();
}
put_FullName( newVal)
{
  wcstombs(m_sFullName, newVal, 60);
}

//the method of concentration:
mergeName()
{
  sprintf(m_sFullName, "%s %s", m_sLastName, m_sFirstName);
}
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8151064
I suppose you could try this...

BSTR* GetFullName( BSTR* sFirst, BSTR* sLast)
{
    _bstr_t s= sFirst;    
    s += " ";    
    s += sLast;
    return( s );
}
0
 
LVL 3

Expert Comment

by:elcapitan
ID: 8151101
Dan
I already suggested to use _bstr_t

--EC--
0
 
LVL 1

Expert Comment

by:vadik
ID: 8151135
I don't understand why don't use the class CComBSTR which is native BSTR wrapper in ATL?
0
 

Author Comment

by:jonatec
ID: 8154248
Yep, brilliant, works great thanks. Don't quite know what sprintf does? Is this the only way to concatenate BSTR strings together? And where in the code do you put in the single space character between FirstName and LastName?

Many thanks....
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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 …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

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