简体   繁体   中英

Handling ActiveX Control Exceptions(C#) in Javascript

I've written an ActiveX Control in C# using COM Interop to expose methods/properties.

[ComVisible(true)]
class COMClass:ICOMClass
{ 
     public string methodA()
     {
          string str = "abc";
          if(str != "abcd")
              throw new Exception("invalid string");
         return str;
     }
}
[ComVisible(true)]
interface ICOMClass
{
    string methodA();
}

Is there a way to handle the exception thrown from C# inside javascript? I have looked all over but I could not find anything?

Eg.

var x = new ActiveXObject("COMClass");
try{
   x.methodA
}
catch(e) { 
   alert(e);
}

Calling alert(e.message) works for me.

Make sure your ActiveX class is implementing IObjectSafety :

using System;
using System.Runtime.InteropServices;

[ComImport()]
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IObjectSafety
{
    [PreserveSig()]
    int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions);

    [PreserveSig()]
    int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions);
}

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