日期:2014-05-18 浏览次数:20939 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; using SHDocVw; using mshtml; using System.Runtime.InteropServices; using Microsoft.Win32; namespace BHO_Gyc.BHO { [ ComVisible(true), Guid("8a194578-81ea-4850-9911-13ba2d71efbd"), ClassInterface(ClassInterfaceType.None) ] public class BHO:IObjectWithSite { WebBrowser webBrowser; HTMLDocument document; public void OnDocumentComplete(object pDisp,ref object URL) { //string url = URL as string; //object anObject = new object(); //if (url.IndexOf("about:blank") >= 0) //{ // return; //} //if (url.IndexOf("baidu.com") >= 0) //{ // webBrowser.Navigate("http://www.sohu.com", ref anObject, ref anObject, ref anObject, ref anObject); //} try { document = (HTMLDocument)webBrowser.Document; foreach (IHTMLInputElement tempElement in document.getElementsByTagName("INPUT")) { System.Windows.Forms.MessageBox.Show(tempElement.name != null ? tempElement.name : "it sucks,no name,try id" + ((IHTMLElement)tempElement).id); } } catch(Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); } } public int SetSite(object site) { if (site != null) { webBrowser = (WebBrowser)site; webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete); } else { webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete); webBrowser = null; } return 0; } public int GetSite(ref Guid guid, out IntPtr ppvsite) { IntPtr punk = Marshal.GetIUnknownForObject(webBrowser); int hr = Marshal.QueryInterface(punk, ref guid, out ppvsite); Marshal.Release(punk); return hr; } public static string BHOKEYNAME = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects"; [ComRegisterFunction] public static void RegisterBHO(Type type) { try { RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true); if (registryKey == null) { registryKey = Registry.LocalMachine.CreateSubKey(BHOKEYNAME); } string guid = type.GUID.ToString("B"); RegistryKey ourkey = registryKey.OpenSubKey(guid); if (ourkey == null) { ourkey = registryKey.CreateSubKey(guid); } ourkey.SetValue("Alright", 1); registryKey.Close(); ourkey.Close(); } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message+"1"); } } [ComRegisterFunction] public static void UnregisterBHO(Type type) { RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true); string guid = type.GUID.ToString("B"); if (registryKey != null) { registryKey.DeleteSubKey(guid,false); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text;