使用C#开发ActiveX控件

http://www.mybdqn.com/

使用C#开发ActiveX控件

控件开发

使用C#进行ActiveX控件开发过程其实很简单。首先,在解决方案中添加一个类库项目,目标框架使用.NET Framework 2.0,如图1所示:

file:///C:/Users/è?¨??????~1/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg

图1创建ActiveX控件类库

此处有一个关键操作,需要设置类库项目属性->程序集信息->使程序集COM可见,如图2所示:

file:///C:/Users/è?¨??????~1/AppData/Local/Temp/msohtmlclip1/01/clip_image004.jpg

图2设置ActiveX控件类库程序集COM可见

ActiveX类库的内容大致包括两部分,IObjectSafety接口和实现该接口的控件类。考虑所有控件类都要实现IObjectSafety接口,可以将该接口的实现抽象为一个控件基类。 一、IObjectSafety接口

为了让ActiveX控件获得客户端的信任,控件类还需要实现一个名为“IObjectSafety”的接口。先创建该接口(注意,不能修改该接口的GUID值),接口内容如下:

[url=]file:///C:/Users/è?¨??????~1/AppData/Local/Temp/msohtmlclip1/01/clip_image005.gif[/url]

file:///C:/Users/è?¨??????~1/AppData/Local/Temp/msohtmlclip1/01/clip_image005.gif

1 [ComImport, Guid(\2 [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 3 public interface IObjectSafety 4 {

5 [PreserveSig]

6 intGetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions,[MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions); 7

8 [PreserveSig()]

9 intSetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] intdwEnabledOptions); 10 }

file:///C:/Users/è?¨??????~1/AppData/Local/Temp/msohtmlclip1/01/clip_image005.gif

[url=]file:///C:/Users/è?¨??????~1/AppData/Local/Temp/msohtmlclip1/01/clip_image005.gif[/url] 二、ActiveXControl控件基类

[url=]file:///C:/Users/è?¨??????~1/AppData/Local/Temp/msohtmlclip1/01/clip_image005.gif[/url]

file:///C:/Users/è?¨??????~1/AppData/Local/Temp/msohtmlclip1/01/clip_image005.gif

http://www.mybdqn.com/

1 public abstract class ActiveXControl : IObjectSafety 2 {

3 #regionIObjectSafety 成员 4

5 private const string _IID_IDispatch = \6 private const string _IID_IDispatchEx = \7 private const string _IID_IPersistStorage = \8 private const string _IID_IPersistStream = \9 private const string _IID_IPersistPropertyBag = \10

11 private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001; 12 private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002; 13 private const int S_OK = 0;

14 private const int E_FAIL = unchecked((int)0x80004005);

15 private const int E_NOINTERFACE = unchecked((int)0x80004002); 16

17 private bool _fSafeForScripting = true; 18 private bool _fSafeForInitializing = true; 19 20

21 public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions) 22 {

23 int Rslt = E_FAIL; 24

25 string strGUID = riid.ToString(\

26 pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER |INTERFACESAFE_FOR_UNTRUSTED_DATA; 27 switch (strGUID) 28 {

29 case _IID_IDispatch: 30 case _IID_IDispatchEx: 31 Rslt = S_OK;

32 pdwEnabledOptions = 0; 33 if(_fSafeForScripting == true)

34 pdwEnabledOptions =INTERFACESAFE_FOR_UNTRUSTED_CALLER; 35 break;

36 case _IID_IPersistStorage: 37 case _IID_IPersistStream:

38 case _IID_IPersistPropertyBag: 39 Rslt = S_OK;

40 pdwEnabledOptions = 0;

41 if(_fSafeForInitializing == true)

http://www.mybdqn.com/

42 pdwEnabledOptions =INTERFACESAFE_FOR_UNTRUSTED_DATA; 43 break; 44 default:

45 Rslt = E_NOINTERFACE; 46 break; 47 } 48

49 return Rslt; 50 } 51

52 public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, intdwEnabledOptions) 53 {

54 int Rslt = E_FAIL; 55

56 string strGUID = riid.ToString(\57 switch (strGUID) 58 {

59 case _IID_IDispatch: 60 case _IID_IDispatchEx:

61 if(((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER)&&

62 (_fSafeForScripting ==true)) 63 Rslt = S_OK; 64 break;

65 case _IID_IPersistStorage: 66 case _IID_IPersistStream:

67 case _IID_IPersistPropertyBag:

68 if(((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA)&&

69 (_fSafeForInitializing== true)) 70 Rslt = S_OK; 71 break; 72 default:

73 Rslt = E_NOINTERFACE; 74 break; 75 } 76

77 return Rslt; 78 } 79

80 #endregion 81 }

file:///C:/Users/è?¨??????~1/AppData/Local/Temp/msohtmlclip1/01

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4