6

我承认我对 COM 和 IE 体系结构了解得足够多,只是很危险。我有一个与此类似的工作 C# .NET ActiveX 控件:

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";
            }
        }
    }

我使用 regasm 注册并创建了一个类型库:

regasm CTIActiveX.dll /tlb:CTIActiveXNet.dll /codebase

并且可以在 javascript 中成功实例化它:

var CTIAX = new ActiveXObject("CTI.CTIActiveX");
alert(CTIAX.GetMsg());  

如何在 CTIActiveX 中获取对客户端站点(浏览器窗口)的引用?我通过实现 IObjectWithSite 在 BHO 中完成了这项工作,但我认为这不是 ActiveX 控件的正确方法。如果我在尝试用 Javascript 实例化时在 CTIActiveX 上实现任何接口(我的意思是像 IObjectWithSite 这样的 COM 接口),我会收到一个错误,即该对象不支持自动化。

4

3 回答 3

4

First, your interface needs ComVisible(true) in order to be seen by the calling script (this is probably causing the error).

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.)

Then, you need to add a property of type IHtmlDocument2 to your interface:

IHtmlDocument2 Document { set; }

...implement it in your class:

public IHtmlDocument2 Document
{
  set { _doc = value;}
}

...call it from script

CTIAX.Document = document; 

...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.

于 2008-09-16T17:55:48.203 回答
1

我找到了一个可行的解决方案。这并不理想,因为它依赖于匹配 IE 窗口的位置 URL 来获取正确的容器,但它确实有效。在我的情况下,我在查询字符串上使用了一个特殊值来确保我得到正确的窗口。

这获得了对 SHDocVw.InternetExplorer 的引用,它公开了与 SHDocVw.WebBrowser 相同的 GetProperty 和 PutProperty:

private InternetExplorer GetIEWindow(string url)
{
    SHDocVw.ShellWindowsClass sh = new ShellWindowsClass();
    InternetExplorer IE;

    for (int i = 1; i <= sh.Count; i++)
    {
        IE = (InternetExplorer)sh.Item(i);
        if (IE != null)
        {
            if (IE.LocationURL.Contains(url))
            {
                return IE;
            }
        }
    }

    return null;
}
于 2008-09-16T19:25:03.077 回答
0

有一种简单而干净的方法来做到这一点:

public void GetBrowser()
        {

            ShellWindows m_IEFoundBrowsers = new ShellWindows();

            foreach (InternetExplorer Browser in m_IEFoundBrowsers)
            {
                webBrowser = (SHDocVw.WebBrowser) Browser;
                 // do what you want ...
            }

        }
于 2012-05-02T15:38:55.737 回答