메뉴 건너뛰기


Developer > Application

C,C++ Guide to BSTR and C String Conversions

2013.11.27 02:08

푸우 조회 수:18346

원문http://www.codeguru.com/Cpp/Cpp/string/conversions/article.php/c5639/



Guide to BSTR and C String Conversions

번역

Xogus Kim (http://www.xogus.pe.kr),  2004-06-16

원문

Robert Pittenger (view profile),  August 19, 2003

Introduction

Windows programming 혼동되는 측면  하나는 Visual Basic style string C 언어 style string간의 변환을 관리하는것이다.  이것은 매우 힘들지는 않지만 단지 세부적인 것을 기억하는 것이 어려울 뿐이다.  이것은 보통 자주 일어나는 일은 아니며, MSDN docummentation 너무 방대하여 쉽게 문제의 답을 찾기 힘들다.  하지만최악의 부분은 당신이 수행한 몇몇 typecast 오류없이 컴파일되지만 기대한대로 수행되지 않는다는 것이다.  제대로 동작하지 않는 코드와 버그들의 결과는 찾아내기 어렵다.  약간의 경험후에 당신은 당신이 기대하는 형태로의 확실한 string conversion 배우게 된다.

C string NULL character 의해 종결되는 character들의 array이다.  Visual Basic string string 안에 string 길이가 선행한다는 점에서 다르다.  따라서, VB string 자신의 길이를 알고있다.  또한모든 VB string Unicode(16 bit/character)이다.

String Types

BSTR/C String conversions 다음과 같은 경우에 필요하다.

  • C/C++ COM programming 하는 경우
  • Visual Basic application 관련된 C++ DLL 같은 다중 언어 application 작성하는 경우.

C Language String Types and Classes

 기사는 다음의 C/MFC/ATL string type들을 다룬다:

String Type

Description

char/wchar/TCHAR  

ANSI Unicode 대한 C string

CString

C string 대한 C++/MFC wrapper class

BSTR

Visual Basic string

_bstr_t

Visual Basic string type 대한 C++ wrapper class

CComBSTR

ATL code에서 탁월하게 사용되는 또하나의 Visual Basic string wrapper class

 

Demo Project

demo project 단지  type conversion 대한 button 가지는 MFC dialog-based application이다.  이것은 VC++ 6.0사용하여 만들어졌으며도움이 될만한 유용한 function들을 지원한다.

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

이제 시작한다.  여기에 conversion technique들이 있다:

1.       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);

2.       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);

3.       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);

4.       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);

 

 

 

 

5.      Converting BSTR to C String

(Note: Unicode에서만 동작한다)

  // BSTR to C String

  BSTR bstrStart;

  bstrStart = GetBSTR();

 

  TCHAR szFinal[255];

 

  // BSTR로부터LPCTSTR로의 직접 변환은 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.       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);

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

 

 

8.       Converting LPCTSTR to _bstr_t

constructor 또는 = 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);

9.       Converting LPCTSTR to CComBSTR

constructor 또는 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);

 

 


Summery (역자)

TO

 

From

LPCTSTR

LPCOLECHAR

BSTR

_bstr_t

CComBSTR

LPCTSTR,

LPCOLECHAR

 

::SysAllocString()

b = SysAllocString (OLECHAR * sz)

Constructor

_bstr_t::_bstr_t (LPCTSTR)

Constructor 또는

CComBSTR::Append

CComBSTR::CComBSTR(LPCTSTR)

BSTR

Type Casting

 Unicode에서만

(LPCTSTR)bstr

 

Constructor

_bstr_t::_bstr_t(BSTR)

 

_bstr_t

Type Casting

(LPCTSTR)_bstr

_bstr_t::copy()

b = _b.copy();

 

CComBSTR::AppendBSTR(BSTR b)

combstr.AppendBSTR(b)

CComBSTR

Type Casting

(LPCTSTR)combstr

CComBSTR::Copy()

b = combstr.Copy();