none
get the current instance IE RRS feed

  • Question

  • Bonjour,

    j'ai injecter un script dans une page IE en utilisant le BHO .et j'ai une interface windows form qui s'ouvre par clique droti sur un element de la page . cette interface contient un bouton ; je veux que lorsque je clique sur le bouton je recupere cette instance . voici le code de ma classe :

     [ComVisible(true),
         Guid("2159CB25-EF9A-54C1-B43C-E30D1A4A8277"),
         ClassInterface(ClassInterfaceType.None)]
        public partial class ApplicationTesting : Form,IObjectWithSite
        {

    private SHDocVw.WebBrowser webBrowser;

    private HTMLDocument document;

     public int SetSite(object site)
            {
                if (site != null)
                {
                    webBrowser = (SHDocVw.WebBrowser)site;
                    TEST = webBrowser.Name;
                    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 void OnDocumentComplete(object pDisp, ref object URL)
            {
                string url = URL as string;
                if (string.IsNullOrEmpty(url)
                    || url.Equals("about:blank", StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }
                InternetExplorer explorer = pDisp as InternetExplorer;

                // Set the handler of the document in InternetExplorer.
                if (explorer != null)
                {
                     document = (HTMLDocument)webBrowser.Document;
                    IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)
                                  document.all.tags("head")).item(null, 0);
                    IHTMLScriptElement scriptObject =
                      (IHTMLScriptElement)document.createElement("script");
                    scriptObject.type = @"text/javascript";
                    scriptObject.text = "\nfunction highlight(test1){alert(test1);}\n\n";
                    ((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject);
    }

    public const string BHO_REGISTRY_KEY_NAME =
               "Software\\Microsoft\\Windows\\" +
               "CurrentVersion\\Explorer\\Browser Helper Objects";

            [ComRegisterFunction]
            public static void RegisterBHO(Type type)
            {
                RegistryKey registryKey =
                  Registry.LocalMachine.OpenSubKey(BHO_REGISTRY_KEY_NAME, true);

                if (registryKey == null)
                    registryKey = Registry.LocalMachine.CreateSubKey(
                                            BHO_REGISTRY_KEY_NAME);

                string guid = type.GUID.ToString("B");
                RegistryKey ourKey = registryKey.OpenSubKey(guid);

                if (ourKey == null)
                {
                    ourKey = registryKey.CreateSubKey(guid);
                }

                ourKey.SetValue("NoExplorer", 1, RegistryValueKind.DWord);

                registryKey.Close();
                ourKey.Close();
            }

            [ComUnregisterFunction]
            public static void UnregisterBHO(Type type)
            {
                RegistryKey registryKey =
                  Registry.LocalMachine.OpenSubKey(BHO_REGISTRY_KEY_NAME, true);
                string guid = type.GUID.ToString("B");

                if (registryKey != null)
                    registryKey.DeleteSubKey(guid, false);
            }
     public ApplicationTesting()
            {
                InitializeComponent();
            }

    ....................

     private void button1_Click(object sender, EventArgs e)
            {
               try
               {
                    document = (HTMLDocument)webBrowser.Document;
                   
                    if (document != null)
                    {
                       IHTMLWindow2 parentWindow = document.parentWindow;
                       if (parentWindow != null)
                        {
                            string a = "//DIV[@id=searchform]/FORM[@id=tsf]/DIV/DIV[3]/CENTER/INPUT[@name=btnK]";
                          parentWindow.execScript("highlight('" + a + "')", "javascript");
                        }
                   }
                }
                catch (Exception ex) { MessageBox.Show(ex.ToString()); }
            }

    //

    l'erreur qui s'affiche est : la reference d'objet n'est pas définie à une instance d'un objet.

    est ce que quelqu'un peut m'aider

    merci

    mercredi 1 février 2012 10:51

Réponses

Toutes les réponses