_bstr_t::Assign
Microsoft Specific
Copies a BSTR into the BSTR wrapped by a _bstr_t.
void Assign(
BSTR s
);
Parameters
- s
A BSTR to be copied into the BSTR wrapped by a _bstr_t.
Remarks
Assign does a binary copy, which means the entire length of the BSTR is copied, regardless of content.
Example
// _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));
}
Output
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