VS2005自定義ActiveX控件在asp.net中應(yīng)用方法
開發(fā)環(huán)境為VS 2005, .NET framework 2.0
文件—>新建—>項(xiàng)目
彈出下面對(duì)話框
選擇Windows 控件庫 輸入名稱TestControl 點(diǎn)擊“確定”
在設(shè)計(jì)窗口中拖入控件 label1 timer 2個(gè)button 如下圖:
1. COM的接口和類創(chuàng)建
COM 接口(Interface):IMonitor
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.ComponentModel;
5using System.Runtime.InteropServices;
6
7namespace TestControl
8{
9 [
10 Guid("b505d5bb-2318-4fc9-8e91-6346cdd6fc91"),
11 InterfaceType(ComInterfaceType.InterfaceIsDual),
12 ComVisible(true)
13 ]
14 public interface IMonitor
15 {
16 void LoadControl(string sClassroom);
17 void UnloadControl();
18 }
19}
20
COM 類(Class):Monitor
Code
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8using System.Runtime.InteropServices;
9
10namespace TestControl
11{
12 [
13 Guid("b505d5bb-2318-4fc9-8e91-6346cdd6fc91"),
14 ProgId("TestControl.Monitor"),
15 ClassInterface(ClassInterfaceType.None),
16 ComDefaultInterface(typeof(IMonitor)),
17 ComVisible(true)
18 ]
19 public partial class Monitor : UserControl,IMonitor, IObjectSafety
20 {
21 public Monitor()
22 {
23 InitializeComponent();
24 }
25
26 public void LoadControl(string msg)
27 {
28 if (timer.Enabled == false)
29 {
30 timer.Start();
31 }
32 }
33 public void UnloadControl()
34 {
35 if (timer.Enabled == true)
36 {
37 timer.Stop();
38 }
39 }
40
41 private void timer_Tick(object sender, EventArgs e)
42 {
43 label1.Text = DateTime.Now.ToString();
44 }
45
46
47 private void Monitor_Load(object sender, EventArgs e)
48 {
49
50 }
51
52 private void button1_Click(object sender, EventArgs e)
53 {
54 timer.Enabled = true;
55 LoadControl("開始計(jì)時(shí)");
56 }
57
58 private void button2_Click(object sender, EventArgs e)
59 {
60 timer.Enabled = false;
61 UnloadControl();
62 }
63 }
64}
1)GUID 指定該類或者接口的GUID。
2)聲明屬性InterfaceType(ComInterfaceType.InterfaceIsDual),ComVisible(true),以支持register 和 unregister。
3) IMonitor 是COM interface。C# 的COM Class 可以繼承自COM interface 也可以不用interface.
2 實(shí)現(xiàn)IObjectSafety 接口
微軟IObjectSafety 接口定義, GUID 為固定值, 一般不要修改,也可以重新生成GUID 在菜單欄 工具—>創(chuàng)建GUID
聲明:
ObjectSafety
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Runtime.InteropServices;
5
6namespace TestControl
7{
8 [
9 Serializable,
10 ComVisible(true)
11 ]
12 public enum ObjectSafetyOptions
13 {
14 INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
15 INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
16 INTERFACE_USES_DISPEX = 0x00000004,
17 INTERFACE_USES_SECURITY_MANAGER = 0x00000008
18 };
19
20 //
21 // MS IObjectSafety Interface definition
22 //
23 [
24 ComImport(),
25 Guid("b505d5bb-2318-4fc9-8e91-6346cdd6fc91"),
26 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
27 ]
28 public interface IObjectSafety
29 {
30 [PreserveSig]
31 long GetInterfaceSafetyOptions(ref Guid iid, out int pdwSupportedOptions, out int pdwEnabledOptions);
32
33 [PreserveSig]
34 long SetInterfaceSafetyOptions(ref Guid iid, int dwOptionSetMask, int dwEnabledOptions);
35 };
36}
37
Monitor實(shí)現(xiàn):
Code
1[IObjectSafety implementation]#region [IObjectSafety implementation]
2 private ObjectSafetyOptions m_options = ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_CALLER |
3 ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_DATA;
4
5 public long GetInterfaceSafetyOptions(ref Guid iid, out int pdwSupportedOptions, out int pdwEnabledOptions)
6 {
7 pdwSupportedOptions = (int)m_options;
8 pdwEnabledOptions = (int)m_options;
9 return 0;
10 }
11
12 public long SetInterfaceSafetyOptions(ref Guid iid, int dwOptionSetMask, int dwEnabledOptions)
13 {
14 return 0;
15 }
16 #endregion
如需轉(zhuǎn)載請(qǐng)加入本人Blog地址
IObjectSafety 是一個(gè)接口,它可將其功能顯露給 Internet Explorer的“設(shè)置腳本安全性”和“設(shè)置初始化安全性”安全特性。
3.注冊(cè)和卸載
如果Class 聲明中使用了 InterfaceType ,ComVisible(true) 屬性,并且項(xiàng)目屬性頁設(shè)置 點(diǎn)擊“生產(chǎn)”的 “為COM interop 注冊(cè)” 項(xiàng)為TRUE,那么VS編譯該項(xiàng)目可自動(dòng)為Class注冊(cè)。
也可以使用VS2005 自帶工具 regasm.exe 手動(dòng)注冊(cè)和卸載 Class,可以用VS 自帶工具oleview 查看是否注冊(cè)成功
4.測試 以及調(diào)試
1)創(chuàng)建測試頁面 解決方案右鍵 添加—>新建網(wǎng)站 Default.aspx
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。