Please Enable JavaScript!
Gon[ Enable JavaScript ]

_bstr_t 클래스 사용법

기타 언어/C# & MFC
반응형
_bstr_t Class

Microsoft Specific

A _bstr_t object encapsulates the BSTR data type. The class manages resource allocation and deallocation through function calls to SysAllocString and SysFreeString and other BSTR APIs when appropriate. The _bstr_t class uses reference counting to avoid excessive overhead.

Construction

_bstr_t

Constructs a _bstr_t object.

Operations

Assign

Copies a BSTR into the BSTR wrapped by a _bstr_t.

Attach

Links a _bstr_t wrapper to a BSTR.

copy

Constructs a copy of the encapsulated BSTR.

Detach

Returns the BSTR wrapped by a _bstr_t and detaches the BSTR from the _bstr_t.

GetAddress

Points to the BSTR wrapped by a _bstr_t.

GetBSTR

Points to the beginning of the BSTR wrapped by the _bstr_t.

length

Returns the number of characters in the _bstr_t.

Operators

operator =

Assigns a new value to an existing _bstr_t object.

operator +=

Appends characters to the end of the _bstr_t object.

operator +

Concatenates two strings.

operator !

Checks if the encapsulated BSTR is a NULL string.

operator ==, !=, <, >, <=, >=

Compares two _bstr_t objects.

operator wchar_t* | char*

Extract the pointers to the encapsulated Unicode or multibyte BSTR object.



// _bstr_t_Assign.cpp
#include <comdef.h>
#include <stdio.h>
int main()
{
    // creates a _bstr_t wrapper
    _bstr_t bstrWrapper;
    // creates BSTR and attaches to it
    bstrWrapper = "some text";
    wprintf_s(L"bstrWrapper = %s\n",
              static_cast<wchar_t*>(bstrWrapper));
    // bstrWrapper releases its BSTR
    BSTR bstr = bstrWrapper.Detach();
    wprintf_s(L"bstrWrapper = %s\n",
              static_cast<wchar_t*>(bstrWrapper));
    // "some text"
    wprintf_s(L"bstr = %s\n", bstr);
    bstrWrapper.Attach(SysAllocString(OLESTR("SysAllocedString")));
    wprintf_s(L"bstrWrapper = %s\n",
              static_cast<wchar_t*>(bstrWrapper));
    // assign a BSTR to our _bstr_t
    bstrWrapper.Assign(bstr);
    wprintf_s(L"bstrWrapper = %s\n",
              static_cast<wchar_t*>(bstrWrapper));
    // done with BSTR, do manual cleanup
    SysFreeString(bstr);
    // resuse bstr
    bstr= SysAllocString(OLESTR("Yet another string"));
    // two wrappers, one BSTR
    _bstr_t bstrWrapper2 = bstrWrapper;  
    *bstrWrapper.GetAddress() = bstr;
    // bstrWrapper and bstrWrapper2 do still point to BSTR
    bstr = 0;  
    wprintf_s(L"bstrWrapper = %s\n",
              static_cast<wchar_t*>(bstrWrapper));
    wprintf_s(L"bstrWrapper2 = %s\n",
              static_cast<wchar_t*>(bstrWrapper2));
    // new value into BSTR
    _snwprintf_s(bstrWrapper.GetBSTR(), 100, bstrWrapper.length(),
                 L"changing BSTR");  
    wprintf_s(L"bstrWrapper = %s\n",
              static_cast<wchar_t*>(bstrWrapper));
    wprintf_s(L"bstrWrapper2 = %s\n",
              static_cast<wchar_t*>(bstrWrapper2));
}

결과값
bstrWrapper = some text
bstrWrapper = (null)
bstr = some text
bstrWrapper = SysAllocedString
bstrWrapper = some text
bstrWrapper = Yet another string
bstrWrapper2 = some text
bstrWrapper = changing BSTR
bstrWrapper2 = some text
반응형
Posted by 녹두장군

댓글을 달아 주세요