问题
I have a class function which is receving a BSTR. In my class I have a member variable which is LPCSTR. Now I need to append BSTR ins LPCSTR. How I can do that. Here is my function.
void MyClass::MyFunction(BSTR text)
{
LPCSTR name = "Name: ";
m_classMember = name + text; // m_classMember is LPCSTR.
}
in my m_classMember I want that after this function value should be "Name: text_received_in_function". How i can do that.
回答1:
Use the Microsoft specific _bstr_t class, which handles the ANSI/Unicode natively. Something like
#include <comutils.h>
// ...
void MyClass::MyFunction(BSTR text)
{
_bstr_t name = "Name: " + _bstr_t(text, true);
m_classMember = (LPCSTR)name;
}
is what you almost want. However, as pointed out by the remarks, you have to manage the lifetime of m_classMember
and the concatened string. In the example above, the code is likely to crash.
If you own the MyClass
object, you could simply add another member variable:
class MyClass {
private:
_bstr_t m_concatened;
//...
};
and then use m_classMember
as a pointer to the string content of m_concatened
.
void MyClass::MyFunction(BSTR text)
{
m_concatened = "Name: " + _bstr_t(text, true);
m_classMember = (LPCSTR)m_concatened;
}
Otherwise, prior to the assignment of m_classMember
, you should free it in the same way you allocated it (free
, delete []
, etc), and create a new char*
array in which you copy the content of the concatened string. Something like
void MyClass::MyFunction(BSTR text)
{
_bstr_t name = "Name: " + _bstr_t(text, true);
// in case it was previously allocated with 'new'
// should be initialized to 0 in the constructor
delete [] m_classMember;
m_classMember = new char[name.length() + 1];
strcpy_s(m_classMember, name.length(), (LPCSTR)name);
m_classMember[name.length()] = 0;
}
should do the work.
回答2:
First, I suggest you to not use raw char/wchar_t*
pointers as data members for strings; in general, it's better (easier, more maintainable, exception-safe, etc.) to use a robust C++ string class.
Since you are writing Windows code, you may want to use ATL::CString
, which is well integrated in the context of Win32 programming (e.g.: it offers several conveniences, like loading strings from resources, it works out-of-the-box with the TCHAR
model, etc.).
If you want to work with the TCHAR
model (and make your code compilable in both ANSI/MBCS and Unicode builds), you may want to use the ATL string conversion helper class CW2T
to convert from BSTR
(which is Unicode wchar_t*
) to char*
in ANSI/MBCS build, and leave it as wchar_t*
in Unicode builds.
#include <atlstr.h> // for CString
#include <atlconv.h> // for CW2T
void MyClass::MyFunction(BSTR text)
{
// Assume:
// CString m_classMember;
m_classMember = _T("Name: ");
// Concatenate the content of the BSTR.
// CW2T keeps the BSTR as Unicode in Unicode builds,
// and converts to char* in ANSI/MBCS builds.
m_classMember += CW2T(text);
}
Instead, if you just want to compile your code in Unicode (which makes sense in today's world), you can get rid of _T("...")
decoration and CW2T
, and just use:
void MyClass::MyFunction(BSTR text)
{
// Assume:
// CString m_classMember;
m_classMember = L"Name: ";
// Concatenate the content of the BSTR.
m_classMember += text;
}
(Or use STL's std::wstring
as others suggested.)
来源:https://stackoverflow.com/questions/12948010/appending-bstr-in-a-lpcstr