begin process at 2012 02 11 18:13:06
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

.NET

 > COM INTEROP AVEC INTERNET EXPLORER (SHDOCVW, MSHTML) EN C#

COM INTEROP AVEC INTERNET EXPLORER (SHDOCVW, MSHTML) EN C#


 Information sur la source

Note :
Aucune note
Catégorie :.NET Source .NET ( DotNet ) Classé sous :COM-Interop, SHDocVw, Explorer Niveau :Initié Date de création :25/05/2009 Date de mise à jour :30/05/2009 15:36:11 Vu / téléchargé :5 538 / 132

Auteur : didiermeo

Ecrire un message privé
Site perso
Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

 Description

L'utilisation de SHDocVw.dll et MSHTML.tlb pour acquérir des données d'une page web (HTML) affichée par Internet Explorer. Cette librairie contient une methode qui livre le nombre des Explorer (Explorer.exe et IExplore.exe) déjà executés, le contenu HTML de la page, l'adresse URL et le titre affichés par Internet Explorer. Il faut attribuer à la propriété SearchedTitle une valeur qui équivaut à un titre affiché par un Internet Explorer, pour en acquérir les données de cette page web. Cela est possible à l'aide de COM Interoperabilité entre MS.Net (C#) et le système Windows, ici Windows XP.

Source

  • using System;
  • //using System.Collections.Generic;
  • //using System.Linq;
  • //using System.Text;
  • using System.Runtime.InteropServices;
  • using System.Reflection;
  • //First add these references: SHDocVw.dll and mshtml.tlb
  • using mshtml;
  • using SHDocVw; // SHDocVw can be replaced by MeoSHDocVw
  • //MeoSHDocVw.dll is an interop dll for SHDocVW.dll with a strong name key in the file MeoSHDocVw.snk,
  • //created like this:
  • //1) run sn.exe -k MeoSHDocVw.snk
  • //2) After that run TlbImp.exe c:\windows\system32\ShdocVw.dll /keyfile:MeoSHDocVw.snk /out:MeoSHDocVw.dll
  • namespace ClassLibrary1
  • {
  • /// <summary>
  • /// Autor: Didier Meo, ICT Consultant, developed for http://www.meo-x.net
  • /// </summary>
  • public class IExplorer
  • {
  • #region class variables & Constructor
  • //private static SHDocVw.InternetExplorer _Explorer = null;
  • private static SHDocVw.IWebBrowserApp _Webbrowser = null;
  • private static mshtml.HTMLDocument _objDocument = null;
  • private SHDocVw.ShellWindows _ShellWindows = null;
  • private int _iIEnr = 0;
  • private bool _blnSearchedExplorer = false;
  • private string _strSearchedTitle = null;
  • private string _strContent = null, _strContentAll = null, _strLocationURL = null, _strTitle = null;
  • private int _iFrameNumber = 0;
  • ///
  • /// Constructor for IExplorer
  • ///
  • public IExplorer()
  • {
  • this._strSearchedTitle = "";
  • this._Init();
  • }
  • #endregion
  • #region Properties
  • ///
  • /// Location URL
  • ///
  • public string HttpUrl
  • {
  • get
  • {
  • return _strLocationURL;
  • }
  • set
  • {
  • _strLocationURL = (string)value.ToString();
  • }
  • }
  • ///
  • /// Searched Title
  • ///
  • public string SearchedTitle
  • {
  • set
  • {
  • _strSearchedTitle = (string)value.ToString();
  • }
  • }
  • ///
  • /// Content for a Frame or for a page without Frames
  • ///
  • public string Content
  • {
  • get
  • {
  • return this._strContent;
  • }
  • }
  • ///
  • /// Content of an HTML page without Frames or Content for all Frames separated by STX
  • ///
  • public string ContentAll
  • {
  • get
  • {
  • return this._strContentAll;
  • }
  • }
  • ///
  • /// Title of an HTML page
  • ///
  • public string Title
  • {
  • get
  • {
  • return this._strTitle;
  • }
  • }
  • ///
  • /// Number of frames for an HTML page
  • ///
  • public int FrameNumber
  • {
  • get
  • {
  • return this._iFrameNumber;
  • }
  • }
  • #endregion
  • #region Methods
  • ///
  • /// Initialization of Variables
  • ///
  • [ComVisible(false)]
  • private void _Init()
  • {
  • this._strContent = "";
  • this._strContentAll = "";
  • this._strLocationURL = "";
  • this._strTitle = "";
  • this._iFrameNumber = 0;
  • this._iIEnr = 0;
  • }
  • ///
  • /// Gett Internet Explorer Data (URL, Title, Content) for a searched HTML page or for all its frames.
  • ///
  • public void GetIExplorerData()
  • {
  • this._Init();
  • this._blnSearchedExplorer = false;
  • _ShellWindows = new SHDocVw.ShellWindows();
  • _iIEnr = _ShellWindows.Count;
  • if (_iIEnr < 1)
  • {
  • this._blnSearchedExplorer = false;
  • return;
  • }
  • _objDocument = new mshtml.HTMLDocument();
  • for (int nI = 0; nI < _iIEnr; nI++)
  • {
  • object index = (object)nI;
  • _strLocationURL = "";
  • _strTitle = "";
  • object oSWItem = _ShellWindows.Item(index);
  • _Webbrowser = (IWebBrowserApp)oSWItem;
  • try
  • {
  • _objDocument = (mshtml.HTMLDocument)_Webbrowser.Document;
  • _strLocationURL = _Webbrowser.LocationURL;
  • _strTitle = _objDocument.title;
  • if (_strTitle.IndexOf(this._strSearchedTitle) > -1)
  • {
  • this._blnSearchedExplorer = true;
  • }
  • }
  • catch (System.Exception exc)
  • {
  • if (nI >= _iIEnr - 1)
  • {
  • return;
  • }
  • }
  • //
  • if (this._blnSearchedExplorer)
  • {
  • this._strContent = "";
  • this._strContentAll = "";
  • mshtml.FramesCollection oFrameCol = _objDocument.frames;
  • this._iFrameNumber = oFrameCol.length;
  • if (_iFrameNumber == 0)
  • {
  • try
  • {
  • this._strContent = _objDocument.body.outerHTML;
  • this._strContentAll = this._strContent;
  • }
  • catch (System.Exception exc)
  • {
  • return;
  • }
  • return;
  • }
  • this._strContent = "";
  • this._strContentAll = "";
  • this._iFrameNumber = 0;
  • IHTMLElementCollection iElcol = _objDocument.getElementsByTagName("frame");
  • foreach (IHTMLElement iEl in iElcol)
  • {
  • HTMLFrameElement frm = (HTMLFrameElement)iEl;
  • DispHTMLDocument doc = (DispHTMLDocument)((SHDocVw.IWebBrowser2)frm).Document;
  • if (_iFrameNumber > 0) this._strContentAll += Convert.ToChar(2); //STX (start of text) = ASCII CHAR(2)
  • this._strContent = doc.body.outerHTML;
  • this._strContentAll += this._strContent;
  • _iFrameNumber++;
  • }
  • return;
  • }
  • else
  • {
  • this._strContent = "";
  • this._strContentAll = "";
  • _iFrameNumber = -1;
  • _strLocationURL = "";
  • _strTitle = "";
  • }
  • }
  • }
  • #endregion
  • }
  • }
using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
//First add these references: SHDocVw.dll and mshtml.tlb
using mshtml;
using SHDocVw; // SHDocVw can be replaced by MeoSHDocVw
//MeoSHDocVw.dll is an interop dll for SHDocVW.dll with a strong name key in the file MeoSHDocVw.snk,
//created like this: 
//1) run  sn.exe -k MeoSHDocVw.snk 
//2) After that  run TlbImp.exe c:\windows\system32\ShdocVw.dll /keyfile:MeoSHDocVw.snk /out:MeoSHDocVw.dll 

namespace ClassLibrary1
{
    /// <summary>
    /// Autor: Didier Meo, ICT Consultant, developed for http://www.meo-x.net
    /// </summary>
    public class IExplorer
    {
        #region class variables & Constructor

        //private static SHDocVw.InternetExplorer _Explorer = null;
        private static SHDocVw.IWebBrowserApp _Webbrowser = null;
        private static mshtml.HTMLDocument _objDocument = null;
        private SHDocVw.ShellWindows _ShellWindows = null;
        private int _iIEnr = 0;
        private bool _blnSearchedExplorer = false;
        private string _strSearchedTitle = null;
        private string _strContent = null, _strContentAll = null, _strLocationURL = null, _strTitle = null;
        private int _iFrameNumber = 0;


        /// 
        /// Constructor for IExplorer
        /// 
        public IExplorer()
        {
            this._strSearchedTitle = "";
            this._Init();
        }

        #endregion


        #region Properties

        /// 
        /// Location URL 
        /// 
        public string HttpUrl
        {
            get
            {
                return _strLocationURL;
            }
            set
            {
                _strLocationURL = (string)value.ToString();
            }
        }


        /// 
        /// Searched Title 
        /// 
        public string SearchedTitle
        {
            set
            {
                _strSearchedTitle = (string)value.ToString();
            }
        }

        /// 
        /// Content for a Frame or for a page without Frames
        /// 
        public string Content
        {
            get
            {
                return this._strContent;
            }
        }

        /// 
        /// Content of an HTML page without Frames or Content for all Frames separated by STX 
        /// 
        public string ContentAll
        {
            get
            {
                return this._strContentAll;
            }
        }

        /// 
        /// Title of an HTML page
        /// 
        public string Title
        {
            get
            {
                return this._strTitle;
            }
        }

        /// 
        /// Number of frames for an HTML page
        /// 
        public int FrameNumber
        {
            get
            {
                return this._iFrameNumber;
            }
        }

        #endregion


        #region Methods

        /// 
        /// Initialization of Variables
        /// 
        [ComVisible(false)]
        private void _Init()
        {
            this._strContent = "";
            this._strContentAll = "";
            this._strLocationURL = "";
            this._strTitle = "";
            this._iFrameNumber = 0;
            this._iIEnr = 0;
        }


        /// 
        /// Gett Internet Explorer Data (URL, Title, Content) for a searched HTML page or for all its frames. 
        /// 
        public void GetIExplorerData()
        {
            this._Init();
            this._blnSearchedExplorer = false;
            _ShellWindows = new SHDocVw.ShellWindows();
            _iIEnr = _ShellWindows.Count;
            if (_iIEnr < 1)
            {
                this._blnSearchedExplorer = false;
                return;
            }
            _objDocument = new mshtml.HTMLDocument();
            for (int nI = 0; nI < _iIEnr; nI++)
            {
                object index = (object)nI;
                _strLocationURL = "";
                _strTitle = "";
                object oSWItem = _ShellWindows.Item(index);
                _Webbrowser = (IWebBrowserApp)oSWItem;
                try
                {
                    _objDocument = (mshtml.HTMLDocument)_Webbrowser.Document;
                    _strLocationURL = _Webbrowser.LocationURL;
                    _strTitle = _objDocument.title;
                    if (_strTitle.IndexOf(this._strSearchedTitle) > -1)
                    {
                        this._blnSearchedExplorer = true;
                    }
                }
                catch (System.Exception exc)
                {
                    if (nI >= _iIEnr - 1)
                    {
                        return;
                    }
                }
                //
                if (this._blnSearchedExplorer)
                {
                    this._strContent = "";
                    this._strContentAll = "";
                    mshtml.FramesCollection oFrameCol = _objDocument.frames;
                    this._iFrameNumber = oFrameCol.length;
                    if (_iFrameNumber == 0)
                    {
                        try
                        {
                            this._strContent = _objDocument.body.outerHTML;
                            this._strContentAll = this._strContent;
                        }
                        catch (System.Exception exc)
                        {
                            return;
                        }
                        return;
                    }
                    this._strContent = "";
                    this._strContentAll = "";
                    this._iFrameNumber = 0;
                    IHTMLElementCollection iElcol = _objDocument.getElementsByTagName("frame");
                    foreach (IHTMLElement iEl in iElcol)
                    {
                        HTMLFrameElement frm = (HTMLFrameElement)iEl;
                        DispHTMLDocument doc = (DispHTMLDocument)((SHDocVw.IWebBrowser2)frm).Document;
                        if (_iFrameNumber > 0) this._strContentAll += Convert.ToChar(2); //STX (start of text) = ASCII CHAR(2) 
                        this._strContent = doc.body.outerHTML;
                        this._strContentAll += this._strContent;
                        _iFrameNumber++;
                    }
                    return;
                }
                else
                {
                    this._strContent = "";
                    this._strContentAll = "";
                    _iFrameNumber = -1;
                    _strLocationURL = "";
                    _strTitle = "";
                }
            }
        }

        #endregion

    }
}

 Conclusion

Après la compilation on obtient un fichier dll. On peut continuer le développement de cette source pour acquérir toutes les donées du contenu HTML de la page web.

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
  •   ClassLibrary1
  • ClassLibrary1.dllTélécharger ce fichier [Réservé aux membres club]6 656 octets
  • Interop.SHDocVw.dllTélécharger ce fichier [Réservé aux membres club]126 976 octets

Télécharger le zip


 Sources de la même categorie

Source avec Zip Source avec une capture Source .NET (Dotnet) ORIONBANQUE par toutphp
Source avec Zip Source avec une capture Source .NET (Dotnet) ORIONAPPLICATION par toutphp
Source avec Zip SOCKET CONNEXION CLIENT & SERVEUR par ziedto83
Source avec Zip Source .NET (Dotnet) FFMPEG.NET : WRAPPER .NET DE FFMPEG par MasterShadows
Source avec Zip Source .NET (Dotnet) ATTACHER, CRÉER ET SAUVEGARDER UNE BASE DE DONNÉES SQL SERVE... par Alvepinai

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture Source .NET (Dotnet) FROSTSHELL 1 (SHELL REPLACEMENT) par Elxior
Source avec Zip Source .NET (Dotnet) MODÈLE POUR CRÉER VOS BANDEAUX INTERNET EXPLORER/WINDOWS EXP... par ShareVB
Source avec Zip Source avec une capture Source .NET (Dotnet) ZEXPLORER (EXPLORATEUR WINDOWS) par Zap
Source avec une capture Source .NET (Dotnet) LOGICIEL GOOGLE POUR INTERNET EXPLORER par stailer
Source avec Zip Source avec une capture Source .NET (Dotnet) MYSQL EXPLOREUR par APWEB

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Explorer [ par thebigboss ] Salut,Je souhaite faire une form contenant deux parties. En haut, un composant du type explorateur de fichiers et en bas un datagrid. (le tout donne u Récupérer l'URL en cours dans Internet Explorer... [ par coolfire ] Bonjour à tous !J'ai besoin d'un peu d'aide:J'ai un programme C# qui recupère la liste des processus.pas de problème.Parmi ces processus, mon programm Internet Explorer en Automatique [ par Doombringer ] Bon, je voulais faire un programme qui change automatiquement les pages internet de Internet Explorer tous les X secondes. Bon, j'ai réussi quelque pe Récuperer les adresse d'internet explorer [ par oberown ] Je voudrais faire une programme, qui permet lorsqu'il est activé de récuperer les adresses web des sites vu sur IE en temps réel.Par exemple on est su Evenements venant d'internet explorer [ par ascj ] Bonjour,je voudrai savoir si c'est possible de verouiller les boutons "Précédent" et/ou "Suivant" d'internet explorer ou, encore mieux, de pouvoir réc Internet Explorer et Proxy [ par jeunepadawan ] Bonjour à tous,Je voudrais savoir si on peut spécifier un proxy ainsi que son port quand on lance une page Internet Explorer à l'aide du code suivant L'assembly référencé 'Interop.SHDocVw' n'a pas un nom fort... [ par seup ] Bonjour !Je développe actuellement une barre d'outil type GoogleBar et je reste bloqué par l'erreur suivante : La génération de l'assembly a échoué -- comment activer la propriété "autopostback" aux boutons de navigation d'internet explorer [ par mohamed_bn ] med belhassendans mon application web j'utilise les variables "Session" pour avoir des informations sur le client en cours. j'aime bien savoir comment utilisation a distance de internet explorer [ par rvmartin ] SalutJe debute en C#.Je voudrais savoir comment&nbsp;recuperer les composants (boutons, zone de texte)&nbsp;d'une page HTML. La page se trouve dans&nb Ajouter un item au contextemenustrip de l'internet explorer [ par Fildomen ] Salutje veux ajouter une nouvelle commande dans le clique droit dans l'internet explorer, tt comme f&#233; windows media player et winrar dans windows


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,825 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales