描述:
1.
IHTMLDocument2 *pIHTMLDocument2;和
CComQIPtr< IHTMLDocument2 >这样写法有什么区别
2.
我想写个自动填表机,最后一步的IHTMLInputTextElement一直取不到
有啥办法?
解决方案1:
CComQIPtr< IHTMLFormElement > spFormElement = pDisp;
pDisp->Release();
这个时候得到的是一个表单元素,一个表单元素实际上也是一个元素集合,所以可以这样操作
long nElemCount=0;
hr = spFormElement->get_length( &nElemCount );
可以得到表单下子元素的个数
for(long j=0; j<nElemCount; j++)
{
IHTMLInputTextElement pPwdElement;
HRESULT hr = spFormElement->QueryInterface(IID_IHTMLInputTextElement,(void**)&pPwdElement);
.......
这个地方有错,spFormElement是个元素集合,不是一个单元素,所以QueryInterface(IID_IHTMLInputTextElement会失败,它转不成InputTextElemnet,
应该先使用spFormElement->item得到每个子元素的dispatch指针,再对dispatch指针QueryInterface(IID_IHTMLInputTextElement),如果成功就说明这个子元素是一个InputTextElement.
IHTMLDocument2 *pIHTMLDocument2需要自已queryInterface,使用完需要release
CComQIPtr< IHTMLDocument2 >,中间的QI就是QueryInterface,会做一次QueryInterface,
而且不需要自已release.
IHTMLInputTextElement一直取不到有啥办法?
不知道你是怎么取的,一般思路是要得到文档的所有元素集合作遍历。
对IHTLDocument2指针QueryInterface(IHTMLElementCollection),然后用IHTMLElementCollection的item方法遍历得到每个IHTMLElement,然后通过其特征(如InnerText)等判断是否是自已需要的
IHTMLDocument2 *pIHTMLDocument2 在堆上创建
CComQIPtr< IHTMLDocument2 > 模版创建的智能指针
1.
IHTMLDocument2 *pIHTMLDocument2;和
CComQIPtr< IHTMLDocument2 >这样写法有什么区别
第二种写法是只能指针, 里面已经做了一次QueryInterface的