开发者

Accessing a frame name results on an "Access is denied" exception

开发者 https://www.devze.com 2023-02-28 05:54 出处:网络
I\'m making a toolbar on C# for IE and I need to access a certain frame within the website. On my initial tests, I\'m just trying to cycle through the frames and have a MessageBox popup with the name

I'm making a toolbar on C# for IE and I need to access a certain frame within the website. On my initial tests, I'm just trying to cycle through the frames and have a MessageBox popup with the name of the frame. But I'm encountering an exception whenever I try to do it, saying "Access is denied". The full text of the exception is:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Here's the code:

IHTMLSelectionObject currentSelection = myHTMLDocumen开发者_C百科t.selection;

IHTMLFramesCollection2 frames = (IHTMLFramesCollection2)myHTMLDocument.frames;

for (int i = 0; i < frames.length; i++)
{
    object refIndex = i;

    IHTMLWindow2 currentFrame = (IHTMLWindow2)frames.item(ref refIndex);

    if (currentFrame != null)
    {
        MessageBox.Show(currentFrame.name);
    }
    else
        MessageBox.Show("Null");

}

From my searchs on the web, I found out that this is, in fact, not a bug. It's expected to behave that way. My question is: what is the correct way to do what I'm trying to do?

Thanks in advance!


Probably you are getting this message because you are trying to access a frame from another domain. The same origin policy prevent you doing this. To make it work use IServiceProvider.

IServiceProvider isp = (IServiceProvider) currentFrame;

then query using QueryService to get IWebBrowser2 object.

Make sure you use System.Runtime.InteropServices

0

精彩评论

暂无评论...
验证码 换一张
取 消