Search code examples
c++htmlvisual-c++mshtml

how to handle bad pointers


all, as the title asked how to handle bad pointers. I am using MSHTML::IHTMLStyle to handle style features that when I read in a snippet of html code, I collect some specific style features among all the html elements within the code.

hash_map<wstring, wstring> CMyAppDlg::GetNodeStyles(VARIANT varSrc)
{
    long lLength = 0;
    MSHTML::IHTMLDocument2Ptr htmDoc = NULL;
    MSHTML::IHTMLElementCollectionPtr pElemColl = NULL;
    MSHTML::IHTMLElementPtr pChElem = NULL;
    MSHTML::IHTMLStylePtr pStyle = NULL;
    _bstr_t bstrtTagName;
    hash_map<wstring, wstring> hmStyles;
    SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
    CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (void**) &htmDoc);

    VARIANT *param;
    HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
    param->vt = VT_BSTR;
    param->bstrVal = varSrc.bstrVal;

    hr = SafeArrayUnaccessData(psaStrings);
  hr = htmDoc->write(psaStrings);

    pElemColl = htmDoc->all;
    lLength = pElemColl->length;
    for(long i = 0; i < lLength; i++)
    {
        pChElem = pElemColl->item(_variant_t(i), _variant_t());

        MessageBox(pChElem->tagName, _T("The tag name of this html element is"), MB_OK);
        pStyle = pChElem->style;

        pStyle->fontStyle;

        hmStyles[wstring(pStyle->fontStyle)] = L"FontStyle";

        hmStyles[wstring(pStyle->fontFamily)] = L"FontFamily";

        hmStyles[wstring(pStyle->textDecoration)] = L"TextDecoration";
    }

    return hmStyles;
}

the problem is when read in an arbitrary html code like:

<A href='/servlet/BookDetailsPL?bi=1257056972&amp;tab=1&amp;searchurl=bt.x%3D44%26bt.y%3D10%26sts%3Dt%26tn%3Dharry%2Bpotter' cmImpressionSent='1'>The Orchard Bookshop.</A> <SPAN class=scndInfo>(Hayes., UK, United Kingdom)</SPAN>

IHTMLDocument write() will automatically adds plain "HTML", "TITLE", "HEAD", "BODY", etc, to the code, that they dont contain any styles; so

pStyle->fontStyle; pStyle->fontFamily and pStyle->textDecoration will return bad pointers of _bstr_t type, that these bad pointers lead my program crashed. My question is how to avoid these bad pointers, like setting an condition to by-pass them.

MSHTML::IHTMLStyle::GetfontStyle returned   {<Bad Ptr> (1)} _bstr_t
MSHTML::IHTMLStyle::GetfontFamily returned  {<Bad Ptr> (1)} _bstr_t
MSHTML::IHTMLStyle::GettextDecoration returned  {<Bad Ptr> (1)} _bstr_t

here is the doc on IHTMLElement style property:

HRESULT IHTMLElement::get_style(IHTMLStyle **p);

p is the address of a pointer to the IHTMLStyle interface for the style sheet.


Solution

  • the problem is caused by using smart pointers on IHTMLDocument2, IHTMLElement and IHTMLElementCollection, and IHTMLStyle. I solved the bad pointers problem by declaring normal pointers on them, and dont know why smart pointers failed me this time, and seems to be failed in some other places too.