jonatec
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...
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...
Should be wrapper class and not as I wrote. :-)
--EC--
--EC--
I think you need something like that:
USES_CONVERSION;
BSTR bstrFirst = SysAllocString(A2COLE("Fir stName"));
BSTR bstrSecond = SysAllocString(A2COLE("Sec ondName")) ;
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...
USES_CONVERSION;
BSTR bstrFirst = SysAllocString(A2COLE("Fir
BSTR bstrSecond = SysAllocString(A2COLE("Sec
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I suppose you could try this...
BSTR* GetFullName( BSTR* sFirst, BSTR* sLast)
{
_bstr_t s= sFirst;
s += " ";
s += sLast;
return( s );
}
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 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?
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....
Many thanks....
--EC--