Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3029
  • Last Modified:

How to concatenate BSTR strings in ATL

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
jonatec
Asked:
jonatec
1 Solution
 
elcapitanCommented:
Use the wrappern class _bstr_t defined in <comdef.h>
--EC--
0
 
elcapitanCommented:
Should be wrapper class and not as I wrote. :-)
--EC--
0
 
vadikCommented:
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
Independent Software Vendors: 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!

 
cprotCommented:
//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
 
DanRollinsCommented:
I suppose you could try this...

BSTR* GetFullName( BSTR* sFirst, BSTR* sLast)
{
    _bstr_t s= sFirst;    
    s += " ";    
    s += sLast;
    return( s );
}
0
 
elcapitanCommented:
Dan
I already suggested to use _bstr_t

--EC--
0
 
vadikCommented:
I don't understand why don't use the class CComBSTR which is native BSTR wrapper in ATL?
0
 
jonatecAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now