如何跨域操作IE浏览器呢?在上次我的这篇帖子中已经有到 http://blog
.csdn.net/u012395622/article/details/46426043 不过它并不是一个很好
的解决方案 因为你无法操作非 Internet Explorer 但基于IE内核的程序
看上图你可以明确的看到由“易语言”编写的一个简单的“超文本浏览框”
小软件处于运行状态,然而它却跳转到我的博客上,你可以在上明确
的知道,是去浏览百度的主页。
看上图,你会看到代码在远程跨域操作对方的浏览器进行跳转到我的
博客,你可以理解为浏览器劫持。
首先你需要准备一个“窗口Spy++”工具 可以使用“Visual Studio”自带的
Spy++工具,你首先需要去寻找一个“基于IE内核”的浏览器软件,得到
它的句柄,但记得窗口的类名必须是“Internet Explorer_Server”仔细看
上图,然后把它的窗口句柄,提供给GetHtmlDocumentByHandle函数
参考本帖:http://blog.csdn.net/u012395622/article/details/46503661
获取到webDomDoc后,调用CorssDomain.GetWebBrowserByHtml
Document函数获取到浏览器的操作指针(IWebBrowser2)然后下面我
们就可以想怎么愉快的玩耍就怎么愉快的玩耍,你可以为它挂接一个
事件接口用于监听浏览器POST请求或者直接干扰用户浏览器跳转等
示例代码一:
object webDomDoc = GetHtmlDocumentByHandle((IntPtr)4784608); CorssDomain.IWebBrowser2 webObject = CorssDomain.GetWebBrowserByHtmlDocument(webDomDoc); webObject.Silent = true; object unknown = null; webObject.Navigate("http://blog.csdn.net/u012395622", ref unknown, ref unknown, ref unknown, ref unknown);示例代码二:
CorssDomain.IWebBrowser2 webObject = CorssDomain.GetWebBrowserByHtmlDocument(webBrowser1.Document.DomDocument); webObject.Silent = true; object unknown = null; webObject.Navigate("http://blog.csdn.net/u012395622", ref unknown, ref unknown, ref unknown, ref unknown);CorssDomain:
using System; using System.Reflection; using System.Runtime.InteropServices; namespace CorssDomain { public partial class CorssDomain { [ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IServiceProvider { [return: MarshalAs(UnmanagedType.I4)] [PreserveSig] int QueryService(ref Guid guidService, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppvObject); } [ComImport, Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E"), TypeLibType(TypeLibTypeFlags.FOleAutomation | TypeLibTypeFlags.FDual | TypeLibTypeFlags.FHidden)] public interface IWebBrowser2 { [DispId(100)] void GoBack(); [DispId(0x65)] void GoForward(); [DispId(0x66)] void GoHome(); [DispId(0x67)] void GoSearch(); [DispId(0x68)] void Navigate([In] string Url, [In] ref object flags, [In] ref object targetFrameName, [In] ref object postData, [In] ref object headers); [DispId(-550)] void Refresh(); [DispId(0x69)] void Refresh2([In] ref object level); [DispId(0x6a)] void Stop(); [DispId(200)] object Application {[return: MarshalAs(UnmanagedType.IDispatch)] get; } [DispId(0xc9)] object Parent {[return: MarshalAs(UnmanagedType.IDispatch)] get; } [DispId(0xca)] object Container {[return: MarshalAs(UnmanagedType.IDispatch)] get; } [DispId(0xcb)] object Document {[return: MarshalAs(UnmanagedType.IDispatch)] get; } [DispId(0xcc)] bool TopLevelContainer { get; } [DispId(0xcd)] string Type { get; } [DispId(0xce)] int Left { get; set; } [DispId(0xcf)] int Top { get; set; } [DispId(0xd0)] int Width { get; set; } [DispId(0xd1)] int Height { get; set; } [DispId(210)] string LocationName { get; } [DispId(0xd3)] string LocationURL { get; } [DispId(0xd4)] bool Busy { get; } [DispId(300)] void Quit(); [DispId(0x12d)] void ClientToWindow(out int pcx, out int pcy); [DispId(0x12e)] void PutProperty([In] string property, [In] object vtValue); [DispId(0x12f)] object GetProperty([In] string property); [DispId(0)] string Name { get; } [DispId(-515)] int HWND { get; } [DispId(400)] string FullName { get; } [DispId(0x191)] string Path { get; } [DispId(0x192)] bool Visible { get; set; } [DispId(0x193)] bool StatusBar { get; set; } [DispId(0x194)] string StatusText { get; set; } [DispId(0x195)] int ToolBar { get; set; } [DispId(0x196)] bool MenuBar { get; set; } [DispId(0x197)] bool FullScreen { get; set; } [DispId(500)] void Navigate2([In] ref object URL, [In] ref object flags, [In] ref object targetFrameName, [In] ref object postData, [In] ref object headers); [DispId(0x1f5)] OLECMDF QueryStatusWB([In] OLECMDID cmdID); [DispId(0x1f6)] void ExecWB([In] OLECMDID cmdID, [In] OLECMDEXECOPT cmdexecopt, ref object pvaIn, IntPtr pvaOut); [DispId(0x1f7)] void ShowBrowserBar([In] ref object pvaClsid, [In] ref object pvarShow, [In] ref object pvarSize); [DispId(-525)] int ReadyState { get; } [DispId(550)] bool Offline { get; set; } [DispId(0x227)] bool Silent { get; set; } [DispId(0x228)] bool RegisterAsBrowser { get; set; } [DispId(0x229)] bool RegisterAsDropTarget { get; set; } [DispId(0x22a)] bool TheaterMode { get; set; } [DispId(0x22b)] bool AddressBar { get; set; } [DispId(0x22c)] bool Resizable { get; set; } } public enum OLECMDF { OLECMDF_DEFHIDEONCTXTMENU = 0x20, OLECMDF_ENABLED = 2, OLECMDF_INVISIBLE = 0x10, OLECMDF_LATCHED = 4, OLECMDF_NINCHED = 8, OLECMDF_SUPPORTED = 1 } public enum OLECMDID { OLECMDID_PAGESETUP = 8, OLECMDID_PRINT = 6, OLECMDID_PRINTPREVIEW = 7, OLECMDID_PROPERTIES = 10, OLECMDID_SAVEAS = 4 } public enum OLECMDEXECOPT { OLECMDEXECOPT_DODEFAULT, OLECMDEXECOPT_PROMPTUSER, OLECMDEXECOPT_DONTPROMPTUSER, OLECMDEXECOPT_SHOWHELP } public static Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E"); public static Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046"); public const int S_OK = 0; public static IWebBrowser2 GetWebBrowserByHtmlDocument(object ppvObject) { object webObject = null; if (ppvObject != null && Marshal.IsComObject(ppvObject)) { IServiceProvider sp; //ppvObject = ppvObject.GetType(). // InvokeMember("parentWindow", BindingFlags.GetProperty, null, ppvObject, null); if ((sp = ppvObject as IServiceProvider) != null) { if (sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out webObject) != S_OK) throw new ArgumentException(); } } return webObject as IWebBrowser2; } } }
[DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetClassName( [In]IntPtr hWnd, [MarshalAs(UnmanagedType.VBByRefStr)]ref string IpClassName, [In]int nMaxCount ); [DllImport("oleacc.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ObjectFromLresult( [In]int lResult, [In]ref Guid riid, [In]int wParam, [Out, MarshalAs(UnmanagedType.IUnknown)]out object ppvObject ); [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.I4)] public static extern int RegisterWindowMessage( [In]string lpString ); [DllImport("user32.dll", EntryPoint = "SendMessageTimeoutA", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SendMessageTimeout( [In]IntPtr MSG, [In]int hWnd, [In]int wParam, [In]int lParam, [In]int fuFlags, [In]int uTimeout, [In, Out]ref int lpdwResult ); public static object GetComObjectByHandle(int Msg, Guid riid, IntPtr hWnd) { object _ComObject; int lpdwResult = 0; if (!SendMessageTimeout(hWnd, Msg, 0, 0, SMTO_ABORTIFHUNG, 1000, ref lpdwResult)) return null; if (ObjectFromLresult(lpdwResult, ref riid, 0, out _ComObject)) return null; return _ComObject; } public object GetHtmlDocumentByHandle(IntPtr hWnd) { string buffer = new string('\0', 24); GetClassName(hWnd, ref buffer, 25); if (buffer != "Internet Explorer_Server") return null; return GetComObjectByHandle(WM_HTML_GETOBJECT, IID_IHTMLDocument, hWnd); } public const int SMTO_ABORTIFHUNG = 2; public readonly static int WM_HTML_GETOBJECT = RegisterWindowMessage("WM_HTML_GETOBJECT"); public readonly static Guid IID_IHTMLDocument = new Guid("626fc520-a41e-11cf-a731-00a0c9082637");
原文地址:http://blog.csdn.net/u012395622/article/details/46517373