how to handle bad pointers

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
hi,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.

<div style="color:Black;background-color:White; <pre>
hash_map<wstring, wstring> CMyAppDlg::GetNodeStyles(VARIANT varSrc)
{
<span style="color:Blue; 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, (<span style="color:Blue; 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;
<span style="color:Blue; for(<span style="color:Blue; long i = 0; i < lLength; i++)
{
pChElem = pElemColl->item(_variant_t(i), _variant_t());

MessageBox(pChElem->tagName, _T(<span style="color:#A31515; "The tag name of this html element is"), MB_OK);
pStyle = pChElem->style;

pStyle->fontStyle;

hmStyles[wstring(pStyle->fontStyle)] = L<span style="color:#A31515; "FontStyle";

hmStyles[wstring(pStyle->fontFamily)] = L<span style="color:#A31515; "FontFamily";

hmStyles[wstring(pStyle->textDecoration)] = L<span style="color:#A31515; "TextDecoration";
}

<span style="color:Blue; return hmStyles;
}
[/code]

<br/>
the problem is when read in an arbitrary html code like:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; A <span style="color:Red; href<span style="color:Blue; =<span style="color:Blue; /servlet/BookDetailsPL?bi=1257056972&amp;tab=1&amp;searchurl=bt.x%3D44%26bt.y%3D10%26sts%3Dt%26tn%3Dharry%2Bpotter <span style="color:Red; cmImpressionSent<span style="color:Blue; =<span style="color:Blue; 1<span style="color:Blue; >The Orchard Bookshop.<span style="color:Blue; </<span style="color:#A31515; A<span style="color:Blue; > <span style="color:Blue; <<span style="color:#A31515; SPAN <span style="color:Red; class<span style="color:Blue; =<span style="color:Blue; scndInfo<span style="color:Blue; >(Hayes., UK, United Kingdom)<span style="color:Blue; </<span style="color:#A31515; SPAN<span style="color:Blue; >
[/code]

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

<div style="color:Black;background-color:White; <pre>
pStyle->fontStyle;
[/code]
<div style="color:Black;background-color:White; <pre>
pStyle->fontFamily
[/code]
<div style="color:Black;background-color:White; <pre>
pStyle->textDecoration
[/code]

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.
cheers
daiyue

View the full article
 
Back
Top