views:

400

answers:

2

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

+1  A: 

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.

Richard
A: 

Make sure that your site is in 'Trusted Sites' on the client's machine.

Petey B