Writing ActiveX controls in C # (i)

Source: Internet
Author: User
Tags copy modify client root directory
Active|activex| Control

A few days ago to do a Web project, you must write an ActiveX control yourself. Most of today's ActiveX controls are developed using vb/c++, and I'm not familiar with them, so consider writing ActiveX controls using familiar C #.

First, create a WinForm control project HelloWorld and drag into a Label control, with the text set to HelloWorld, as shown in figure:


UserControl1.cs contents are as follows:

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Drawing;
Using System.Data;
Using System.Windows.Forms;

Namespace HelloWorld
{
/**////<summary>
Summary description of the UserControl1.
</summary>
public class Demo:System.Windows.Forms.UserControl
{
Private System.Windows.Forms.Label Label1;
/**////<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;

Public Demo ()
{
This call is required for the Windows.Forms form designer.
InitializeComponent ();

TODO: Add any initialization after the initcomponent call

}

/**////<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
Components. Dispose ();
}
Base. Dispose (disposing);
}

Component Designer generated Code #region Component Designer generated code
/**////<summary>
Designer supports the desired method-do not use the Code Editor
Modify the contents of this method.
</summary>
private void InitializeComponent ()
{
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
}
}


When you compile the project, you can generate HelloWorld.dll. Copy this DLL to the virtual root directory of IIS and then create a helloworld.htm file with the following HTML code:

<body bgcolor= ' #223344 ' >
<object id= "HelloWorld" classid= ' Http://localhost/HelloWorld.dll#HelloWorld.Demo ' width= "184" height= "96" Viewastext> </object>
</body>

In the IE Address bar, enter the following address: http://localhost/helloworld.htm, the interface appears:

As shown in the figure, the control has been successfully displayed on the page. OK, we've finished the first step.

But the problem has not been solved here. Don't believe it? You can try the test on another machine, note that you need to modify the corresponding HTML code and URL address. You can see this is a red fork where the control was originally displayed, or a dialog box appears, indicating that the control does not have any permissions. This result is due to Microsoft's default setting, and the author must execute a security declaration in the assemblyinfo.cs/vb of the control in which the control is located, declaring that the control must use the given permission to display the interface. We refer to the System.Security namespace in AssemblyInfo.cs and add a sentence:

[Assembly:allowpartiallytrustedcallers ()]

Now recompile, and replace the previous DLL, the interface can be displayed again.

The caveat is that until now, we haven't written a real ActiveX control. So far, this control is only able to implement its own display, and can not achieve more functions, such as the implementation of the script or the operation of the client's registry or disk. This is due to the limitations of the. Net Framework's security model. If we want this control to break through the limitations of the. Net Framework security model, to interact with the script or to manipulate the client's registry or disk, you must make it a real ActiveX control. Below, we turn the control that we just made into a real ActiveX control.

First Use the tool-〉 create a GUID to generate a GUID and modify the UserControl1.cs file. First add the reference System.Runtime.InteropServices namespace and add a statement before the demo:

Note that the string in the GUID is the GUID string you generated. It is the unique identifier of the ActiveX control you are generating. Then modify the project properties, as shown in figure:

Note the last item in the panel, the only thing we need to change is to change its value to true.

Recompile. We use the tool-〉ole/com Object Viewer to view, as shown in figure:

As you can see, the Helloworld.demo we write has been correctly identified as a COM component. Now, we can already display it in a Web page just as you would with other ActiveX controls. In Helloworld.demo click the right mouse button, as shown in figure:


Select Copy HTML <object> Tag to Clipboard, and you can copy the code into the Clipboard.

Now, we rewrite the helloworld.htm,html code as follows:

<body bgcolor= ' #223344 ' >
<object id= "HelloWorld"
Classid= "CLSID:9551B223-6188-4387-B293-C7D9D8173E3A" width= "184" height= ">"
</object>
</body>


With IE view, our controls can also be displayed in the Web page. This time, however, it is no longer a previous. NET WinForm control, but a genuine ActiveX control.

However, the task of writing ActiveX controls has not yet been completed. We have not implemented scripting interaction or read/write I/O, nor implemented automatic distribution of ActiveX controls. In the next blog, I'll finish writing ActiveX controls.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.