简体   繁体   中英

Can't fire an event from ActiveX to client with .Net(C#)

I'm writing an ActiveX Object with .Net, and need the object can fire events to client page with ie11, but now when I fire the event, NotImplementedException will be thrown.

The code of the page is:

<!DOCTYPE html>
<script language="javascript" for="RegisterTestClass" event="OnSetString()" type="text/javascript">
    alert("Event is raised");
</script>
<object id="RegisterTestClass" name="RegisterTestClass" style="width:0px;height:0px;display:none" classid="CLSID:336A359B-EEC4-47EB-AA8D-B9DD0D4D4B5B"></object>
<script type="text/javascript">
    function SetOCXString(varStr)
    {
        alert("SetOCXString");
        RegisterTestClass.SetString(varStr);
    }
</script>
<html>
<head>
    <meta charset="utf-8" />
    <title>Test OCX</title>
</head>
<body>
    <button onclick="SetOCXString('Hello World!');"> Click me!</button>
</body>
</html>

And the ActiveX file is:

[Guid("7279003B-0619-4E19-95BC-F22BD29CC950")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITestEvent
{
    void OnSetString();
}

[ComVisible(false)]
public delegate void EventHandlerTest();

[Guid("336A359B-EEC4-47EB-AA8D-B9DD0D4D4B5B")]
[ComSourceInterfaces(typeof(ITestEvent))]
public class RegisterTestClass
{
    public event EventHandlerTest OnSetString;

    public void SetString(string str)
    {
        MessageBox.Show("SetString:" + str);
        if (null != this.OnSetString)
        {
            try
            {
                MessageBox.Show("Event is not null:" + this.OnSetString.Target.ToString() + "," + this.OnSetString.Method.Name);
                this.OnSetString();
                MessageBox.Show("Event raised");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.StackTrace, e.Source + e.Message);
            }
        }
        else
        {
            MessageBox.Show("Event is null");
        }
    }
}

Can anyone help me? Thx

I have just solved the problem.

Just add a "DispId" for the ITestEvent.OnSetString. So the coding is:

[Guid("7279003B-0619-4E19-95BC-F22BD29CC950")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITestEvent
{
    [DispId(0x0001)]
    void OnSetString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM