/// <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