none
Call Internet Explorer as COM object RRS feed

  • Question

  • Hello.

    Is there any posibility to create the Internet Explorer COM object and pass to it html string (not a Url) from C# o other language that supports COM technology?

    Wednesday, September 16, 2020 4:53 PM

Answers

  • You can do for example =>

            // Add reference to "Microsoft Internet Controls"
            // Add reference to "Microsoft HTML Object Library"


    SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
    IE.Visible = true;
    IE.Navigate("about:blank");
    // should be done in DocumentComplete
    mshtml.IHTMLDocument2 htmlDoc2 = IE.Document as mshtml.IHTMLDocument2;
    htmlDoc2.body.outerHTML = "<meta http-equiv='X-UA-Compatible' content='IE=edge'> <iframe width='854' height='480' src='https://www.youtube.com/embed/QwqmJilXxJY?autoplay=1&muted=1' frameborder='0' allow='autoplay'></iframe>";

    • Marked as answer by aka Pipo Thursday, September 17, 2020 2:21 PM
    Wednesday, September 16, 2020 6:18 PM

All replies

  • You can do for example =>

            // Add reference to "Microsoft Internet Controls"
            // Add reference to "Microsoft HTML Object Library"


    SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
    IE.Visible = true;
    IE.Navigate("about:blank");
    // should be done in DocumentComplete
    mshtml.IHTMLDocument2 htmlDoc2 = IE.Document as mshtml.IHTMLDocument2;
    htmlDoc2.body.outerHTML = "<meta http-equiv='X-UA-Compatible' content='IE=edge'> <iframe width='854' height='480' src='https://www.youtube.com/embed/QwqmJilXxJY?autoplay=1&muted=1' frameborder='0' allow='autoplay'></iframe>";

    • Marked as answer by aka Pipo Thursday, September 17, 2020 2:21 PM
    Wednesday, September 16, 2020 6:18 PM
  • Why? What are you trying to do. COM would be overkill if you need something as simple as HTML parsing. If you actually want to get a website to load then there is already support for hosting the browser without using COM directly.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, September 16, 2020 7:09 PM
    Moderator
  • Thank you! It works well.

    Thursday, September 17, 2020 2:22 PM
  • Thank you for your reply.

    A HTML text is stored in a database. I do not need a parsing, i need just to display correct HTML.

    Of course, i can save the text to file and open it with default system browser.


    • Edited by aka Pipo Thursday, September 17, 2020 2:26 PM
    Thursday, September 17, 2020 2:25 PM
  • Dear Castorix31, another one question please.

    Can i embed an iexplorer ActiveX into my form without opening separate iexplorer window?


    • Edited by aka Pipo Thursday, September 17, 2020 2:31 PM
    Thursday, September 17, 2020 2:30 PM
  • Then use WebBrowser if you have an winforms or WPF app. Or the newer WebView if possible. That hosts the browser in your app directly. No need for COM.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, September 17, 2020 2:48 PM
    Moderator
  • Yes, as CoolDadTx said,

    you can just use a WebBrowser control and WebBrowser.DocumentText

    to display the HTML text



    • Edited by Castorix31 Thursday, September 17, 2020 3:14 PM
    Thursday, September 17, 2020 3:12 PM
  • CoolDadTx, thank you. But I do not use Windows Forms or WPF.

    There is an application that has own window class. It allows embed ActiveX objects within it. That is why i asked about COM technology only.

    6 hours 46 minutes ago
  • So you're writing a plugin for some other app? You should really clarify exactly what you're trying to do when you post a question like this. Writing plugins for other apps (especially unmanaged apps) is pretty important to such discussions.

    This is especially true given that C# isn't the best choice if you need a native ActiveX control. To get C# to work the unmanaged app would need to host the CLR runtime which means you have to use the same version they are using. Things can get complicated.

    Personally I would recommend that you create a Winforms control that supports ActiveX (documentation tells you how to do this). Then host the WebBrowser control inside that. This gives you a control that you'll have to register with COM but can then be hosted in other apps. It's a lot of work but that is the cost of working with ActiveX controls.


    Michael Taylor http://www.michaeltaylorp3.net

    5 hours 13 minutes ago
    Moderator
  • Thank you. I have been thinking about it. To create a Winform and put the WebBrowser inside it.

    4 hours 50 minutes ago
  • You can also use ATL to embed ActiveX controls, but I don't know if it will work in your case...

    A test =>

        public partial class Form1 : Form
        {
            [DllImport("Atl.dll", SetLastError = true)]
            public static extern int AtlAxGetHost(IntPtr h, [MarshalAs(UnmanagedType.IUnknown)] out object pp);
    
            [DllImport("Atl.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
            public static extern int AtlAxCreateControl(string lpszName, IntPtr hWnd, IntPtr pStream, [MarshalAs(UnmanagedType.IUnknown)] out object ppUnkContainer);
    
            [DllImport("Atl.dll", SetLastError = true)]
            public static extern bool AtlAxWinInit();
    
            [DllImport("Atl.dll", SetLastError = true)]
            public static extern int AtlAxGetControl(IntPtr h, [MarshalAs(UnmanagedType.IUnknown)] out object pp);
    
            public const int WS_OVERLAPPED = 0x0;
            public const int WS_BORDER = 0x00800000;
            public const int WS_POPUP = unchecked((int)0x80000000L);
            public const int WS_CHILD = 0x40000000;
            public const int WS_MINIMIZE = 0x20000000;
            public const int WS_VISIBLE = 0x10000000;
            public const int WS_DISABLED = 0x8000000;
    
            [DllImport("User32.dll", SetLastError = true)]
            public static extern IntPtr CreateWindowEx(int dwExStyle, string lpClassName, string lpWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
    
            private IntPtr hWndContainer = IntPtr.Zero;
            SHDocVw.IWebBrowser2 pWebBrowser2 = null;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.ClientSize = new System.Drawing.Size(800, 600);
                CenterToScreen();
                if (AtlAxWinInit())
                {
                    hWndContainer = CreateWindowEx(0, "AtlAxWin", "about:blank", WS_VISIBLE | WS_CHILD | WS_BORDER, 10, 10, ClientSize.Width/2, ClientSize.Height/2, this.Handle, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                    object oWebBrowser2;
                    int hr = AtlAxGetControl(hWndContainer, out oWebBrowser2);
                    pWebBrowser2 = (SHDocVw.IWebBrowser2)oWebBrowser2;
    
                    ((SHDocVw.DWebBrowserEvents2_Event)pWebBrowser2).NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(axWebBrowser1_NavigateComplete2);
                 }
            }
    
            public void axWebBrowser1_NavigateComplete2(object pDisp, ref object URL)
            { 
                mshtml.IHTMLDocument2 pHtmlDoc2 = pWebBrowser2.Document as mshtml.IHTMLDocument2;
                // pHtmlDoc2.body.innerHTML = "<body> <p style='color:#FF0000';>This is a red paragraph text</p> </body>";
                string sHTML = @"<!DOCTYPE html>
    <html>
    <head>
    	<title>HTML Style Example</title>
    </head>
    <body>
    
    <p style='color:red;'>This is HTML Style Example (red colored)</p>
    <p style='color:green;'>This is HTML Style Example (green colored)</p>
    <p style='color:blue;'>This is HTML Style Example (blue colored)</p>
    <p style='color:purple;'>This is HTML Style Example (purple colored)</p>
    <p style='color:cyan;'>This is HTML Style Example (cyan colored)</p>
    <p style='color:yellow;'>This is HTML Style Example (yellow colored)</p>
    
    </body>
    </html>";
                pHtmlDoc2.body.innerHTML = sHTML;
            }      
        }

    3 hours 51 minutes ago