2
我有這樣的代碼,卻反其道而行之:被賦予一個iframe element
子文件:如何從跨域iframe中的子文檔獲取父文檔中的iframe元素?
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
但我發現了一個空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);
}