Create COM/ActiveXObject in C#, use from JScript, with simple event
Asked Answered
H

2

9

I'd like to create a COM object in C#, and use it via IDispatch from JScript. That part is pretty simple.

I also want to implement simple callbacks on the COM object, similar to the event exposed by the XmlHttpRequest object that is usable in a browser. That model allows Javascript to attach event handlers like this:

var xmlhttp = new ActiveXObject("MSXML.XMLHTTP"); 
xmlhttp.onReadyStateChange = function() {
  ...
};

I want my client-side JScript code to look like this:

var myObject = new ActiveXObject("MyObject.ProgId");
myObject.onMyCustomEvent = function(..args here..) { 
   ...
};

What does the C# code look like? I'd like the general case - I'd like to be able to pass arguments back to the Javascript fn.


I've seen How can I make an ActiveX control written with C# raise events in JavaScript when clicked? , but the answers there look really complicated to implement, and complicated to use.


From this article, it seems that XMLHttpRequest events are not COM events. The onreadystatechange is a property of type IDispatch. When script clients set that property to a function, JScript marshals it as an IDispatch object.

The only problem that remains is then to invoke the IDispatch from C#.

Hemline answered 24/6, 2012 at 5:16 Comment(0)
H
15

Since it's COM, start by defining an interface. Let's keep it simple.

[Guid("a5ee0756-0cbb-4cf1-9a9c-509407d5eed6")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IGreet
{
    [DispId(1)]
    string Hello(string name);

    [DispId(2)]
    Object onHello { get; set; }
}

Then, the implementation:

[ProgId("Cheeso.Greet")]
[ComVisible(true)]
[Guid("bebcfaff-d2f4-4447-ac9f-91bf63b770d8")]
[ClassInterface(ClassInterfaceType.None)]
public partial class Greet : IGreet
{
    public Object onHello { get; set; }

    public String Hello(string name)
    {
        var r = FireEvent();
        return "Why, Hello, " + name + "!!!" + r;
    }
}

The main trick is the FireEvent method. This worked for me.

    private string FireEvent()
    {
        if (onHello == null) return " (N/A)";
        onHello
            .GetType()
            .InvokeMember
            ("",
             BindingFlags.InvokeMethod,
             null,
             onHello,
             new object [] {});

        return "ok";
    }

Compile that all together, register it with regasm:

%NET64%\regasm.exe Cheeso.Greet.dll /register /codebase

...And then use it from JScript like this:

var greet = new ActiveXObject("Cheeso.Greet"), response;
greet.onHello = function() {
    WScript.Echo("onHello (Javascript) invoked.");
};
response = greet.Hello("Fred");
WScript.Echo("response: " + response);

It works.

You can also call it from VBScript:

Sub onHello ()
    WScript.Echo("onHello (VBScript) invoked.")
End Sub

Dim greet
Set greet = WScript.CreateObject("Cheeso.Greet")
greet.onHello = GetRef("onHello")
Dim response
response = greet.Hello("Louise")
WScript.Echo("response: " &  response)

To pass parameters back from C# to JScript with this approach, I think objects need to be IDispatch, but of course you can send back simple values marshaled as string, int, and so on which are marshaled as you would expect.

For example, modify the C# code to send back a reference to itself, and the number 42.

        onHello
            .GetType()
            .InvokeMember
            ("",
             BindingFlags.InvokeMethod,
             null,
             onHello,
             new object [] { this, 42 });

Then, you can get that in jscript like so:

greet.onHello = function(arg, num) {
    WScript.Echo("onHello (Javascript) invoked.");
    WScript.Echo("  num = " + num + "  stat=" + arg.status);
};

Or in VBScript like so:

Sub onHello (obj, num)
    WScript.Echo("onHello (VBScript) invoked. status=" & obj.status )
    WScript.Echo("  num= " & num)
End Sub

NB: You can define your jscript event handler function to accept fewer args than are sent by the C# object when invoking the "event". In my experience you need to design the event handler in VBScript to explicitly accept the correct number of arguments.

Hemline answered 24/6, 2012 at 5:22 Comment(5)
This is a late bound call, not an event. Use the [ComSourceInterfaces] attribute to expose .NET events.Reger
Exactly. It's not a COM event at all, but it sure is simpler to build and to use than wiring up COM events for this restricted scenario - one object and one user of that object. And in fact, though it is not a COM event, programmers correctly understand it to satisfy their needs for "an event" for a scriptable object. This is why XMLHttpRequest uses the approach, I suppose, for onreadystatechange. That too, is "not an event" but it certainly is understood to be an event by millions of devs. COM's specific definition of the term "event" is irrelevant for some purposes.Hemline
When creating the ActiveXObject in JScript, does the "Cheeso.Greet" come from the ProjId, the DLL name, Namespace.ClassName or somewhere else altogether? No matter what I try I seem to always get "Automation server can't create object".Woden
@SandyGifford - ProgId. Debugging COM activations is a large pain. In my experience, these days, it's usually a security issue.Hemline
I agree entirely, but we have a requirement to interact Excel with a fashion that's just not possible with the JScript.NET that we need to start with (you can't get a current instance of the application, you can only create a new one). Thanks, though. This was exactly what I needed to know.Woden
D
2

Wow, wow! May be easy?

using System;
using System.EnterpriseServices;

[assembly: ApplicationName("Calculator")]
[assembly: ApplicationActivation(ActivationOption.Library)]
public class Calculator : ServicedComponent
{
    public int Add(int x, int y){ return (x + y); }
}

then use these build command

 sn -k Calculator.snk           
 csc /t:library Calculator.cs
 regsvcs Calculator.dll

On jscript (wsh):

c = new ActiveXObject("Calculator");
WScript.Echo(typeof(c));  // output: object
WScript.Echo(c.Add(4,1)); // output: 5

source: msdn

Enjoy!

Dollfuss answered 15/2, 2015 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.