samedi 23 juillet 2016

Safely using ConvertBSTRToString and VARIANT

The following code seems to occasionally throw NullReferenceException on one particular system only:

VARIANT vtProp;

hres = pclsObj->Get(L"VolumeSerialNumber", 0, &vtProp, 0, 0);
if (!FAILED(hres))
{
    string result = _com_util::ConvertBSTRToString(vtProp.bstrVal); //fails here
    //and then do stuff
}
VariantClear(&vtProp);

So far as I can tell the failure is in ConvertBSTRToString. (It's not my system nor am I onsite so I can't properly debug). Should I first be testing that the variant actually is non-empty and a string, and if so, how? Is there any other reason why erratic failure might be happening?

Aucun commentaire:

Enregistrer un commentaire