【问题标题】:Getting C# ActiveX/COM Library to Work through JScript让 C# ActiveX/COM 库通过 JScript 工作
【发布时间】:2014-01-21 18:41:19
【问题描述】:

我检查了 stackoverflow(以及其他地方的情况)。我想让一个 COM 解决方案工作,以便可以将 jscript 文件编写为

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

它将由

在命令提示符下执行
cscript mytest.js

在这种情况下,我收到错误“自动化服务器无法创建对象”。

在 C# 中,我遵循了各种建议,最新的界面是:

[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()
    {
         ...
    }
 ...
}

谢谢,如果需要更多信息,请告诉我。

【问题讨论】:

  • 完成这项工作的几个步骤,其他问题已经很好地涵盖了它们。然而,没有人把这个名字弄错了。假设命名空间名称实际上是 MySimulator,ProgID 是“MySimulator.MyReader”,调用的方法是 MyFunction,而不是 MyMethod。
  • 汉斯 - 感谢您的关注。我之前做对了——尝试不同的事情。将其更改回 MySimulator.MyReader 并不能修复它。我已经查看并尝试了十几个潜在的解决方案,但还没有任何工作。我想知道,什么是关键/关键,以便 jscript 可以看到它,以便通过 jscript 创建对象?再次感谢。

标签: c# com activex jscript


【解决方案1】:

我假设如下。您的开发环境可能是 64 位操作系统,并且您的 C# DLL 项目可能配置为使用 Any CPU 作为平台目标进行编译。如果是这样,请继续阅读。

选择x86x64 并编译项目。如果您选择x86,则使用 32 位版本的 RegAsm.exe 注册您的程序集:

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

然后使用 32 位版本的 cscript.exe 运行您的 JavaScript 测试:

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

如果你选择x64,那就是:

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

[EDITED] 使用上述说明验证以下代码可以正常工作。

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();

输出

MyFunction called

【讨论】:

    猜你喜欢
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 2020-05-20
    • 2015-06-29
    相关资源
    最近更新 更多