简体   繁体   中英

How Can I Create My C# Com Class From JavaScript Using ActiveXObject

I wrote C# class to COM but I could not use it from JavaScript. Example

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(ICommandsEvents))]
[ProgId("Scripting.Commands")]
public class Commands : ICommands
{
    public Commands()
    {

    }
    public int CreateChannel(string channelName)
    {
        return 0;
    }

    public int GetChannelID(string channelName)
    {
        return CreateChannel(channelName);
    }

     public event ChannelEventsHandler OnChannelEvents;
}

[ComVisible(false)]
public delegate void ChannelEventsHandler(string a);

[ComVisible(true)]
[Guid("E2147768-8BA8-400b-8602-A1FDC31E6AA5")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ICommands
{
    [DispId(5)]
    int CreateChannel(string channelName);

    [DispId(6)]
    int GetChannelID(string channelName);
}

[ComVisible(true)]
[Guid("22316373-A8DF-4ace-B48C-EA9953BD73FF")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ICommandsEvents
{
    [DispId(1)]
    void OnChannelEvents(string a);
}

and I checked "Register for COM interop" checkbox of project property.

when I want to Create this from JavaScript like this.

var a = ActiveXObject("Scripting.Commands");

I am getting "Automation Server Can't create object" exception. What is my wrong.

Thank you

There are a large number of reasons for this kind of error.

  • Ensure you have an assembly level GuidAttribute for the type library
  • First check the registry that interface, type library and coclass registration are correct.
  • Use Process Monitor to check the registration is being read correctly.
  • Attach a debugger to the process, so you can add breakpoints to your code.
  • Does a C# client (using COM, so you'll need to import tge typelib to create a PIA) work?

But I notice your class does not have a GuidAttribute , so coclass registration will have failed.

确保您的站点位于客户端计算机上的“受信任的站点”中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM