How to use. Net to write ActiveX Controls

Source: Internet
Author: User
Plato labels:. net, ActiveX, ActiveX control, script, interoperability, assembly

Step 1: create a solution, demosolution, and add a Windows control library project. we name it demoactivex. For ease of reading, we change the control name to democontrol. CS and drag a Textbox Control to the form.

Step 2: create a public method test under the democontrol class to implement interoperability between controls and client scripts,

Public void test ()
{
MessageBox. Show ("the content you entered is:" + this. textbox1.text );
}

Step 3: Enable assemblyinfo. CS to modify assembly information. Reference System. security namespace, and add the [Assembly: allowpartiallytrustedcallers ()] security declaration. Modify [Assembly: comvisible (false)] to [Assembly: comvisible (true)] to make the Assembly com visible.

Step 4: register for com InterOP. Right-click the demoactivex Project Properties and tick "register for com InterOP" on the "generate" tab.

Step 5: add the namespace System in demoactivex. runtime. interopservices, use the tool-> Create guid tool to create a new guid {E5FD041B-8250-4cbc-B662-A73FC7988FB5} and use this GUID to initialize demoactivex, which is the classid we want to reference this ActiveX on the web page.

Step 6: implement the IObjectSafety interface and Mark ActiveX controls as secure.

Interface:

[GUID ("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), interfacetype (cominterfacetype. 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 );
}

Implementation:

Public void getinterfaccesafyoptions (int32 riid, out int32 pdwsupportedoptions, out int32 pdwenabledoptions)
{
// Todo: Add democontrol. getinterfaccesafyoptions to implement
Pdwsupportedoptions = 1;
Pdwenabledoptions = 2;
}

Public void setinterfacesafetyoptions (int32 riid, int32 dwoptionssetmask, int32 dwenabledoptions)
{
// Todo: Add democontrol. setinterfacesafetyoptions to implement
}

So far, the entire program has been completed. What we need to do next is how to publish this ActiveX

Step 7: release. Add a new Windows Installation Project winsetup to add the project's main output. Then, modify the ActiveX control's main output file and change its register attribute to vsdrpcom. Unfortunately, the installation URL must use an absolute path instead of a relative path. This means that the path must be determined when the installation program is generated, which is not very convenient. In the example, I used http: // localhost/demoactivex to change it to the actual domain name during release. Then generate the installation program and copy setup.exe and winsetup. MSI to the virtual directory demoactivex. /

Step 8: test. Compile an HTM page for testing. Note that codebase attribute code is added to the object block to specify the location of the downloaded control. Refer to the following code file.

Step 9: Add a signature. You can apply for one online.

Demoactivex. CS code:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. drawing;
Using system. Data;
Using system. text;
Using system. Windows. forms;
Using system. runtime. interopservices;

Namespace demoactivex
{
[GUID ("E5FD041B-8250-4cbc-B662-A73FC7988FB5")]
Public partial class democontrol: usercontrol, IObjectSafety
{
Public democontrol ()
{
Initializecomponent ();
}

Public void test ()
{
MessageBox. Show ("the content you entered is:" + this. textbox1.text );
}

Public void getinterfaccesafyoptions (int32 riid, out int32 pdwsupportedoptions, out int32 pdwenabledoptions)
{
// Todo: Add democontrol. getinterfaccesafyoptions to implement
Pdwsupportedoptions = 1;
Pdwenabledoptions = 2;
}

Public void setinterfacesafetyoptions (int32 riid, int32 dwoptionssetmask, int32 dwenabledoptions)
{
// Todo: Add democontrol. setinterfacesafetyoptions to implement
}

}

[GUID ("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), interfacetype (cominterfacetype. 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 );
}

}

Test.htm code:

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1">
<Table>
<Tr>
<TD align = "center">
<Object ID = "demoactivex"
Classid = "CLSID: E5FD041B-8250-4cbc-B662-A73FC7988FB5" codebase = "setup.exe" width = "200" Height = "100">
</Object>
</TD>
</Tr>
<Tr>
<TD align = "center">
<Input type = button id = "button1" value = "test" onclick = "javascript: dotest ()">
</TD>
</Tr>
</Table>

<SCRIPT>
Function dotest ()
{
VaR OBJ = Document. getelementbyid ("demoactivex ");
OBJ. Test ();
}
</SCRIPT>
</Form>
</Body>
</Html>

Test http: // localhost/demoactivex/test.htm OK! I think you have succeeded.

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.