<p>I admit I know enough about COM and IE architecture only to be dangerous.  I have a working C# .NET ActiveX control similar to this:</p>

<pre><code>using System;
using System.Runtime.InteropServices;
using BrowseUI;
using mshtml;
using SHDocVw;
using Microsoft.Win32;

    namespace CTI
    {
        public interface CTIActiveXInterface
        {
            [DispId(1)]
            string GetMsg();
        }

        [ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)]
        public class CTIActiveX : CTIActiveXInterface
        {

            /*** Where can I get a reference to SHDocVw.WebBrowser? *****/
            SHDocVw.WebBrowser browser;

            public string GetMsg()
            {
                return "foo";
            }
        }
    }
</code></pre>

<p>I registered and created a type library using regasm:</p>

<pre><code>regasm CTIActiveX.dll /tlb:CTIActiveXNet.dll /codebase
</code></pre>

<p>And can successfully instantiate this in javascript:</p>

<pre><code>var CTIAX = new ActiveXObject("CTI.CTIActiveX");
alert(CTIAX.GetMsg());  
</code></pre>

<p>How can I get a reference to the client site (browser window) within CTIActiveX?  I have done this in a BHO by implementing IObjectWithSite, but I don't think this is the correct approach for an ActiveX control.  If I implement any interface (I mean COM interface like IObjectWithSite) on CTIActiveX when I try to instantiate in Javascript I get an error that the object does not support automation.</p>

[{'answer_id': 75086, 'author': 'jlew', 'author_id': 7450, 'author_profile': 'https://Stackoverflow.com/users/7450', 'pm_score': 2, 'selected': False, 'text': '<p>First, your interface needs ComVisible(true) in order to be seen by the calling script (this is probably causing the error).  </p>\n\n<p>Second, add a .NETreference in your project to "Microsoft.mshtml".  This will import the COM interfaces for various IE-related things (windows, HTML documents, etc.)</p>\n\n<p>Then, you need to add a property of type IHtmlDocument2 to your interface:</p>\n\n<pre><code>IHtmlDocument2 Document { set; }\n</code></pre>\n\n<p>...implement it in your class:</p>\n\n<pre><code>public IHtmlDocument2 Document\n{\n  set { _doc = value;}\n}\n</code></pre>\n\n<p>...call it from script</p>\n\n<pre><code>CTIAX.Document = document; \n</code></pre>\n\n<p>...once you have stored a reference to the document, you can use it at will to get to the window, other frames, or any part of the HTML DOM that you wish.</p>\n'}, {'answer_id': 75982, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>I have found a workable solution.  It's not ideal because it relies on matching the location URL of the IE window to get the correct container, but it does work.  In my case I'm using a special value on the query string to make sure I get the right window.</p>\n\n<p>This gets a reference to SHDocVw.InternetExplorer, which exposes the same GetProperty and PutProperty that SHDocVw.WebBrowser does:</p>\n\n<pre><code>private InternetExplorer GetIEWindow(string url)\n{\n    SHDocVw.ShellWindowsClass sh = new ShellWindowsClass();\n    InternetExplorer IE;\n\n    for (int i = 1; i &lt;= sh.Count; i++)\n    {\n        IE = (InternetExplorer)sh.Item(i);\n        if (IE != null)\n        {\n            if (IE.LocationURL.Contains(url))\n            {\n                return IE;\n            }\n        }\n    }\n\n    return null;\n}\n</code></pre>\n"}, {'answer_id': 10417056, 'author': 'MarcoM', 'author_id': 1307467, 'author_profile': 'https://Stackoverflow.com/users/1307467', 'pm_score': 0, 'selected': False, 'text': '<p>There a simple and cleaner way to do it:</p>\n\n<pre><code>public void GetBrowser()\n        {\n\n            ShellWindows m_IEFoundBrowsers = new ShellWindows();\n\n            foreach (InternetExplorer Browser in m_IEFoundBrowsers)\n            {\n                webBrowser = (SHDocVw.WebBrowser) Browser;\n                 // do what you want ...\n            }\n\n        }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/72048', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']