C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult.
Go Back  Xtreme Visual Basic Talk > > > > C# Internet Explorer DOM using ObjectFromLresult.


Reply
 
Thread Tools Display Modes
  #1  
Old 04-12-2008, 05:57 PM
Csharp's Avatar
CsharpC# Internet Explorer DOM using ObjectFromLresult. Csharp is offline
Senior Contributor

* Expert *
 
Join Date: Jul 2003
Location: Ashby, Leicestershire.
Posts: 967
Default C# Internet Explorer DOM using ObjectFromLresult.


Howdo, I've been working on this for a couple of days, I did a VB.NET example of this on a forum a few years ago, but C# presented me with a new challenge & it took a while to figure out.

The Idea is to get hold of an open instance of IE ( IE7 in this case ) & grab hold of it's IHTMLDocument2 Interface, giving the ability to manipulate that external Internet Explorer window / grab it's url address, it's html, make it navigate elsewhere, etc...

when this code is added to a project, you must make a reference to Microsoft.mshtml
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using mshtml;

namespace HookBrowser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region API CALLS

        [DllImport("user32.dll", EntryPoint = "GetClassNameA")]
        public static extern int GetClassName(IntPtr hwnd, StringBuilder lpClassName, int nMaxCount);

        /*delegate to handle EnumChildWindows*/
        public delegate int EnumProc(IntPtr hWnd, ref IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(IntPtr hWndParent, EnumProc lpEnumFunc, ref  IntPtr lParam);
        [DllImport("user32.dll", EntryPoint = "RegisterWindowMessageA")]
        public static extern int RegisterWindowMessage(string lpString);
        [DllImport("user32.dll", EntryPoint = "SendMessageTimeoutA")]
        public static extern int SendMessageTimeout(IntPtr hwnd, int msg, int wParam, int lParam, int fuFlags, int uTimeout, out int lpdwResult);
        [DllImport("OLEACC.dll")]
        public static extern int ObjectFromLresult(int lResult, ref Guid riid, int wParam, ref IHTMLDocument2 ppvObject);
        public const int SMTO_ABORTIFHUNG = 0x2;
        public Guid IID_IHTMLDocument = new Guid("626FC520-A41E-11CF-A731-00A0C9082637");

        #endregion

        public IHTMLDocument2 document;

        private void button1_Click(object sender, EventArgs e)
        {
            document = documentFromDOM();
            /// check that we have hold of the IHTMLDocument2...
            if (!(bool)(document == null))
            {
                this.Text = document.url;
            }
        }

        private IHTMLDocument2 documentFromDOM()
        {
            Process[] processes = Process.GetProcessesByName("iexplore");
            if (processes.Length > 0)
            {
                IntPtr hWnd = processes[0].MainWindowHandle;
                int lngMsg = 0;
                int lRes;

                EnumProc proc = new EnumProc(EnumWindows);
                EnumChildWindows(hWnd, proc, ref hWnd);
                if (!hWnd.Equals(IntPtr.Zero))
                {
                    lngMsg = RegisterWindowMessage("WM_HTML_GETOBJECT");
                    if (lngMsg != 0)
                    {
                        SendMessageTimeout(hWnd, lngMsg, 0, 0, SMTO_ABORTIFHUNG, 1000, out lRes);
                        if (!(bool)(lRes == 0))
                        {
                            int hr = ObjectFromLresult(lRes, ref IID_IHTMLDocument, 0, ref document);
                            if ((bool)(document == null))
                            {
                                MessageBox.Show("No IHTMLDocument Found!", "Warning");
                            }
                        }
                    }
                }
            }
            return document;
        }

        private int EnumWindows(IntPtr hWnd, ref IntPtr lParam)
        {
            int retVal = 1;
            StringBuilder classname = new StringBuilder(128);
            GetClassName(hWnd, classname, classname.Capacity);
            /// check if the instance we have found is Internet Explorer_Server
            if ((bool)(string.Compare(classname.ToString(), "Internet Explorer_Server") == 0))
            {
                lParam = hWnd;
                retVal = 0;
            }
            return retVal;
        }
    }
}
__________________
~~ please don't PM me regarding code, I only reply to personnal messages ~~
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult. C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult.
 
C# Internet Explorer DOM using ObjectFromLresult.
C# Internet Explorer DOM using ObjectFromLresult.
 
-->