Use C # To write ActiveX

Source: Internet
Author: User
Create a project of the "Windows Forms control library" Type

 

Set the accessibility of the Assembly

Add the following code to assemblyinfo. CS:

Using system. Security; <br/> [Assembly: allowpartiallytrustedcallers]

 

Set "allow interaction with com"

After opening the properties page of the project,
On the Application tab,
Click "assembly information..." and select "make the Assembly c0m visible ";

On the "generate" tab, select "register for com interoperability ".

 

 

Security Creation Interface

If this interface is not implemented, you must modify the IE Security Attribute "initialize and Run ActiveX controls that are not marked as secure" during use ".

[Comimport, guidattribute ("CB5BDC81-93C1-11CF-8F20-00805F2CD064")] <br/> [interfacetypeattribute (cominterfacetype. interfaceisiunknown)] <br/> Public interface IObjectSafety {<br/> [preservesig] <br/> void getinterfaccesafyoptions (<br/> int riid, <br/> out int pdwsupportedoptions, <br/> out int pdwenabledoptions); </P> <p> [preservesig] <br/> void setinterfacesafetyoptions (<br/> int riid, <br/> int dwoptionssetmask, <br/> int dwenabledoptions); <br/>}< br/>

 

User Control example

 Using system. runtime. interopservices; </P> <p> [GUID ("7cdf344d-a42d-44e0-a5b5-4562288b6a38")] <br/> [comvisible (true)] <br/> Public partial class usercontrol1: usercontrol, IObjectSafety {<br/> Public String message {Get; Set ;}< br/> Public mshtml. htmlwindow2class HTML {private get; Set ;}</P> <p> # region IObjectSafety member <br/> Public void getinterfaccesafyoptions (INT riid, out int pdwsupportedoptions, out int pdwenabledoptions) {<br/> conditions = 1; <br/> pdwenabledoptions = 2; <br/>}< br/> Public void setinterfacesafetyoptions (INT riid, int dwoptionssetmask, int dwenabledoptions) {<br/> throw new notimplementedexception (); <br/>}< br/> # endregion </P> <p> Public void showmessage () {<br/> MessageBox. show (Message); <br/>}</P> <p> private void messagebutton_click (Object sender, eventargs e) {<br/> showmessage (); <br/>}</P> <p> private void jsbutton_click (Object sender, eventargs e) {<br/> html.exe cscript (string. format ("document. getelementbyid ('numbertext '). value = '{0}'; ", datetime. now), "JavaScript"); <br/>}< br/>

 

HTML example

<Div> <br/> <Object ID = "myactivex" classid = "CLSID: 7cdf344d-a42d-44e0-a5b5-4562288b6a38 "> <br/> <Param name =" message "value =" the current attribute is successfully assigned by the param parameter "/> <br/> </Object> <br/> <input type = 'button 'onclick = 'myactivex. message = "the current attribute is successfully assigned by JS"; myactivex. showmessage () '<br/> value = 'interaction with controls using JavaScript'/> <br/> <input type = "text" id = "numbertext" readonly = "readonly"/> <br/> </div> </P> <p> <SCRIPT type = "text/JavaScript" Language = "J Avascript "> <! -- <Br/> window. onload = function () {<br/> document. myactivex. html = This; <br/>}; <br/> // --> </SCRIPT> <br/>

 

Install the installation package for ActiveX to be manually installed on the client.

Activexno, which uses .net, directly uses regsvr32.exe for installation and registration.

Vswill generate three types of cabw.msiw.setup.exe files

 

To enable the client to automatically prompt installation, add a digital signature to the installation program.

Open Visual Studio 2008 command line and run the following commands in sequence:
Makecert-SS myactivex-n "cn = mycorporation"-sv d:/myactivex. PVK-r d:/myactivex. Cer
Cert2spc D:/myactivex. Cer D:/myactivex. SPC
Signtool signwizard

 

 

References

ActiveX control development practices

C # ActiveX Control and cab package

Visual Studio 2008 (C #) ActiveX control development and cab package creation Summary (1)

Visual Studio 2008 (C #) ActiveX control development and cab package creation Summary (2)

Visual Studio 2008 (C #) ActiveX control development and cab package creation Summary (3)

 

Download

 

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.