一个关于VC获取指针的问题
dl315 2014-04-24 11:43:04 我写了这样一段代码:
void CMyWebDlg::OnBnClickedQuerydata()
{
// TODO: 在此添加控件通知处理程序代码
CComVariant vtEmpty;
m_web.put_Silent(TRUE);
m_web.Navigate2(&CComVariant("http://abc.com"), &vtEmpty,&vtEmpty, &vtEmpty, &vtEmpty);//打开指定的网页
CComPtr < IDispatch >spDispDoc;
spDispDoc =m_web.get_Document();
UpdateData();
CComQIPtr< IHTMLDocument2 >spDocument2 = spDispDoc;
AfxMessageBox("111"); // 注释1
CComQIPtr<IHTMLElementCollection > spElementCollection;
HRESULT hr;
hr =spDocument2->get_all(&spElementCollection);
// 注释2
if(SUCCEEDED(hr))
{
// 注释3
CComPtr<IDispatch>spDisp;
CComPtr<IDispatch>spDisp1;
HRESULT hr1,hr2;
hr1 =spElementCollection->item(CComVariant("DeptName"),CComVariant("0"),&spDisp);
hr2 =spElementCollection->item(CComVariant("OfficeName"),CComVariant("0"), &spDisp1);
if(SUCCEEDED(hr1))
{
UpdateData();
if(NULL== spDisp)
{
AfxMessageBox("NULL!");
}
else
{
AfxMessageBox("Not NULL!");
}
CComQIPtr<IHTMLInputElement > pElement = spDisp;
pElement->put_value(CComBSTR("222"));
CComQIPtr<IHTMLInputElement > pElement1 = spDisp1;
pElement1->put_value(CComBSTR("333"));
}
}
}
现在的问题是,我在注释1处加了AfxMessageBox,在注释4处就能获取非空指针spDisp,而如果在注释1处不加AfxMessageBox,或者把AfxMessageBox移动到注释2处或者之后,在注释4处获取的spDisp就是空指针,请问这是为什么?求解释!
另外如果我不想在注释1处加AfxMessageBox,又想让注释4处获取的指针非空,该如何修改?