Access is denied trying to access iframe document

53 views
Skip to first unread message

ale...@gmail.com

unread,
Nov 22, 2007, 11:39:34 AM11/22/07
to csEXWB
Hello All,
I'm trying to ge to the document object of an iframe and I get access
is denied exception.
Does any1 know how to get the document object in this case?

here's my function code:

private void GetIframe(IHTMLDocument3 doc)
{

foreach (IHTMLFrameBase2 iframe in
(IHTMLElementCollection)doc.getElementsByTagName("iframe"))
{
IHTMLDocument3 doc1 =
(IHTMLDocument3)iframe.contentWindow.document;
Return GetPageData(doc1);
}
}


thanks

mh

unread,
Nov 22, 2007, 1:47:21 PM11/22/07
to csEXWB
Hi,

This could possibly be related to cross domain issues.

Do you have a url to test? OS and IE versions?

MH

Dominik Flejter

unread,
May 22, 2009, 9:41:18 AM5/22/09
to csEXWB
I had the same problem and found a solution at
http://codecentrix.blogspot.com/2008/02/when-ihtmlwindow2document-throws.html

Here comes the code:

using System.Runtime.InteropServices;

...

IHTMLWindow2 win = fbel.contentWindow as
IHTMLWindow2;
if (win != null)
{
IHTMLDocument2 doc;
try
{
doc = win.document as IHTMLDocument2;
}
catch (System.UnauthorizedAccessException ex)
{
// this exception happens with (I)FRAMES
from different domain
// solution adapted from:
// http://codecentrix.blogspot.com/2008/02/when-ihtmlwindow2document-throws.html
doc = null;
IServiceProvider sp = (IServiceProvider)
win;
Object brws = null;
sp.QueryService(ref IID_IWebBrowserApp,
ref IID_IWebBrowser2, out brws);

// Get the document from IWebBrowser2.
IWebBrowser2 browser = (IWebBrowser2)
(brws);

doc = (IHTMLDocument2)browser.Document;

}

...

private static Guid IID_IWebBrowserApp = new Guid
("0002DF05-0000-0000-C000-000000000046");
private static Guid IID_IWebBrowser2 = new Guid("D30C1661-
CDAF-11D0-8A3E-00C04FC9E26E");

[ComImport(), ComVisible(true), Guid
("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceTypeAttribute
(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServiceProvider
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int QueryService(ref Guid guidService, ref Guid riid,
[MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}

....

DF
Reply all
Reply to author
Forward
0 new messages