Use C # To compile ActiveX Controls (1)

Source: Internet
Author: User
To create a web project a few days ago, you must write an ActiveX control by yourself. Today, ActiveX controls are mostly developed using VB/C ++, and I am not familiar with them. Therefore, I want to write ActiveX controls using C.

First, create a winform Control Project helloworld, and drag a label control. Set the text to helloworld,

The content of usercontrol1.cs is as follows: Using System;
Using System. collections;
Using System. componentmodel;
Using System. drawing;
Using System. Data;
Using System. Windows. forms;

Namespace Helloworld
{
/**/ /// <Summary>
///Description of usercontrol1.
/// </Summary>
Public   Class Demo: system. Windows. Forms. usercontrol
{
Private System. Windows. Forms. Label label1;
/**/ /// <Summary>
///Required designer variables.
/// </Summary>
Private System. componentmodel. Container Components =   Null ;

Public Demo ()
{
//This call is required by the windows. Forms Form Designer.
Initializecomponent ();

//Todo: add any initialization after the initcomponent call

}

/**/ /// <Summary>
///Clear all resources in use.
/// </Summary>
Protected   Override   Void Dispose ( Bool Disposing)
{
If (Disposing)
{
If(Components! = Null)
Components. Dispose ();
}
Base . Dispose (disposing );
}

Generated by the component designerCode # Region Code generated by the component designer
/**/ /// <Summary>
///The designer supports the required methods-do not use the code editor
///Modify the content 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
}
}

Compile the project to generate helloworld. dll. Create a helloworld.htm file in the virtual root directory of iis. the HTML code is as follows: < Body Bgcolor = '#223344'>
<Object ID = "Helloworld" Classid = 'HTTP: // localhost/helloworld. dll # helloworld. demo' Width = "184" Height = "96" Viewastext >   </ Object >
</ Body >

Enter the following address in the IE Address Bar: http: // localhost/helloworld.htm. The page is displayed:

The control is successfully displayed on the page. OK. We have completed the first step.

However, the problem persists. Don't believe it? You can try testing on another machine. Note that you need to modify the corresponding HTML code and URL address. You can see that this is a Red Cross in the original display of the control, or a dialog box will pop up, indicating that the control does not have any permissions. This result is caused by Microsoft's default settings. The author must check the assemblyinfo of the control where the control is located. run a security statement in CS/Vb to declare that the control must use the granted permissions to display the interface. Reference the system. Security namespace in assemblyinfo. CS and add the following sentence:[Assembly: allowpartiallytrustedcallers ()]

Now recompile and replace the previous DLL. The interface is displayed again.

So far, we have not compiled ActiveX controls. This control can only display itself until now, and cannot implement more functions, such as interacting with scripts or operating the client registry or disk. This is due to the limitation of the. NET Framework security model. If we want this control to break through the limits of the. NET Framework security model to interact with the script or operate the registry or disk of the client, we must make it a real ActiveX control. Next, we will turn the control just now into a real ActiveX control.

First, use the tool-> to 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 your ActiveX control. Then modify the project properties,

Note that the last item in the panel must be changed to true.

Recompile. We can use the tool-> OLE/COM Object viewer to view,

As you can see, the helloworld. demo we wrote has been correctly recognized as a COM component. Now, we can display it on a webpage like using other ActiveX controls. Right-click helloworld. Demo,

Select copy HTML <Object> tag to clipboard to upload the code to the clipboard.

Now let's change to helloworld.htm. the HTML code is as follows: < Body Bgcolor = '#223344'>
<Object ID = "Helloworld"
Classid = "CLSID: 9551b223-6188-4387-b293-c7d9d8173e3a" Width = "184" Height = "96" >
</ Object >
</ Body >

Use IE to view the data, and our controls can be displayed on the webpage again. However, it is no longer a previous. Net winform control, but a real ActiveX control.

However, the task of compiling ActiveX controls is not completed yet. We have not implemented script interaction or read/write I/O, nor have we implemented automatic distribution of ActiveX controls. In the next blog, I will write ActiveX controls.

PS: This is my first post in the blog park, and I hope to get your support.

All rights reserved by the red horse world
blog: http://homer.cnblogs.com/
welcome to reprint, but reprint must indicate the author and source.

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.