Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8125835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:56:55+00:00 2026-06-06T06:56:55+00:00

I’d like to create a COM object in C#, and use it via IDispatch

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:56:57+00:00Added an answer on June 6, 2026 at 6:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.