C # ActiveX Control Development
For ActiveX control development, there are many examples on the Internet. It took a whole day to study yesterday. most examples on the Internet use JavaScript to call controls. To implement the event of "corresponding" controls on the html page, it takes a whole day. the key points are "creating ActiveX controls" 2nd, and 7th. This technology has great limitations, such as Browser needs to be installed.. net Framework, limited to IE browser. for more information about ActiveX certificates and settings during browser installation, see www.bkjia.com. create ActiveX Control 1. create a class library. 2. project properties-Application-assembly information-"Make Assembly COM visible" check; 3. project properties-generate-register for COM interoperability. (This will take a day, otherwise the registration event will be unavailable); 4. create interface: IObjectSafety (note that the GUID cannot be changed); 5. create a base class of ActiveX control and implement IObjectSafety. ActiveX control can inherit it to reduce code; 6. create an ActiveX Custom controls such as ActiveXDemo1; 7. define ActiveXDemo1's "method interface" and "event interface ". (This method is required if custom events are used.) The [DispId (x)] identifier should be added to the "event interface" member; 8. activeX Control created. the IObjectSafety interface defines [ComImport, Guid ("4A359FBB-C9A4-494E-B048-AC068DB4FCB2")] // The GUID cannot be changed to [InterfaceType (ComInterfaceType. interfaceIsIUnknown)] public interface IObjectSafety {[PreserveSig] int GetInterfaceSafetyOptions (ref Guid riid, [financialas (UnmanagedType. u4)] ref int pdwSupportedOption S, [financialas (UnmanagedType. u4)] ref int pdwEnabledOptions); [PreserveSig ()] int SetInterfaceSafetyOptions (ref Guid riid, [financialas (UnmanagedType. u4)] int dwOptionSetMask, [financialas (UnmanagedType. u4)] int dwEnabledOptions);} ActiveX control base class (ActiveXControlBase) public class ActiveXControlBase: IObjectSafety {# region IObjectSafety member private const string _ IID_IDispatch = "{Condition 00000000046} "; private const string _ IID_IDispatchEx =" {a6ef9860-c720-11d0-9337-00a0c90dcaa9} "; private const string _ IID_IPersistStorage =" {region} "; private const string _ IID_IPersistStream =" {region }"; private const string _ IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}"; private const int INTERFACESAFE_FOR_UNTRUS TED_CALLER = 0x00000001; private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002; private const int S_ OK = 0; private const int E_FAIL = unchecked (int) 0x80004005 ); private const int E_NOINTERFACE = unchecked (int) 0x80004002); private bool _ fSafeForScripting = true; private bool _ fSafeForInitializing = true; public int GetInterfaceSafetyOptions (ref Guid riid, ref int pdwSupportedOptions, r Ef int pdwEnabledOptions) {int Rslt = E_FAIL; string strGUID = riid. toString ("B"); pdwSupportedOptions = optional | switches; switch (strGUID) {case _ IID_IDispatch: case _ IID_IDispatchEx: Rslt = S_ OK; pdwEnabledOptions = 0; if (_ fSafeForScripting = true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; break; case _ IID_IPersistStorage: ca Se _ IID_IPersistStream: case _ cases: Rslt = S_ OK; pdwEnabledOptions = 0; if (_ fSafeForInitializing = true) pdwEnabledOptions = enabled; break; default: Rslt = E_NOINTERFACE; break ;} return Rslt;} public int SetInterfaceSafetyOptions (ref Guid riid, int dwOptionSetMask, int dwEnabledOptions) {int Rslt = E_FAIL; string strGUID = riid. toString ("B"); switc H (strGUID) {case _ IID_IDispatch: case _ IID_IDispatchEx: if (dwEnabledOptions & dwOptionSetMask) = signature) & (_ fSafeForScripting = true) Rslt = S_ OK; break; case _ IID_IPersistStorage: case _ When: if (dwEnabledOptions & dwOptionSetMask) = signature) & (_ fSafeForInitializing = true) Rslt = S _ OK; break; default: Rslt = E_NOINTERFACE; break;} return Rslt;} # endregion} custom ActiveX Control [ComVisible (true)] [Guid ("identifier")] [InterfaceType (ComInterfaceType. interfaceIsIDispatch)] public interface IThreadDemoEvent {[DispId (1)] // you must add this identifier void ShowMessage1 (string str_Msg); [DispId (2)] void ShowMessage2 (string str_Msg);} [ComVisible (true)] [Guid ("4D12136B-9545-423B-A1 10-B9405ADF8B30 ")] [InterfaceType (ComInterfaceType. interfaceIsDual)] public interface IThreadDemo {string StartTimer (); string StopTimer ();} [Guid ("2B4FCB85-A3B7-43BD-B104-7380E7F3483F"), ClassInterface (ClassInterfaceType. autoDual), ComDefaultInterface (typeof (IThreadDemo), ComSourceInterfaces (typeof (IThreadDemoEvent), ComVisible (true)] public class ActivexThreadDemo: ActiveXControlBase, IThreadDem O {~ ActivexThreadDemo () {ShowMessage1 ("released");} Thread _ th; bool _ isStop; public event ShowMessageHandle ShowMessage1; public event ShowMessageHandle ShowMessage2; void ThreadMethd () {while (true) {Thread. sleep (3000); if (ShowMessage1! = Null) {ShowMessage1.Invoke (DateTime. now. toString ();} if (_ isStop) break;} _ th. abort (); _ th = null;} public string StartTimer () {if (_ th = null) {_ isStop = false; _ th = new Thread (ThreadMethd ); _ th. isBackground = false; _ th. start (); return "Start Time";} if (ShowMessage2! = Null) {ShowMessage2 ("StartTimer executed");} return "started timing;";} public string StopTimer () {if (_ isStop) {return "the timing has been stopped! ";}Else {_ isStop = true; return" Stop timing ";}}}note: events cannot be declared using generic delegation, for example, public event Action <T> ShowMessageHandle; when the class contains static members, the cross-thread trigger event is not cleared when the page is refreshed: [event]. invoke (parameter 1, parameter 2 ...); activeX Control Setup1. create an Installer project; 2. right-click Add project-> project output. The add project output group dialog box is displayed, and the ActiveX control class library is selected. 3. the attribute Register value of the primary output file is vsdrpCOM (key), and RemovePreviousVersions is set to true for web page testing; 1. create an object tag. calassid is the GUID of the Control <object id = "ActiveXObj1" classid = "clsid: 3BCF612C-91C F-4543-83BB-FD2331FDDCB6 "> </object> 2. call the control method var r = document. activeXObj1.Test1 (); 3. "register control events" <script language = "javascript" type = "text/javascript" for = "ActiveXObj1" event = "ShowMessage1 (msg)"> alert ("ActiveXObj1: "+ msg) </script>