Go C # Developing ActiveX controls,. NET developing OCX control cases

Source: Internet
Author: User

Citation: Baidu Http://hi.baidu.com/yanzuoguang/blog/item/fe11974edf52873aaec3ab42.html

What is the purpose of the ActiveX control? In the Web page and how to use the OCX control, such as QQ mailbox large file upload should be the use of OCX control, let's start.

0. Preface

An ActiveX control, formerly known as an OLE control or OCX control, is a software component or object that can be inserted into a Web page or other application. With ActiveX plug-ins, you can easily and conveniently insert multimedia effects, interactive objects, complex programs, and more in a Web page.

ActiveX controls are typically developed using C + + or VB, and this article explores the technical implementation of developing ActiveX controls in visual Studio 2005 environments using C #.

1. Problem scenario in the system of C/s architecture, the client implements some business functions, which can be conveniently implemented by installing the relevant application set.

The same demand, in the B/s architecture of the system is more difficult to achieve. Because all the programs are placed on the server side, the client simply uses the browser to access the server side via the HTTP protocol.

A more mature solution is to develop an ActiveX control installed to the client so that the client's browser can access the local ActiveX control to perform the relevant local operation.

What this article is going to talk about is developing an ActiveX control using C # to read and display the client's system time.

2. Development environment Windows XP Visual Studio 2005. NET Framework 2.0 (C #)

3. Implementation process

3.1.ActiveX Control Development

In the Visual Studio 2005 development environment, you can use the Windows Control Library project to implement ActiveX control development, but you need to make some necessary settings for your project.

Here's a look at how to develop an ActiveX control using the Windows Control Library project. First create an application solution and add a Windows Control Library project:

To change the project properties-application-assembly information setting, tick "make Assembly COM Visible":

Change the "Project Properties-Build" setting and tick "register for COM Interop" (Note that if it is modified in the debug state, it needs to be set again in the release state):

Modify the AssemblyInfo.cs file to add a [Assembly:allowpartiallytrustedcallers ()] entry (you need to reference the System.Security namespace):

usingSystem.Reflection;usingSystem.Runtime.CompilerServices;usingSystem.Runtime.InteropServices;usingSystem.Security; [Assembly:assemblytitle ("Yilin.Preresearch.CSharpActiveX")][assembly:assemblydescription ("")][assembly:assemblyconfiguration ("")][assembly:assemblycompany ("10BAR")][assembly:assemblyproduct ("Yilin.Preresearch.CSharpActiveX")][assembly:assemblycopyright ("Copyright©10bar")][assembly:assemblytrademark ("")][assembly:assemblyculture ("")][assembly:allowpartiallytrustedcallers ()][assembly:comvisible (true)][assembly:guid ("114D1F0C-43B8-40AC-AE7C-5ADCCC19AEF3")][assembly:assemblyversion ("")][assembly:assemblyfileversion ("")]

To add a Windows user control:

The development of the control is done in the same way as the Windows user control, in this example, the main implementation of the two business functions, one is to provide a public method for reading the usbkey stored in the signature certificate, saved to the local C packing directory, and return operation information;

Another business function provides a UI interface that includes a button control and a Label control, the Click event of the button control invokes the method provided earlier, and the return information is displayed on the label control.

This can achieve two purposes, one, the ActiveX control provides a public method for the B/s program to call directly, from the subsequent realization of business functions;

Second, the ActiveX control can provide a B/s program UI interface, by responding to the B/S program in the UI operation events to achieve business functions.

After the control has been developed, the following modifications are required to make the user control available as an ActiveX control:

First, add a GUID for the control class that will be used for the client call of the B/s system (you can create a GUID using the Tools-Create GUID menu):

Guid ("4a44cf4e-f859-4328-aa22-3e9d7afff1ab")] public partial class Hello:usercontrol {

Second, in order for the ActiveX control to gain trust from the client, the control class also needs to implement an interface called "IObjectSafety". Create the interface first (note that the GUID value of the interface cannot be modified):

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;namespacepreresearch.csharpactivex{[ComImport, GuidAttribute ("cb5bdc81-93c1-11cf-8f20-00805f2cd064")] [InterfaceTypeAttribute (Cominterfacetype.interfaceisiunknown)] Public   InterfaceIObjectSafety {[PreserveSig]intGetInterfaceSafetyOptions (refGuid riid, [MarshalAs (UNMANAGEDTYPE.U4)]ref   intPdwsupportedoptions, [MarshalAs (UNMANAGEDTYPE.U4)]ref   intpdwenabledoptions); [PreserveSig ()]intSetInterfaceSafetyOptions (refGuid riid, [MarshalAs (UNMANAGEDTYPE.U4)]intDwoptionsetmask, [MarshalAs (UNMANAGEDTYPE.U4)]intdwenabledoptions); }} 

Then inherit and implement the interface in the control class:

#regionIObjectSafety membersPrivate   Const   string_iid_idispatch ="{00020400-0000-0000-c000-000000000046}" ;Private   Const   string_iid_idispatchex ="{A6EF9860-C720-11D0-9337-00A0C90DCAA9}" ;Private   Const   string_iid_ipersiststorage ="{0000010a-0000-0000-c000-000000000046}" ;Private   Const   string_iid_ipersiststream ="{00000109-0000-0000-c000-000000000046}" ;Private   Const   string_iid_ipersistpropertybag ="{37d84f60-42cb-11ce-8135-00aa004bb851}" ;Private   Const   intInterfacesafe_for_untrusted_caller =0x00000001 ;Private   Const   intInterfacesafe_for_untrusted_data =0x00000002 ;Private   Const   intS_OK =0 ;Private   Const   intE_fail =unchecked((int)0x80004005 );Private   Const   intE_nointerface =unchecked((int)0x80004002 );Private   BOOL_fsafeforscripting =true ;Private   BOOL_fsafeforinitializing =true ; Public   intGetInterfaceSafetyOptions (refGuid riid,ref   intPdwsupportedoptions,ref   intpdwenabledoptions) {     intRSLT =E_fail; stringstrGUID = riid. ToString ("B" ); Pdwsupportedoptions= Interfacesafe_for_untrusted_caller |Interfacesafe_for_untrusted_data; Switch(strguid) { Case_iid_idispatch: Case_iid_idispatchex:rslt=S_OK; Pdwenabledoptions=0 ; if(_fsafeforscripting = =true) Pdwenabledoptions=Interfacesafe_for_untrusted_caller;  Break ;  Case_iid_ipersiststorage: Case_iid_ipersiststream: Case_iid_ipersistpropertybag:rslt=S_OK; Pdwenabledoptions=0 ; if(_fsafeforinitializing = =true) Pdwenabledoptions=Interfacesafe_for_untrusted_data;  Break ; default: Rslt=E_nointerface;  Break ; }     returnRslt;} Public   intSetInterfaceSafetyOptions (refGuid riid,intDwoptionsetmask,intdwenabledoptions) {     intRSLT =E_fail; stringstrGUID = riid. ToString ("B" ); Switch(strguid) { Case_iid_idispatch: Case_iid_idispatchex:if(((dwenabledoptions & dwoptionsetmask) = = Interfacesafe_for_untrusted_caller) && (_fsafeforscripting = =true)) Rslt=S_OK;  Break ;  Case_iid_ipersiststorage: Case_iid_ipersiststream: Case_iid_ipersistpropertybag:if(((dwenabledoptions & dwoptionsetmask) = = Interfacesafe_for_untrusted_data) && (_fsafeforinitializing = =true)) Rslt=S_OK;  Break ; default: Rslt=E_nointerface;  Break ; }returnRslt;}#endregion 

In this way, an ActiveX control is developed for completion.

3.2.ActiveX Control Deployment

ActiveX controls can be deployed using the installation project of Visual Studio 2005.

This is almost identical to the deployment of a normal Windows Form application, with only one place to note, the user control project that you created earlier as the primary output project, and set its Register property to vsdrpCOM, as shown in:

3.3. Testing

Build a Web Application project, add a reference to the ActiveX control in the HTML code of the test page, and invoke the public members of the control through JavaScript (note that the value behind the CLSID here is the GUID previously set for the user control class):

< object id = "Csharpactivex" classid = "clsid:e5e0446c-8680-4444-9fc2-f837bc617ed9" ></object > < input ty PE = "button" onclick = "alert (Csharpactivex.sayhello ());" VALUE = "Show Current Time"/>

Publish the Web application project to IIS. Also find a computer as a client test environment, make sure it is connected to the server-side network, and install the. NET Framework 2.0 and the ActiveX control.

Once the installation is complete, you can use your browser to access the server and test it (you can also install the ActiveX control in your development environment and run the WebApp project directly in VS 2005 to see the results):

4. Summary

In summary, using C # to develop ActiveX controls in a visual Studio 2005 environment is not difficult to implement, and the only problem is that the client needs to install the. NET Framework.

Since ActiveX controls are generally implemented with some simple, single functionality, the. NET Framework 2.0 is fully manageable, so it is recommended that you develop under the. NET Framework 2.0.

As compared to the. NET Framework 3.5 200 multi-Gigabit installation package, the. NET Framework 2.0 installation package is more than 20 megabytes, which is relatively easy for users to accept.

5. FAQ 5.1. How do I fix the following error?

This issue is a bug in Visual Studio 2005 and is not always happening on the web.

My solution is to copy Regcap.exe from the installation directory of Visual Studio 2008 and overwrite the corresponding files in Visual Studio 2005.

The file directory is typically "~\microsoft Visual Studio 8\common7\tools\deployment egcap.exe".

The visual Studio 2008 version of the file is available in the compressed package.

Go C # Developing ActiveX controls,. NET developing OCX control cases

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.