if I create a BSTR in C++, do I need to release it ?

L

Lynn McGuire

If I create a BSTR in my C++ code calling Excel, do I need to explicitly
release the VARIANT holding the BSTR after I call Excel ?

VARIANT range;
VariantInit ( & range);
range.vt = VT_BSTR;
_bstr_t address = _bstr_t (cellAddress.c_str ());
range.bstrVal = address;
OLEMethod (DISPATCH_PROPERTYGET, & result1, pExcelSheet, L"Range", 1, range);

Thanks,
Lynn
 
A

Akihito Yamashiro

Hi.

Yes, you have to.
For example , Microsoft's sample code is doing a same thing.
In http://support.microsoft.com/kb/238393/en-us
-----------------------
// Call Documents.Open() to open C:\Doc1.doc
IDispatch *pDoc;
{
VARIANT result;
VariantInit(&result);
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = ::SysAllocString(L"C:\\Doc1.doc");

AutoWrap(DISPATCH_METHOD, &result, pDocs, L"Open", 1, x);
pDoc = result.pdispVal;
SysFreeString(x.bstrVal);
}
 
A

Akihito Yamashiro

Sorry , I have not noticed that you are using _bstr_t until now.
What you have said is right.
The _bstr_t class automatically doesSysFreeString for you in its
destructer.
You don't have to do SysFreeString in this case.
 
L

Lynn McGuire

Sorry , I have not noticed that you are using _bstr_t until now.
What you have said is right.
The _bstr_t class automatically doesSysFreeString for you in its
destructer.
You don't have to do SysFreeString in this case.

Thanks for confirming that !

Lynn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top