最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to use ActiveX with ASP.NET - Stack Overflow

programmeradmin1浏览0评论

I have created an ActiveX ponent, but not able to access that ActiveX poment in ASP.NET. It gives "Microsoft JScript runtime error: Automation server can't create object" error message while creating activeX object using javascript.

ActiveX Component Code:

using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace FirstActiveX
{
    [Guid("465F2D2E-C638-413e-A353-01E09DC4C7ED")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [ComVisible(true)]
    public interface IMyActiveX
    {
        [DispId(1)]
        string FirstName{ get; set;}
        [DispId(2)]
        string LastName { get; set; }
        [DispId(3)]
        string Address { get; set; }
        [DispId(4)]
        void Show();
    }

    [Guid("8975D137-9D96-492c-87AE-37D653BADE16")]
    [ProgId("FirstActiveX.MyActiveX")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(IMyActiveX))]
    [ComVisible(true)]
    public class MyActiveX : IMyActiveX
    {
        #region IMyActiveX Members

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }

        public void Show()
        {
            MessageBox.Show(string.Format("Mr. {0} {1}, Address : {2}", FirstName, LastName, Address));
        }

        #endregion
    }

}

HTML Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebActiveXTest._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">

<html xmlns="" >
<head runat="server">
    <title></title>
</head>
<script language="javascript" type="text/javascript">

    function UseActiveX() {
        var x = new ActiveXObject("FirstActiveX.MyActiveX");
        x.FirstName = "Nirajan";
        x.LastName = "Singh";
        x.Address = "Kamothe, Navi Mumbai";
        alert(x.FirstName);
        return false;
    }

</script>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btnShow" runat="server" Text="Show" OnClientClick="return UseActiveX();" />
    </div>
    </form>

</body>
</html>

I have created an ActiveX ponent, but not able to access that ActiveX poment in ASP.NET. It gives "Microsoft JScript runtime error: Automation server can't create object" error message while creating activeX object using javascript.

ActiveX Component Code:

using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace FirstActiveX
{
    [Guid("465F2D2E-C638-413e-A353-01E09DC4C7ED")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [ComVisible(true)]
    public interface IMyActiveX
    {
        [DispId(1)]
        string FirstName{ get; set;}
        [DispId(2)]
        string LastName { get; set; }
        [DispId(3)]
        string Address { get; set; }
        [DispId(4)]
        void Show();
    }

    [Guid("8975D137-9D96-492c-87AE-37D653BADE16")]
    [ProgId("FirstActiveX.MyActiveX")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(IMyActiveX))]
    [ComVisible(true)]
    public class MyActiveX : IMyActiveX
    {
        #region IMyActiveX Members

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }

        public void Show()
        {
            MessageBox.Show(string.Format("Mr. {0} {1}, Address : {2}", FirstName, LastName, Address));
        }

        #endregion
    }

}

HTML Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebActiveXTest._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<script language="javascript" type="text/javascript">

    function UseActiveX() {
        var x = new ActiveXObject("FirstActiveX.MyActiveX");
        x.FirstName = "Nirajan";
        x.LastName = "Singh";
        x.Address = "Kamothe, Navi Mumbai";
        alert(x.FirstName);
        return false;
    }

</script>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btnShow" runat="server" Text="Show" OnClientClick="return UseActiveX();" />
    </div>
    </form>

</body>
</html>
Share Improve this question edited Jun 23, 2012 at 21:36 John Saunders 162k26 gold badges252 silver badges402 bronze badges asked Jan 14, 2010 at 7:46 Nirajan SinghNirajan Singh 2,8955 gold badges25 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

If the ActiveX control is accessed with JavaScript, then the ActiveX control must be installed as a browser (IE only) add-on with permissions set to allow scripting. The error you are receiving is because the ActiveX control is not accessible in IE.

You can use ActiveX controls on the server (in ASP.NET), but it would be unusual. ActiveX controls are primarily for the browser, but since an ActiveX control is also a COM DLL, it is possible.

I remend against developing your own ActiveX control, IE security has gotten tighter, and unless it is for internal use (i.e., behind a firewall), most people (visitors to your web page) will resist installing it on their puter.

You probably need to register the DLL.

See this for a plete tutorial on how to go about this.

regasm AClass.dll /tlb /codebase

发布评论

评论列表(0)

  1. 暂无评论