Cross-origin access IFRAME

Source: Internet
Author: User

/// <Summary>
/// IFRAME for cross-origin access
/// </Summary>
///
/*
Htmldocument main_formdoc = webbrowser1.document;
Ihtmldocument2 main_mshtmlihtmldoc = (ihtmldocument2) main_formdoc.domdocument;
Object frame_index = 1;
Ihtmlwindow2 target_mshtmlihtmlwindow = (ihtmlwindow2) main_mshtmlihtmldoc.frames.item (ref frame_index );
Ihtmldocument2 target_mshtmlihtmldoc = crossframeie. getdocumentfromwindow (target_mshtmlihtmlwindow );
*/
Public class crossframeie
{
// Returns NULL in case of failure.
Public static ihtmldocument2 getdocumentfromwindow (ihtmlwindow2 htmlwindow)
{
If (htmlwindow = NULL)
{
Return NULL;
}

// First try the usual way to get the document.
Try
{
Ihtmldocument2 Doc = html##doc ument;
Return Doc;
}
Catch (comexception COMEX)
{
// I think comexception won't be ever fired but just to be sure...
If (COMEX. errorcode! = E_accessdenied)
{
Return NULL;
}
}
Catch (system. unauthorizedaccessexception)
{
}
Catch
{
// Any other error.
Return NULL;
}

// At this point the error was e_accessdenied because the frame contains a document from another domain.
// Ie tries to prevent a cross frame scripting security issue.
Try
{
// Convert ihtmlwindow2 to iwebbrowser2 using iserviceprovider.
Iserviceprovider sp = (iserviceprovider) htmlwindow;

// Use iserviceprovider. queryservice to get iwebbrowser2 object.
Object brws = NULL;
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;
}
Catch
{
}

Return NULL;
}

Private const int e_accessdenied = unchecked (INT) 0x80070005l );
Private Static guid iid_iwebbrowserapp = new GUID ("0002df05-0000-0000-c000-000000000046 ");
Private Static guid iid_iwebbrowser2 = new GUID ("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E ");
}

// This is the com iserviceprovider interface, not system. iserviceprovider. net interface!
[Comimport (), comvisible (true), GUID ("6d5140c1-7436-11ce-8034-00aa006009fa "),
Interfacetypeattribute (cominterfacetype. interfaceisiunknown)]
Public interface iserviceprovider
{
[Return: financialas (unmanagedtype. I4)]
[Preservesig]
Int queryservice (ref guid guidservice, ref guid riid, [financialas (unmanagedtype. Interface)] Out object ppvobject );
}

Cross-origin access IFRAME

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.