출처:http://www.codeproject.com/string/bstrsproject1.asp
String Types
BSTR/C String conversions are required if:
You are doing COM programming in C/C++
You are writing multiple language applications, such as C++ DLL's accessed by Visual Basic applications.
C Language String Types and Classes
This article deals with the following C/MFC/ATL string types:
char/wchar/TCHAR -- The C strings for ANSI and Unicode
CString -- The C++/MFC class wrapper for C strings
BSTR -- The Visual Basic string type
_bstr_t -- A C++ class wrapper for the Visual Basic string type
CComBSTR -- Yet another C++ class wrapper for the Visual Basic string type used predominately in ATL code
Demo Project
The demo project is just an MFC dialog based application with buttons for each type of conversion. It is built using VC++ 6.0. It uses a couple of support functions you may find helpful:
BSTR GetBSTR()
{
_bstr_t bstr1(_T("This is the test string."));
BSTR bstr;
bstr = bstr1.copy();
return bstr;
}
CComBSTR GetComBSTR()
{
CComBSTR bstr("This is the test string.");
return bstr;
}
void CVbsDlg::ShowBSTR(BSTR bstr)
{
_bstr_t bstrStart(bstr);
CString s;
s.Format(_T("%s"), (LPCTSTR)bstrStart);
AfxMessageBox(s);
}
Conversions
So let's get to it. Here are the conversion techniques:
Converting BSTR to _bstr_t
// BSTR to _bst_t
BSTR bstrStart = GetBSTR();
// use the constructor
_bstr_t bstrFinal(bstrStart);
ShowBSTR(bstrFinal);
// Use the = operator
bstrFinal = bstrStart;
ShowBSTR(bstrFinal);
Converting a _bstr_t to BSTR
You may want to get a BSTR from a _bstr_t class.
// _bstr_t to BSTR
_bstr_t bstrStart(_T("This is the test string."));
BSTR bstrFinish;
// use _bstr_t::copy member function
bstrFinish = bstrStart.copy();
ShowBSTR(bstrFinish);
// use = operator
bstrFinish = bstrStart;
ShowBSTR(bstrFinish);
Converting a CComBSTR to BSTR
You may want to get a BSTR from a CComBSTR class.
// CComBSTR to BSTR
CComBSTR bstrStart(_T("This is the test string."));
BSTR bstrFinish;
// use the = operator
bstrFinish = bstrStart;
ShowBSTR(bstrFinish);
// use the Copy member function
bstrFinish = bstrStart.Copy();
ShowBSTR(bstrFinish);
Converting _bstr_t to CComBSTR
// _bstr_t to CComBSTR
_bstr_t bstrStart(_T("This is the test string."));
CComBSTR bstrFinish;
bstrFinish.AppendBSTR(bstrStart);
ShowBSTR(bstrFinish);
Converting BSTR to C String
(Note :- conversion that only works in Unicode)
// BSTR to C String
BSTR bstrStart;
bstrStart = GetBSTR();
TCHAR szFinal[255];
// direct conversion from BSTR to LPCTSTR only works in Unicode
_stprintf(szFinal, _T("%s"), (LPCTSTR)bstrStart);
AfxMessageBox(szFinal);
_bstr_t bstrIntermediate(bstrStart); // convert to _bstr_t
CString strFinal;
// you have to go through _bstr_t to have it work in ANSI and Unicode
_stprintf(szFinal, _T("%s"), (LPCTSTR)bstrIntermediate);
// Or using MFC
strFinal.Format(_T("%s"), (LPCTSTR)bstrIntermediate);
AfxMessageBox(strFinal);
Converting _bstr_t to C String
(this works in both ANSI and Unicode)
_bstr_t bstrStart(_T("This is the test string."));
TCHAR szFinal[255];
_stprintf(szFinal, _T("%s"), (LPCTSTR)bstrStart);
AfxMessageBox(szFinal);
Converting CComBSTR to LPCTSTR
(not possible, must go through _bstr_t )
// CComBSTR to C String
CComBSTR bstrStart("This is the test string.");
_bstr_t bstrIntermediate(bstrStart);
TCHAR szFinal[255];
_stprintf(szFinal, _T("%s"), (LPCTSTR)bstrIntermediate);
AfxMessageBox(szFinal);
Converting LPCTSTR to _bstr_t
(Use a constructor or = operator)
// LPCTSTR to _bstr_t
LPCTSTR szStart = _T("This is the text string");
// Use the constructor
_bstr_t bstrFinal(szStart);
ShowBSTR(bstrFinal);
// or use = operator
bstrFinal = szStart;
ShowBSTR(bstrFinal);
Converting LPCTSTR to CComBSTR
Use a constructor or CComBSTR::Append function
// LPCTSTR to CComBSTR
// Use a constructor
LPCTSTR szStart = _T("This is the text string");
// Use the constructor
CComBSTR bstrFinal(szStart);
ShowBSTR(bstrFinal);
// Or use the Append function
bstrFinal.Empty();
bstrFinal.Append(szStart);
ShowBSTR(bstrFinal);
stl string과 BSTR 사이의 변환
BSTR input, BSTR* output;
USES_CONVERSION;
string rh = W2A(input);
string str2 = "dsdj jjsad";
BSTR*output = ::SysAllocString(A2W(str2.c_str()));
char/wchar/TCHAR-- The C strings for ANSI and Unicode (ansi, unicode기반의 C문자열)
CString-- The C++/MFC class wrapper for C strings (mfc에서 제공하는 문자열 클래스)
BSTR-- The Visual Basic string type (visual basic의 문자열)
_bstr_t-- A C++ class wrapper for the Visual Basic string type (visual basic의 문자열에 대한 c++ 래퍼클래스)
CComBSTR-- Yet another C++ class wrapper for the Visual Basic string type used predominately in ATL code (atl에서 사용하기 위한, visual basic용 c++ 래퍼 클래스)
주의:
I don't know if this is intended but in the example 'Converting BSTR to _bstr_t' people should be aware that there will be a memory leak if the code is used as is.
This is because theBSTR bstrStartis never released. The constructor:
_bstr_t bstrFinal(bstrStart)
does not take ownership of the BSTR but just takes a copy. Therefore the BSTR is still hanging around.
BSTR에서 위와 같이 생성자를 통해 _bstr_t로 변환시는 내용은 복사가 되지만, 소유권은 취하지 않게 됩니다.
따라서, BSTR에 사용된 메모리를 끝까지 해제 되지 않은 상태로 머물게 됩니다.
(
If this constructor was used:
_bstr_t bstrFinal(bstrStart, false)
Then everything is okay asbstrFinaltakes ownership ofbstrStartand the memory that has been created for it will be tidied up whenbstrFinalis destroyed.
false이면, _bstr_t 객체를 생성시, 인자로 주어진 BSTR(bstrStart)를 가지는 객체를 생성하게 됩니다. 이때, SysAllocString 함수를 호출해서, 인자로 주어진 BSTR를 생성치 않으므로, 인자로 주어진 bstrStart은 _bstr_t객체가 소멸시 안전하게 해제 됩니다.
Likewise for the example 'Converting a _bstr_t to a BSTR',bstrFinishdoes not get released.
There should be a::SysFreeString(bstrFinish)at the end somewhere to release thebstrFinish.
Elsewhere in the examples, this should also be called to free the BSTRs that are created.
'Programming > MFC' 카테고리의 다른 글
솔루션에 16384 파일이 (0) | 2008.03.24 |
---|---|
Visual Studio .Net 단축키 모음 (0) | 2008.02.26 |
boost::shared_ptr 살펴보기 (0) | 2008.01.11 |
trl::shared_ptr 사용방법 (0) | 2008.01.10 |
MessgeBox 졸료하기 (0) | 2008.01.08 |