Write an ActiveX control with C #

xiaoxiao2021-03-06  14

Selected from the "Red Horse World" blog, made an appropriate modification will be a web project in the next two chapters (http://www.cnblogs.com/homer/), must write an ActiveX control yourself. Today's ActiveX control is mostly developed by VB / C , and I am not familiar with them, so I consider writing ActiveX controls using familiar C #. First, create a WinForm control project helloWorld and drag into a Label control, and the text is set to helloworld, as shown:

UserControl1.cs content is as follows:

Using

System; using

System.collections; using

System.componentmodel; Using

System.drawing; using

System.data; using

System.Windows.Forms; Namespace

HelloWorld

{/ ** ////

/// UserControl1 Summary Description. /// public class demo: system.windows.Forms.userControl {private system.windows.forms.Label label1; ////// // The designer variable is required. /// private system.componentmodel.container components = null; public demo () {// This call is required for the Windows.Forms Form Designer. InitializationComponent (); // Todo: Add any initialization after the initComponent call} / ** //// /// clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) Components.Dispose ();} Base.Dispose (Disposing);} Component Designer Generated Code # The Region component designer generated code / ** //// /// designer supports the required method - do not use the code editor // to modify the contents of this method.

/// private void initialization () {this.label1 = new system.windows.forms.label (); this.suspendlayout (); // // label1 // this.label1.location = new system. Drawing.Point (32, 32); this.label1.name = "label1"; this.label1.size = new system.drawing.size (120, 32); this.label1.tabindex = 0; this.label1.text = "HelloWorld"; this.label1.textalign = system.drawing.contentalignment.middleCenter; // // demo // this.controls.add (this.label1); this.Name = "demo"; this.size = new System.drawing.size (184, 96); this.ResumeLayout (false);} #ENDREGION}} The compile project at this time can generate HelloWorld.dll. Copy this DLL to the virtual root directory of IIS, then create a file file with HelloWorld.htm, HTML code as follows:

= '# 223344'>

Enter the following address in the IE address bar: http://localhost/helloWorld.htm, appears interface:

As shown in the figure, the control has been successfully displayed on the page. OK, we have completed the first step. But the problem has not been resolved here. Do not believe? You can try to test on another machine, pay attention to modify the corresponding HTML code and URL address. You can see this place where the control is displayed is a red fork, or a dialog will also pop up, indicating that this control does not have any permissions. This result is caused by Microsoft's default settings. The author must perform a security declaration in AssemblyInfo.cs / VB in which the control is located, and declare that this control must be given to the permissions. We reference the system.security namespace in AssemblyInfo.cs and add a sentence:

[assmbly: allowPartiallytrustedCallers ()]

Now recompile, and replace the previous DLL, the interface can be displayed again. It is necessary to remind that until now, we have written it yet is not a real ActiveX control. This control is only available now, but it is only possible to display itself, and more features cannot be implemented, such as implementing the interaction with the script or a registry or disk of the client. This is due to the security model of the .NET Framework. If we want this control to break through the restriction of the .NET Framework security model, implement the interaction with the script or how to operate the client's registry or disk, you must make it a true ActiveX control. Below, we turn the control just now into a real ActiveX control. First Use Tools -> Create GUID to generate a GUID and modify the userControl1.cs file. First add to the reference system.runtime.interopservices namespace and add a statement in front of DEMO: Note that the string in the GUID is the GUID string you generated. It is the unique identifier of the ActiveX control you generated. The project properties are then modified, as shown: Watch the last item in the panel, the only thing we need to modify is to change its value to True. Rebate. We use tools -> OLE / COM object viewer view, as shown: You can see that the HelloWorld.Demo we wrote has been properly identified as COM components. Now, we can already display it in the web page like using other ActiveX controls. Right-click on HelloWorld.Demo, as shown: Select Copy HTML Tag to Clipboard, you can copy the code into the clipboard. Now, we rewrite HelloWorld.htm, HTML code as follows:

= '# 223344'>

ClassID = "CLSID: 9551B223-6188-4387-B293-C7D9D8173E3A" width = "184" height = "96">

Use IE to view, our controls can be displayed in the web page. However, this time it is no longer the previous .NET WinForm control, but a genuine ActiveX control. However, the tasks written by the ActiveX control have not been completed. We have not yet implemented scripting interaction or reading and writing I / O, and there is no automatic distribution of ActiveX controls. First, let's try to implement and interact with JS.

We join the showMessage method in Demo:

Public void showmessage (String

MSG)

{IF (msg! = Null) {MessageBox.show (msg);}}

We recompile. Before re-access the page, let's modify the HTML code:

= '# 223344'>

ClassID = "CLSID: 9551B223-6188-4387-B293-C7D9D8173E3A" width = "184" height = "96"

>


= 'Click'>

Now, re-access http://localhost/helloWorld.htm, click the Click button, you should be able to interact. But the result is unfortunate, we found that IE jumped out of the dialog, as shown:

After clicking OK, we found the JS error. According to the prompt, we determine that the control can be run by modifying the setting of IE. Open IE Tools -> Internet Options -> Security -> Local Intranet -> Custom Level -> That is not labeled as secure ActiveX control to initialize and run, set its value to enable. We refresh the page, and now you can finally run correctly.

Public void showmessage (String

MSG)

{IF (msg! = Null) {MessageBox.show (msg);}}

We recompile. Before re-access the page, let's modify the HTML code:

= '# 223344'>

ClassID = "CLSID: 9551B223-6188-4387-B293-C7D9D8173E3A" width = "184" height = "96"

>


= 'Click'>

Now, re-access http://localhost/helloWorld.htm, click the Click button, you should be able to interact. But the result is unfortunate, we found that IE jumped out of the dialog, as shown:

After clicking OK, we found the JS error. According to the prompt, we determine that the control can be run by modifying the setting of IE. Open IE Tools -> Internet Options -> Security -> Local Intranet -> Custom Level -> That is not labeled as secure ActiveX control to initialize and run, set its value to enable. We refresh the page, and now you can finally run correctly.

Of course, we can't expect our customers to modify this value as us. After all, one is to operate trouble, and the other is to bring a lot of security risks to the computer. After searching on the Internet, you must implement the IObjectsafety interface, tag the ActiveX control as a secure ActiveX control. After searching for MSDN, I found the definition of the IOBJECTSAFETY interface. This is good. First of all, we use C # to implement this interface:

[GUID ("CB5BDC81-93C1-11CF-8F20-00805F2CD064"

), InterfaType (CominterFactype.Interfaceisiunknown)] Public Interface

IOBJECTSAFETY

{// methods void GetInterfacceSafyOptions (System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions); void SetInterfaceSafetyOptions (System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions);} Note that this is the GUID Can't change. Then we implement this interface in the Demo class. Add code:

IObjectsafety member

#region IObjectSafety members public void GetInterfacceSafyOptions (Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions) {// TODO: Add WebCamControl.GetInterfacceSafyOptions achieved pdwSupportedOptions = 1; pdwEnabledOptions = 2;} public void SetInterfaceSafetyOptions (Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions ) {// Todo: Add WebcamControl.setInterfaceSafetyOptions implementation} #ENDREGION

IObjectsafety member

#region IObjectSafety members public void GetInterfacceSafyOptions (Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions) {// TODO: Add WebCamControl.GetInterfacceSafyOptions achieved pdwSupportedOptions = 1; pdwEnabledOptions = 2;} public void SetInterfaceSafetyOptions (Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions ) {// Todo: Add WebcamControl.setInterfaceSafetyOptions implementation} #ENDREGION

Re-compile, then change the settings in IE back. Now, we found that there is no problem with JS interaction.

In this way, a one of the most basic ActiveX controls is already written. You can add any features you need on this control. Here, the task of writing controls has been completed, and our next goal is to publish it.

[GUID ("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfacePe (CominterFactype.Interfaceisiunknown)] Public Interface

IOBJECTSAFETY

{// methods void GetInterfacceSafyOptions (System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions); void SetInterfaceSafetyOptions (System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions);}

Note that this GUID cannot be changed. Then we implement this interface in the Demo class. Add code:

IObjectsafety member

#region IObjectSafety members public void GetInterfacceSafyOptions (Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions) {// TODO: Add WebCamControl.GetInterfacceSafyOptions achieved pdwSupportedOptions = 1; pdwEnabledOptions = 2;} public void SetInterfaceSafetyOptions (Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions ) {// Todo: Add WebcamControl.setInterfaceSafetyOptions implementation} #ENDREGION

IObjectsafety member

#region IObjectSafety members public void GetInterfacceSafyOptions (Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions) {// TODO: Add WebCamControl.GetInterfacceSafyOptions achieved pdwSupportedOptions = 1; pdwEnabledOptions = 2;} public void SetInterfaceSafetyOptions (Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions ) {// Todo: Add WebcamControl.setInterfaceSafetyOptions implementation} #ENDREGION

Re-compile, then change the settings in IE back. Now, we found that there is no problem with JS interaction.

转载请注明原文地址:https://www.9cbs.com/read-67535.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.050, SQL: 12