0
votes

I have checked on stackoverflow (and what seems like everywhere else). I would like to get a COM solution working so that a jscript file can be written as

var T = new ActiveXObject("MySimulator.World"); 
T.MyMethod();

It would be executed at the command prompt by

cscript mytest.js

In this case, I get the error "Automation server can't create object".

In C#, I have followed various suggestions, with the latest interface being:

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83B")]
public interface IComMyReaderInterface
{
    void MyFunction();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None), Guid("0D53A3E8-E51A-49C7-944E-E72A2064F9DD"), ProgId("MySimulator.World")]
[ComDefaultInterface(typeof(IComMyReaderInterface))]
public class MyReader : IComMyReaderInterface
{
    public MyReader()
    {
         ...
    }

    public void MyFunction()
    {
         ...
    }
 ...
}

Thanks and just let me know if more information is needed.

1
Several steps to get this working, they've been covered well in other questions already. Nobody got the name this completely wrong however. Assuming the namespace name is actually MySimulator, the ProgID is "MySimulator.MyReader" and the method to call is MyFunction, not MyMethod. - Hans Passant
Hans - thanks for looking. I had it correct prior - trying different things. Changing it back to MySimulator.MyReader does not fix it. I have looked at and tried a dozen or so potential solutions, but nothing working yet. I am wondering, what is key/critical so that jscript will see it so that the object gets created via jscript? Thanks again. - Buck

1 Answers

2
votes

I'd assume the following. Your development environment is probably a 64-bit OS and your C# DLL project is probably configured to compile with Any CPU as Platform Target. Read on if that's the case.

Choose either x86 or x64 and compile the project. If you go with x86, then register your assembly with the 32-bit version of RegAsm.exe:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe /codebase assembly.dll

Then run your JavaScript test with the 32-bit version of cscript.exe:

C:\Windows\SysWOW64\cscript.exe mytest.js

If you go with x64, that would be:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe /codebase assembly.dll C:\Windows\System32\cscript.exe mytest.js

[EDITED] The following code has been verified to work using the above instructions.

C#:

using System;
using System.Runtime.InteropServices;

namespace ComLibrary
{
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual),
        Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83B")]
    public interface IComMyReaderInterface
    {
        void MyFunction();
    }

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None), 
        Guid("0D53A3E8-E51A-49C7-944E-E72A2064F9DD"), 
        ProgId("MySimulator.World")]
    [ComDefaultInterface(typeof(IComMyReaderInterface))]
    public class MyReader : IComMyReaderInterface
    {
        public MyReader()
        {
        }

        public void MyFunction()
        {
            Console.WriteLine("MyFunction called");
        }
    }
}

JavaScript (mytest.js):

var T = new ActiveXObject("MySimulator.World"); 
T.MyFunction();

Output:

MyFunction called