尝试编辑IHTMLDocument会挂起应用程序 [英] Trying to edit IHTMLDocument hangs an application
问题描述
基于IHTMLDocument2的MSHTML文档,我正在尝试编写简单的HTML解析器. 不幸的是,尝试设置编辑模式失败,换句话说, resultState 从未获得'complete'值,因此应用程序挂起.
based on MSHTML documentation for IHTMLDocument2 I'm trying to write simple HTML parser. Unfortunately trying to set edit-mode fails, in other words resultState never gets 'complete' value so application hangs.
{$APPTYPE CONSOLE}
function ParseHtml(doc: TStringList): TStringList;
var
iHtml: IHTMLDocument2;
v: Variant;
msg: tagMSG;
begin
iHtml := CreateComObject(CLASS_HTMLDocument) as IHTMLDocument2;
Result := TStringList.Create;
try
try
iHtml.designMode := 'on';
while iHtml.readyState <> 'complete' do
PeekMessage(msg, 0, 0, 0, PM_NOREMOVE);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// above loop never finishes
v := VarArrayCreate([0, 0], varVariant);
v[0] := doc.Text;
iHtml.write( PSafeArray(TVarData(v).VArray) );
iHtml.designMode := 'off';
while iHtml.readyState <> 'complete' do
PeekMessage(msg, 0, 0, 0, PM_NOREMOVE);
// processing iHtml.body
...
except
...
end;
finally
...
end;
...
end;
begin
CoInitialize(nil);
...
CoUninitialize;
end.
只是出于好奇,为什么IHTMLDocument2接口的readyState属性永远不会设置为"complete",尽管根据官方文档应该如此?
Just curious why readyState property of IHTMLDocument2 interface is never set to 'complete', although based by official documentation it should ?
推荐答案
尚未将readyState
属性设置为'complete'
,因为您尚未告知IHTMLDocument2
对象实际加载文档.您必须加载一个文档,甚至是空白文档(即'about:blank'
URL),以影响readyState
属性,否则它将保持其初始值'uninitialized'
.
The readyState
property is not being set to 'complete'
because you have not told the IHTMLDocument2
object to actually load a document yet. You have to load a document, even a blank one (ie: the 'about:blank'
URL), in order to affect the readyState
property, otherwise it remains at its initial value of 'uninitialized'
.
这篇关于尝试编辑IHTMLDocument会挂起应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!