블로그 이미지
fiadot_old

칼퇴근을 위한 게임 서버 개발 방법론에 대한 심도있는 고찰 및 성찰을 위한 블로그!

Rss feed Tistory
Technical Article/펌 2005. 7. 29. 14:51

BSTR 관련 타입 변환 팁입니다.

:: BSTR 관련 타입 변환 팁입니다.



박성규 (2004-11-29 12:28:18, Hit: 492, Vote: 40)



출처 : http://www.codeguru.com/Cpp/Cpp/string/conversions/article.php/c5639/
-Robert Pittenger-


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);
}




1. BSTR 형을 _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);

2. _bstr_t 형을 BSTR 형으로 바꾸기
// _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);

3. CComBSTR 형을 BSTR 형으로 바꾸기
// 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);

4. _bstr_t 형을 CComBSTR 형으로 바꾸기
// _bstr_t to CComBSTR
_bstr_t bstrStart(_T("This is the test string."));
CComBSTR bstrFinish;
bstrFinish.AppendBSTR(bstrStart);
ShowBSTR(bstrFinish);

5. BSTR 형을 CString 형으로 바꾸기
// 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);

6. _bstr_t 형을 CString 형으로 바꾸기
_bstr_t bstrStart(_T("This is the test string."));
TCHAR szFinal[255];
_stprintf(szFinal, _T("%s"), (LPCTSTR)bstrStart);
AfxMessageBox(szFinal);

7. CComBSTR 형을 LPCTSTR 형으로 바꾸기
// 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);

8. LPCTSTR 형을 _bstr_t 형으로 바꾸기
// 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);

9. LPCTSTR 형을 CComBSTR 형으로 바꾸기
// 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);



출처 : http://www.codeguru.com/Cpp/Cpp/string/conversions/article.php/c5639/
-Robert Pittenger-






null
(2005.05.24 14:37) 좋은 팁 감사~


현군
(2005.07.29 09:35) OLE2T 등의 매크로도 있습니다. (아시겠지만 -_-a)
BSTR -> TCHAR 등등 그런대로 편해요..
,
TOTAL TODAY