I have BSTR string which is passed from COM server. When I put this string into _bstr_t (or CComBSTR) constructor then access violation is occurred. I checked this exception and found that this exception raised only when BSTR is empty (or null).
I was trying to put nulled BSTR explicitly to _bstr_t constructor and this works fine:
BSTR bstr = NULL;
_bstr_t t(bstr, false);
cout << t.length() << endl;
But with BSTR which is passed from COM server this don't work - access violation exception is ocurred when string is empty or null (or maybe corrupted?)
I found that this workaround works fine:
if (SysStringLen(bstrFromCOMserver) > 0) {
_bstr_t t(bstrFromCOMserver, false);
cout << t.length() << endl;
}
But I want to know why this don't work directly with _bstr_t or CComBSTR wrappers:
_bstr_t t(bstrFromCOMserver, false);
if (t.length() > 0) {...}
Update:
How COM server pass BSTR string:
void CALLBACK CProxy_ISTIQuoteEvents::OnSTIQuoteSnap(const structSTIQuoteSnap& structQuoteSnap) const {
if (SysStringLen(structQuoteSnap.bstrUpdateTime) > 0) {
_bstr_t updateTime(structQuoteSnap.bstrUpdateTime, false);
}
}
}