我正在为一些非常旧的应用程序创建一个 DCOM 扩展。主应用程序内部有 IE 控件。
使用 C# 我能够获得该 IE 控件的处理程序,如下所示:
Process pr = Process.GetCurrentProcess();
var title = pr.MainWindowTitle;
if(title.IndexOf("My old application -")<0)
{
MessageBox.Show("No such window");
return;
}
var wlwWindow = pr.MainWindowHandle;
var ieWindow = PI.FindWindowRecursive(wlwWindow, "Internet Explorer_Server", null);
if (ieWindow == IntPtr.Zero)
{
MessageBox.Show("Unable to locate IE window.");
return;
}
var myDocument= PI.GetIEDocumentFromWindowHandle(ieWindow);
MessageBox.Show(myDocument.url);
这是我的辅助方法:
public static IntPtr FindWindowRecursive(IntPtr parent, string windowClass, string windowCaption)
{
var found = FindWindowEx(parent, IntPtr.Zero, windowClass, windowCaption);
if (found != IntPtr.Zero)
{
return found;
}
var child = FindWindowEx(parent, IntPtr.Zero, null, null);
while (child != IntPtr.Zero)
{
found = FindWindowRecursive(child, windowClass, windowCaption);
if (found != IntPtr.Zero)
{
return found;
}
child = GetNextWindow(child, 2);
}
return IntPtr.Zero;
}
public static IHTMLDocument2 GetIEDocumentFromWindowHandle(IntPtr hWnd)
{
IHTMLDocument2 htmlDocument = null;
if (hWnd != IntPtr.Zero)
{
uint lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT");
UIntPtr lResult;
SendMessageTimeout(hWnd, lMsg, UIntPtr.Zero, UIntPtr.Zero,
SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 1000, out lResult);
if (lResult != UIntPtr.Zero)
{
htmlDocument = ObjectFromLresult(lResult, typeof(IHTMLDocument).GUID, IntPtr.Zero) as IHTMLDocument2;
if (htmlDocument == null)
{
throw new COMException("Unable to cast to an object of type IHTMLDocument");
}
}
}
return htmlDocument;
}
这部分工作正常,我能够从 temp 获取该 html 页面的内容:
<body id="state" class="saveFavorite" onload="load()" onunload="RUIMaster.Destroy()" style="margin: 0px; padding: 0px; overflow: hidden;">
<p style="display: none;">
<object classid="clsid:22FD36F1-A133-11d4-A0E4-005056E2D8AA" height="0" width="0" id="RUIMaster">
</object>
</object>
<form>
<input id="id" type="hidden" value="" />
</form>
</p>
<iframe name="AAMain" style="margin: 0px; border: 0px; padding: 0px;" id="contents" src="rdaui_frame.htm" width="100%" height="100%" frameborder="0" />
</body>
我必须使用 name 访问 iframe AAMain
。
该 iframe 文档的内容如下所示:
<frameset cols="135,*" border="0" frameborder="no" framespacing="0">
<frame name="Menu" scrolling="no" noresize marginwidth="0" marginheight="0" src="blank.htm">
<frameset rows="310,*,31">
<frame name="Top" noresize marginwidth="0" marginheight="0" src="blank.htm">
<frame name="Centre" noresize marginwidth="0" marginheight="0" src="blank.htm">
<frame name="Bottom" noresize marginwidth="0" marginheight="0" src="blank.htm">
</frameset>
</frameset>
所以基本上我必须访问名为AAMain的iframe内的框架名称Center
myDocument -> iframe(AAMain) -> frame(Centre) -> 然后点击图片
我试图迭代帧,myDocument
但我得到无效的强制转换异常。
try
{
FramesCollection frames = myDocument.frames;
object index = 0;
IHTMLWindow2 frame = (IHTMLWindow2)frames.item(ref index);
var xx = (HTMLDocument)frame.document;
MessageBox.Show(xx.body.innerHTML);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
甚至像这样的代码:
MessageBox.Show(myDocument.frames.length.ToString());
给了我同样的演员例外。