通过Javascript管理Activex .NET事件

时间:2012-04-07 07:40:20

标签: c# javascript com activex

我写了一个简单的COM .NET dll(Activex),以便通过我的Html页面通过javascript调用某些方法并管理一些事件。

好吧,在阅读了很多关于现在的主题后,我无法通过javascript管理任何事件。 我确实尝试了许多样本,语法很多,但不是为了我的目的。

我的.NET COM事件声明如下:http://www.codeproject.com/Articles/24089/Create-ActiveX-in-NET-Step-by-Step

我也读过这篇文章(http://stackoverflow.com/questions/150814/how-to-handle-an-activex-event-in-javascript),但我不知道如何扩展我的ActivexControl以一种简单的方式建议。

1 个答案:

答案 0 :(得分:0)

以下是关于我的activex的代码段:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using System.Threading;

namespace AxControls
{

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("61327C9D-EFC5-42B7-BA0D-4A8648797003")]
    public interface IHelloWorld
    {
        string GetText();
    }

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [Guid("61327C9D-EFC5-42B7-BA0D-4A8648797003")]
    public interface IMyEvt
    {
        [DispId(1)]
        void OnMyEvt(int prm);
    }

    [ComImport()]
    [Guid("13AD0E8B-BA3F-4CDE-A7D4-8A311EC1766B")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IObjectSafety
    {
        [PreserveSig()]
        int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions);

        [PreserveSig()]
        int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions);
    }




    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [Guid("32E6AD29-DD5F-46EA-A4D1-BD2F1E3EE064")]
    [ProgId("AxControls.HelloWorld")]
    [ComSourceInterfaces(typeof(IMyEvt))]
    public class HelloWorld : UserControl, IHelloWorld, IObjectSafety
    {
        [ComVisible(false)]
        public delegate void OnMyEvtDelgate(int prm);

        [DispId(1)]
        public event OnMyEvtDelgate OnMyEvt;

        public HelloWorld()
        {
            this.OnMyEvt += new OnMyEvtDelgate(Dummy);
        }

        void Dummy(int param) { }

        #region IHelloWorld Members

        public string GetText()
        {
            if (this.OnMyEvt != null) OnMyEvt(10);
            return "Hello ActiveX World!";
        }

        #endregion

        #region IObjectSafety Members

        public enum ObjectSafetyOptions
        {
            INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
            INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
            INTERFACE_USES_DISPEX = 0x00000004,
            INTERFACE_USES_SECURITY_MANAGER = 0x00000008
        };

        public int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
        {
            ObjectSafetyOptions m_options = ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_CALLER | ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_DATA;
            pdwSupportedOptions = (int)m_options;
            pdwEnabledOptions = (int)m_options;
            return 0;
        }

        public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
        {
            return 0;
        }

        #endregion

    }

}