locked
JScript calling COM object implemented in C# RRS feed

  • Question

  • I've got some JScript, as follows:

    var x = new ActiveXObject("ProgID.Of.X");  
    var y = x.GetY();  
     
    alert(y.Z);  
     

    class X is implemented in C#, as follows:

    1     [Guid("B12CCB95-99E8-4A93-BB78-9E8701012708"), ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]  
    2     public interface IX  
    3     {  
    4         IY GetY();  
    5     }  
    6  
    7     [Guid("9E5F7FDD-CB7C-4287-A84F-0843AA67B56D"), ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]  
    8     public interface IY  
    9     {  
    10         int Z { get; }  
    11     }  
    12  
    13     [Guid("80802159-2AB4-442F-81DE-6CB7F172D3B2"), ComVisible(true), ClassInterface(ClassInterfaceType.None), ProgId("ProgID.Of.X")]  
    14     public class ImplementsX : IX  
    15     {  
    16         public IY GetY()  
    17         {  
    18             return new ImplementsY();  
    19         }  
    20     }  
    21  
    22     [ComVisible(true), ClassInterface(ClassInterfaceType.None)]  
    23     internal class ImplementsY : IY  
    24     {  
    25         public int Z  
    26         {  
    27             get { return 42; }  
    28         }  
    29     }  
    30  

    If I run the JScript given, I get "Microsoft JScript runtime error: 'y.Z' is null or not an object"

    If I call the same thing from VBScript:

    Dim x: Set x = CreateObject("ProgID.Of.X")  
    Dim y: Set y = x.GetY  
     
    MsgBox y.Z  
     

    ...it works fine. What magic am I missing in the C# definition of my COM object that prevents it from being used from JScript?

    • Edited by Roger Lipscombe Friday, June 13, 2008 3:25 PM Correct typo
    • Moved by Peter Ritchie Thursday, June 19, 2008 8:17 PM JScript question
    Friday, June 13, 2008 3:24 PM

Answers

All replies

  • Hi Roger,

    Sorry, I am not familiar with Jscript and VBScript; however, I tried to consume the C# COM in a C++ native console application, and I found the cause.

    If the COM client and COM server file are not in a same folder, the COM object cannot be created at all, but I put the COM files (*.dll and *.tlb) and the C++ console application in a same folder, such as “C:\test”, everything is OK.

    I guess that is the cause, try to put these files (COM client and server) in a folder to fix this issue.

    Regards,

    Xun


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, June 17, 2008 11:53 AM
  • If you add the /codebase switch to RegAsm.exe, then it works, no matter where the C# DLL is stored. I used RegAsm /tlb, and created a C++ client that used #import to consume that type library, and it worked fine.

    It works fine from VBScript, but not from JScript. So, I'm wondering: what's special about my C# COM object that means that it works fine from C++ and VBScript, but not JScript?
    Tuesday, June 17, 2008 1:30 PM
  • Hi Roger,

    Can you use the JScript to consume the other COM component?

    Regards,
    Xun
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, June 18, 2008 2:00 AM
  • "Other COM component"? The JScript can happily consume any method on X (provided I follow the rules for not using [out] parameters, for example). The one thing that it can't do is consume another interface returned from the first object.

    Given that JScript is capable of consuming (e.g.) Excel, which has interfaces returning interfaces, I figure that it ought to work, and that it's something about how C# is exposing the interface that's causing it to fail.
    Friday, June 20, 2008 7:26 AM
  • This thread was moved into the Off Topic forum. I can move it into a specific forum for you if you want.

    Thanks!


    Ed Price (a.k.a User Ed), SQL Server Experience Program Manager (Blog, Twitter, Wiki)

    Friday, May 11, 2012 5:42 AM