Use C # To develop ActiveX controls,

Source: Internet
Author: User

Use C # To develop ActiveX controls,
0. Preface

ActiveX controls were previously called OLE controls or OCX controls. They are software components or objects that can be inserted into WEB pages or other applications. By using ActiveX plug-ins, you can easily insert multimedia effects, interactive objects, and complex programs into web pages.

C ++ or VB is usually used to develop ActiveX controls. This article describes how to develop ActiveX controls using C # in Visual Studio 2005.

1. Problem scenarios

In a C/S architecture system, the client can easily implement certain business functions by installing related application sets. It is difficult to implement the same requirements in a B/S architecture system. Because all programs are placed on the server side, the client only uses a browser to access the server through the HTTP protocol. The mature solution is to develop ActiveX controls and install them on the client, so that the browser of the client can access the local ActiveX controls to perform related local operations. This article will talk about how to use C # To develop an ActiveX control to read and display the system time of the client.

2. Development Environment
  • Windows XP
  • Visual Studio 2005
  • . NET frameworks 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 develop ActiveX controls, but you need to make some necessary settings for the project. The following describes how to use the Windows control library project to develop an ActiveX control. First, create an application solution and add a Windows control library project:

 

 

Change the "Project properties-Application-assembly information" Settings, and select "make assembly COM visible ":

 

 

Change the "Project properties-generate" setting and select "register for COM Interop" (Note: if the configuration is changed in debug state, you need to set it again when it is changed to release state):

 

 

Modify the AssemblyInfo. cs file and add the [assembly: AllowPartiallyTrustedCallers ()] item (System. Security namespace must be referenced ):

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©10BAR2009 ")]
[Assembly: AssemblyTrademark ("")]
[Assembly: AssemblyCulture ("")]
[Assembly: AllowPartiallyTrustedCallers ()]
[Assembly: ComVisible (true)]
[Assembly: Guid ("114d1f0c-43b8-40ac-ae7c-5adccc19aef3")]
[Assembly: AssemblyVersion ("1.0.0.0")]
[Assembly: AssemblyFileVersion ("1.0.0.0")]

Add a Windows user control:

 

Follow the same idea of developing a Windows user control to develop the control. In this example, two business functions are implemented. One is to provide a public method to read the signature certificate saved in the USB key, save it to the local C-drive root directory and return operation information. Another business function provides a UI, including a Button control and a Label control, the Click event of the Button control calls the method provided earlier and displays the returned information on the Label control. This can achieve two goals: first, ActiveX controls provide public methods for direct calls by B/S programs, and then implement business functions; second, activeX control can provide the UI of the B/S program, and implement business functions by responding to the UI operation events in the B/S program.

After the control is developed, you must make the following changes to make the user control used as an ActiveX control:
First, add a GUID to the control class, which will be used for client calls of the B/S system (you can use the tool-create GUID menu to create a GUID ):

Guid ("4A44CF4E-F859-4328-AA22-3E9D7AFFF1AB")]
PublicpartialclassHello: UserControl
{

Second, to allow ActiveX controls to gain client trust, the control class also needs to implement an interface named "IObjectSafety. Create the interface first (note that the GUID value of this 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)]
PublicinterfaceIObjectSafety
{
[PreserveSig]
IntGetInterfaceSafetyOptions (refGuidriid, [financialas (UnmanagedType. U4)] refintpdwSupportedOptions, [externalas (UnmanagedType. U4)] refintpdwEnabledOptions );

[PreserveSig ()]
IntSetInterfaceSafetyOptions (refGuidriid, [financialas (UnmanagedType. U4)] intdwOptionSetMask, [externalas (UnmanagedType. U4)] intdwEnabledOptions );
}
}

Then inherit and implement this interface in the control class:

# RegionIObjectSafety Member

Privateconststring_IID_IDispatch = "{00020400-0000-0000-C000-000000000046 }";
Privateconststring_IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9 }";
Privateconststring_IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046 }";
Privateconststring_IID_IPersistStream = "{00000109-0000-0000-C000-000000000046 }";
Privateconststring_IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851 }";

PrivateconstintINTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
PrivateconstintINTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
PrivateconstintS_ OK = 0;
PrivateconstintE_FAIL = unchecked (int) 0x80004005 );
PrivateconstintE_NOINTERFACE = unchecked (int) 0x80004002 );

Privatebool_fSafeForScripting = true;
Privatebool_fSafeForInitializing = true;

PublicintGetInterfaceSafetyOptions (refGuidriid, refintpdwSupportedOptions, refintpdwEnabledOptions)
{
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;
}

PublicintSetInterfaceSafetyOptions (refGuidriid, 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.

3.2.ActiveX control deployment

ActiveX controls can be deployed using the installation project of Visual Studio 2005. This is almost the same as the deployment of common Windows Form applications. You must note that the previously created user control project is used as the main output project and its Register attribute is set to vsdrpCOM, as shown in:

 

 

3. Test

Create a Web application project and add reference to ActiveX control to the HTML code on the test page, you can also use Javascript to call the Public Members of the control (note that the value after clsid is the GUID set for the user control class ):

<Objectid = "csharpActiveX" classid = "clsid: E5E0446C-8680-4444-9FC2-F837BC617ED9"> </object>
<Inputtype = "button" onclick = "alert (csharpActiveX. SayHello ();" value = "display current time"/>

Publish the Web application project to IIS. In addition, find a computer as the client test environment, make sure it is connected to the server network, and install. NET Framework 2.0 and the ActiveX control. After the installation is complete, you can access the server in a browser and perform a test (you can also install the ActiveX control in the system of the development environment, and directly run the WebApp project in VS 2005 to view the results ):

 

 

4. Summary

To sum up, using C # To develop ActiveX controls in Visual Studio 2005 is not technically difficult. The only problem is that the client needs to install. NET Framework. Since ActiveX Controls generally implement some simple and single functions,. NET Framework 2.0 is ready for use. Therefore, we recommend that you develop ActiveX controls under. NET Framework 2.0. Compared with the. NET Framework 3.5 multi-MB installation package, the. NET Framework two hundred installation package only has more than 20 MB, which is relatively easy to accept.

5. FAQ5.1. how can I solve the following error?

 

 

After checking on the Internet, this issue is a Bug in Visual Studio 2005, not every time. My solution is to overwrite the corresponding file of Visual Studio 2005 from the installation directory of Visual Studio 2008. the file directory is usually "~ \ Microsoft Visual Studio 8 \ Common7 \ Tools \ Deployment \ regcap.exe ". The compressed file provides Visual Studio 2008.

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.