c wm html getobject,使用IHTMLDocument3的getElementsByTagName随机返回任何内容(getElementsByTagName with IHTMLDoc...

使用IHTMLDocument3的getElementsByTagName随机返回任何内容(getElementsByTagName with IHTMLDocument3 randomly returns nothing)

我试图从c ++程序中填充Internet Explorer中的某些表单输入字段,但是我正面临着一个随机错误,我希望这是因为我的代码:

UINT msg = RegisterWindowMessage("WM_HTML_GETOBJECT");

LRESULT result = 0;

SendMessageTimeout(hwnd, msg, NULL, NULL, SMTO_ABORTIFHUNG, 10000, (PDWORD_PTR)&result);

if (!result)

return;

// get main document object

IHTMLDocument3 *doc = NULL;

ObjectFromLresult(result, IID_IHTMLDocument3, NULL, (void**)&doc);

if (!doc)

return;

VARIANT varint, varstr;

varint.vt = VT_I4;

varstr.vt = VT_BSTR;

IHTMLElementCollection* pElemCollections=NULL;

if (FAILED(doc->getElementsByTagName(L"input", &pElemCollections)))

return;

long nelm;

pElemCollections->get_length(&nelm);

...

在最后一行,并且在同一页面上具有相同的HWND,我有时会得到好的数字或输入字段,并且对于nelm通常会得到0。

你在我的代码中看到错误,还是它是一个错误? 请注意,我验证了HWND是正确的,并且从不会调用return 。

谢谢

I'm trying to fill some form input fields in internet explorer from a c++ program but I'm facing a random bug that I hope is because of my code:

UINT msg = RegisterWindowMessage("WM_HTML_GETOBJECT");

LRESULT result = 0;

SendMessageTimeout(hwnd, msg, NULL, NULL, SMTO_ABORTIFHUNG, 10000, (PDWORD_PTR)&result);

if (!result)

return;

// get main document object

IHTMLDocument3 *doc = NULL;

ObjectFromLresult(result, IID_IHTMLDocument3, NULL, (void**)&doc);

if (!doc)

return;

VARIANT varint, varstr;

varint.vt = VT_I4;

varstr.vt = VT_BSTR;

IHTMLElementCollection* pElemCollections=NULL;

if (FAILED(doc->getElementsByTagName(L"input", &pElemCollections)))

return;

long nelm;

pElemCollections->get_length(&nelm);

...

At this last line and with the same HWND at the same page I sometimes get the good number or input fields and often get 0 for nelm.

Do you see something wrong in my code or is it a bug ? Note that I verified that the HWND is correct, and the return are never called.

Thanks

原文:https://stackoverflow.com/questions/45573134

更新时间:2019-11-25 19:34

最满意答案

这样做我没有更多的问题:

UINT msg = RegisterWindowMessage("WM_HTML_GETOBJECT");

LRESULT result = 0;

SendMessageTimeout(hwnd, msg, NULL, NULL, SMTO_ABORTIFHUNG, 10000, (PDWORD_PTR)&result);

if (!result)

return;

// get main document object

IHTMLDocument3 *doc = NULL;

ObjectFromLresult(result, IID_IHTMLDocument3, NULL, (void**)&doc);

if (!doc)

return;

CComVariant varint;

CComVariant varstr;

IHTMLElementCollection* pElemCollections=NULL;

CComBSTR name(L"input")

if (FAILED(doc->getElementsByTagName(name, &pElemCollections)))

return;

long nelm;

pElemCollections->get_length(&nelm);

...

I have no more problem by doing that:

UINT msg = RegisterWindowMessage("WM_HTML_GETOBJECT");

LRESULT result = 0;

SendMessageTimeout(hwnd, msg, NULL, NULL, SMTO_ABORTIFHUNG, 10000, (PDWORD_PTR)&result);

if (!result)

return;

// get main document object

IHTMLDocument3 *doc = NULL;

ObjectFromLresult(result, IID_IHTMLDocument3, NULL, (void**)&doc);

if (!doc)

return;

CComVariant varint;

CComVariant varstr;

IHTMLElementCollection* pElemCollections=NULL;

CComBSTR name(L"input")

if (FAILED(doc->getElementsByTagName(name, &pElemCollections)))

return;

long nelm;

pElemCollections->get_length(&nelm);

...

相关问答

好吧,你得到的正是你要求的 - 属于随机选择的键的值。 要获得key => value pair执行: echo $rand_keys[0] . " => " . $quizes[$rand_keys[0]] . "\n";

当然,您可以根据需要格式化输出。 Well, you are getting exactly what you are asking for - value that belongs to randomly chosen key. To get key => value p

...

这样做我没有更多的问题: UINT msg = RegisterWindowMessage("WM_HTML_GETOBJECT");

LRESULT result = 0;

SendMessageTimeout(hwnd, msg, NULL, NULL, SMTO_ABORTIFHUNG, 10000, (PDWORD_PTR)&result);

if (!result)

return;

// get main document object

IHTMLDocument3 *doc

...

recv()在流的末尾返回零,这在对等体关闭连接时发生。 关于它没有任何“随机”。 recv() returns zero at end of stream, which in turn occurs when the peer closes the connection. There is nothing 'random' about it.

我想到了。 事实证明,我必须解决我忘记的路径,以便Express可以正确地渲染它们。 这不是Gallerific javascript库没有加载,它在我的图库图像的未定义的图像源上抛出错误(我从JSON文件中提取它们)。 一旦我为图像和数据文件放入适当的路径,一切都开始工作了。 感谢所有为我提供建议的人。 这真的帮助我解决了这个问题。 I figured it out. It turns out I had to resolve paths that I had forgotten about s

...

添加Thread.Sleep(5000)应该适用于您的情况。 5000是毫秒,所以它实际上是5秒。 您可以根据您的要求增加时间。 谢谢。 Adding a Thread.Sleep(5000) should work in your case. 5000 is in milliseconds so it is actually 5 secs. You can increase the time as per your requirement. Thanks.

简答:这是为了让你有一些理智。 如果你不知道你是否会得到一个元素或一组元素,你将不得不编写像这样的防御性的类型检查(愚蠢)代码 let foo = document.getElementsByTagName('h1')

if (foo instanceof HTMLCollection)

// do something with all elements

else

// do something with just one element

它让函数总是返回一个已知类型, HTMLElem

...

不幸的是,要完全可靠地完成这项操作,您需要手动完成,例如: function toArray(arraylike) {

var array= new Array(arraylike.length);

for (var i= 0, n= arraylike.length; i

array[i]= arraylike[i];

return array;

}

toArray(document.getElementsByTagName('img')

...

如果doc是局部变量(在过程或函数中),则不必做任何处理。 IHTMLDocument2是一个接口并且被引用计数; 编译器将确保它在超出范围时被释放。 如果它不是局部变量,则可以简单地将其设置为null或Unassigned ,如doc := Unassigned; 这会减少引用计数。 You don't have to do anything to dispose of it, if doc is a local variable (within a procedure or function)

...

编辑: 这是firebug中的一个错误,通过升级到1.10.0a7来修复 因为此方法不可能返回undefined ,所以有两种可能性: 你的调试工具对你说谎 document.getElementsByTagName未引用原始主机对象。 它应该在控制台中引用时打印function getElementsByTagName() {[native code]} 。 你应该能够可靠地查看它是否实际上undefined (在Firefox中): delete window.alert;

window.al

...

选择MSHTML程序集引用,查看其Path属性,你会看到问题: C:\ Windows \组件\ GAC \ Microsoft.mshtml \ 7.0.3300.0__b03f5f7f11d50a3a \ Microsoft.mshtml.dll 这是mshtml类型库的PIA,安装在任何安装了VS的程序员机器上。 版本7.0.3300是.NET 1.0时代的一个版本号,已有14年的历史。 当然,捕获IE11,这就是为什么它没有以后的接口类型。 使用PIA绝不是错误的事情,它可以确保您的程序可

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值