我有这段代码会做相反的事情:获取给定 iframe 的子文档element
:
var htmlWindow = (element as IHTMLFrameBase2).contentWindow;
if (htmlWindow == null) return null;
// Convert IHTMLWindow2 to IWebBrowser2 using IServiceProvider.
IServiceProvider sp = (IServiceProvider)htmlWindow;
// Use IServiceProvider.QueryService to get IWebBrowser2 object.
Object brws = null;
//brws =
sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out brws);
// Get the document from IWebBrowser2.
SHDocVw.IWebBrowser2 browser = (SHDocVw.IWebBrowser2)(brws);
return (IHTMLDocument2)browser.Document;
我的问题是我该怎么做相反的事情?我试着给孩子这样做,document
但我得到一个 null frameWindow
:
Object frameWindow;
IServiceProvider isp = (IServiceProvider)document.parentWindow;
isp.QueryService(ref IID_IWebBrowserApp, ref IID_IHTMLFrameBase, out frameWindow);
以下是供您参考的所有 GUID:
private static Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
private static Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E");
private static Guid IID_IHTMLFrameBase2 = new Guid("3050F6DB-98B5-11CF-BB82-00AA00BDCE0B");
private static Guid IID_IHTMLFrameBase = new Guid("3050F311-98B5-11CF-BB82-00AA00BDCE0B");
private static Guid IID_IHTMLWindow2 = new Guid("332C4427-26CB-11D0-B483-00C04FD90119");
这是我的IServiceProvider
:
[ComImport(), ComVisible(true), Guid("6D5140C1-7436-11CE-8034-00AA006009FA"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
private interface IServiceProvider
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int QueryService(ref Guid guidService, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}