c# - 在 Javascript 中处理 ActiveX 控件异常 (C#)

标签 c# javascript activex

我使用 COM Interop 在 C# 中编写了一个 ActiveX 控件来公开方法/属性。

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

有没有办法在 javascript 中处理 C# 抛出的异常?我找遍了,但找不到任何东西?

例如。

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

最佳答案

调用 alert(e.message) 对我有用。

确保您的 ActiveX 类正在实现 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);
}

关于c# - 在 Javascript 中处理 ActiveX 控件异常 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17198734/

相关文章:

javascript - jquery mobile显示html内容

c# - 无需 VS 设计器即可动态添加 ActiveX 控件

javascript - 带侧边栏的单滚动网页

.net - 如何注册 .net exe 公开的 com 接口(interface)?

delphi - 新版本的第三方 COM DLL - 如何在 Delphi 中安装和保留旧版本?

c# - Telerik ComboBox OnClientKeyPressing 搜索非常慢

c# - System.ObjectDisposedException - 在 Using block 中使用 SQL 连接

c# - 范围内数字的 BinarySearch 限制

c# - 直接在 Visual Studio 项目中使用自定义控件

javascript - 将用户转移到静态浏览器不支持页面