Embedded the custom. Net Control with COM object interface into IE browser

Source: Internet
Author: User
Tags ole sendmsg
Embed custom. Net controls into ie browsers
I'm afraid everyone is familiar with developing custom winform controls using Visual Studio. NET. Under normal circumstances, this control can only be used for development of. Net-based Windows application programs, but cannot be directly embedded into IE, which is a pity.
After all,. Net controls are different from custom controls developed using Delphi or C ++ builder in Win32. Any controlled accessory developed Based on. NET
(Assembly) are all il code and can be. net
Framework for reflection and security checks. There should be a way to make this special control be used by IE.
After several days of research, we finally found a way to apply the. NET winform custom control to common Windows
Application Development, you can use it as an ActiveX control to directly embed it into IE and interact with the JS scripting language (the client needs to install. net
Framework ). This can undoubtedly greatly save the porting of client code in windows and Web applications, and maintain the maximum reuse of control code.
The principle is: the. NET custom control does not have the guid unique identifier of Common ActiveX, and to solve the "DLL hell" problem caused by previous version conflicts, the. NET control no longer enters the Registry
The registration of the row type, but different versions are deployed in the current directory of the application or GAC (Globle assembly
Cache .. Net can generate a strong-name proxy for the. Net Control and provide it to other languages such as VB, Vc, and Delphi. We only need to install the accessory file (under C #
You can set these switches in assemblyinfo. CS, or in the Project Properties of vs. net. In this way,. Net can be automatically generated for us.
The OLE object feature corresponding to classlibrary. Then we will create the event interface and security interface used by Activex.
Step 1-create a. Net custom control.
The following describes how to use Visual Studio. NET 2005 professional as a development tool.
Create a new project and select windows control library. The default name is Windows controllibrary1.
Place the following controls on the Interface: textbox and button. This is all the content on the interface. Double-click the button control on the interface and write the following code:
Private void button#click (Object sender, eventargs E)
{
Streamwriter bplstreamwriter = new streamwriter (
System. environment. getenvironmentvariable ("systemdrive") + "// a.txt ",
True );
Bplstreamwriter. writeline (datetime. Now. touniversaltime () +
"/Tinserted text by. Net winformcontrol .");
Bplstreamwriter. Close ();
}

This Code creates a file in the root directory of the system disk and adds a line of text. To call the streamwriter class, we need to add the system. Io namespace.
Add the property of the custom control: buttoncaption.
[Category ("custom control attributes")]
[Description ("text content of the Setting button")]
Public String buttoncaption
{
Get
{
Return button1.text;
}
Set
{
Button1.text = value;
}
}
Add a delegate and create a key-click event:
// Declare the key-Click Event Type
Public Delegate void buttonclickhandler (string sendmsg );
Public partial class usercontrol1: usercontrol
{
// Declare a key-Click Event
Public event buttonclickhandler onbuttonclick;

Click the button to add the code to ignite the event:
Private void button#click (Object sender, eventargs E)
{
......
If (onbuttonclick! = NULL)
Onbuttonclick (textbox1.text );
}
Add the public method settext:
Public void settext (string text)
{
Textbox1.text = text;
}
Click Run to use vs. net.
2005 the built-in control container runs our control. You can use the Attribute Editor to verify the buttoncaption attribute. Click the button to create a file on the system disk and add the file
This content. You can also create a new windows application project. After adding the control, you can test its properties, events, and methods.

In the previous article, I introduced how to create a common. Net Control. To embed the. Net control into IE
Add the com feature to the control.

Open Project Properties and click "assembly information…" on the application page ..." Button to open
Make Assembly com-visible option.

Go to the build page of the Project Properties and open the register for com InterOP option.

Save the configuration and build it. the. NET control has the features of a common OLE object. Enable vs. net
OLE/COM Object Viewer (default directory: C:/program files/Microsoft Visual Studio
8/common7/tools/bin/oleview. EXE ). We can see the control we created under. Net category.
Windows controllibrary1.usercontrol1. All information has been automatically registered, including
Strong path.

In fact, at this time, the. NET control can already be put into IE, but it cannot be regarded as a qualified ActiveX control, but still
However, it is a qualified. Net Control.

We compile an HTM file to test the control. The content of the HTM file is as follows:

Text Box: <HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK">
</Head>
<Body bgcolor = "# d8eefb">
<Object ID = "usercontrol1"
Classid = "windowscontrollibrary1.dll # windowscontrollibrary1.usercontrol1"
Viewastext> </Object>
<Br>
<Input type = 'button 'value = 'call method 'onclick = 'usercontrol1. settext ("Hello world! "); '>
<Br>
<Input type = 'button 'value = 'set attribute 'onclick = 'usercontrol1. buttoncaption =
"Set"; '>
</Body>
</Html>

Deploy the HTM file and windowscontrollibrary1.dll in the virtual directory of IIS and use a browser to browse
You can see the following results.

Although the. Net control can be displayed in IE, the. NET Framework can dynamically check the code security because
Without security authorization, insecure Code cannot be executed. For example, click the button and we will receive the following prompt.

Interestingly, as long as the control does not have unsafe code during loading, ie will not have any prompts, unlike IE
Security warnings when ActiveX is loaded. This shows that the security of IE's. Net code is handled by the framework.
. Therefore, this type of control is also useful, that is, it can be loaded and displayed in the user's ie browsing without digital signature.
And smoothly execute the secure controlled code. Of course, the premise is that you need to install. NET Framework.

At this time, we still do not get a real ActiveX control, although it can be published on the web page through IIS
. However, it has the following limitations: it cannot be published to other web servers
Access local resources, JS scripts cannot respond to control events, etc.

In the next article, we will discuss how the. NET custom control implements all COM object interfaces, including attributes, methods, and events.
. And use JS scripts to form interactions on Web pages.

This time, we will discuss how the. NET custom control implements all COM object interfaces, including attributes, methods, and events. And use JS scripts to form interactions on Web pages.
After adding the com feature of the. Net Control as described in the previous article, vs. Net has automatically registered the properties, methods, and events of the control, but we still need to implement the event interface.
To implement the event interface, we need to add the following code:
[Comvisible (true)]
[GUID ("43bcdb16-0281-492b-bb53-997546122442")]
[Interfacetype (cominterfacetype. interfaceisidispatch)]
Public interface usercontrol1events
{
[Dispid (0)]
Void onbuttonclick (string sendmsg );
}
To ensure the validity of the guid, we can use the create guid tool of vs. net. The method in the interface is the event function to be triggered. The declaration of this function must follow the events in our control, and the names must be consistent.
Public event buttonclickhandler onbuttonclick;
The next step is to create a guid for our control class and implement the event interface declared above. Here, we should not try to implement the event interface, but reference the COM interface in attribute mode.
[GUID ("FBA0FCC2-D75F-40a5-9C8C-1C9D03813731")]
[Comsourceinterfaces (typeof (usercontrol1events)]
Public partial class usercontrol1: usercontrol
{
After the build, we have obtained a COM control with public attributes, methods, and events.
We create an HTML file to test the new ActiveX control. The Code is as follows:
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK">
</Head>
<Script language = 'javascript 'For = 'usercontrol1' event = 'onbuttonclick (Message) '>
Alert ("the event triggered by ActiveX is responded successfully. "+ Message );
Window. defaultstatus = "onbuttonclick ()";
</SCRIPT>

<Body>
<Object ID = "usercontrol1" classid = "CLSID: FBA0FCC2-D75F-40a5-9C8C-1C9D03813731">
</Object>
<Br>
<Input type = 'button 'value = 'Call ActiveX method 'onclick = 'usercontrol1. settext ("Hello world! "); '>
<Br>
<Input type = 'button 'value = 'sets ActiveX attributes 'onclick = 'usercontrol1. buttoncaption = "set";'>

</Html>
This time, we no longer need IIS and can directly run this HTML file. The following result is displayed:
Our. Net controls can be used as Common ActiveX controls. The. NET control can also be used by. Net Windows applications.

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.