开发者

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&g开发者_JS百科t; (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.


I have no idea what this MSHTML stuff is but I'm guessing pChElem->style is not a pointer.

In which case you are assigning a style value to a style pointer which would certainly be a bad pointer. I'd recommend dereferencing first except that you aren't allocating any of these pointers so if you don't want to just use the element's style perhaps something like this is what you're looking for:

pStyle = &pChElem->style;

Edit: Also did a little research out of my own curiosity about these interfaces and I believe item returns an HRESULT not an element from what I see http://msdn.microsoft.com/en-us/library/aa703930(v=vs.85).aspx unless you wrapped it or I'm missing something at first blush.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜