2010-01-14 119 views
2

我創建了一個ActiveX組件,但無法訪問ASP.NET中的ActiveX控件。它使用JavaScript創建ActiveX對象時會出現「Microsoft JScript運行時錯誤:自動化服務器無法創建對象」錯誤消息。如何在ASP.NET中使用ActiveX控件

ActiveX組件代碼:

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代碼:

<%@ 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.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/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> 

回答

1

你可能需要註冊DLL。

請參閱this瞭解如何進行此操作的完整教程。

regasm AClass.dll/TLB /基本代碼

2

如果ActiveX控件被用JavaScript訪問,則ActiveX控件必須安裝爲一個瀏覽器(IE只)附加設置爲允許腳本權限。您收到的錯誤是因爲ActiveX控件在IE中無法訪問。

您可以在服務器上使用ActiveX控件(在ASP.NET中),但這很不尋常。 ActiveX控件主要用於瀏覽器,但由於ActiveX控件也是一個COM DLL,因此它是可能的。

我建議不要開發自己的ActiveX控件,IE的安全性變得更加緊張,除非是內部使用(即在防火牆後面),否則大多數人(訪問您的網頁)都會拒絕安裝在他們的計算機上。

相關問題