Link to home
Start Free TrialLog in
Avatar of jonatec
jonatecFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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...
Avatar of elcapitan
elcapitan

Use the wrappern class _bstr_t defined in <comdef.h>
--EC--
Should be wrapper class and not as I wrote. :-)
--EC--
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...
ASKER CERTIFIED SOLUTION
Avatar of cprot
cprot

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanRollins
I suppose you could try this...

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

--EC--
I don't understand why don't use the class CComBSTR which is native BSTR wrapper in ATL?
Avatar of jonatec

ASKER

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....