[ActiveX] ActiveX Web Control development for C + + [go]

Source: Internet
Author: User

This article will first show you how to use Visual Studio to develop a simple ActiveX control, and then describe the basics of ActiveX control development, such as methods, properties, and events, and finally how to use this knowledge to communicate between ActiveX controls and page pages.


of the Myactivex control

Brief introduction

ActiveX was a technology introduced by Microsoft in the middle of the 90. With ActiveX technology, developers can create small applications like Java applet applets. These applications can be downloaded and run in Microsoft's browser. This article is intended for Visual C + + developers who want to learn how to develop their first ActiveX control.

When I tried to learn these techniques myself, I found that many of the information about ActiveX technology was no longer applicable, outdated, or lost a lot of critical information, which made it difficult for me to create an ActiveX control for my project. This article will help you get started quickly with the development of ActiveX Web control. This article will first show you how to use Visual Studio to develop a simple ActiveX control, and then describe the basics of ActiveX control development, such as methods, properties, and events, and finally how to use this knowledge to communicate between ActiveX controls and page pages.

In addition, you will learn how to make your control a "safe" ActiveX control by canceling out warnings or error prompts in the control's loading process under the default Internet Explorer security settings.

In this article, we will create an actual ActiveX control. During the control load, it displays a dynamic progress bar that tells the user that the control is loading and processing information. At the same time, this control will show how to pass information between the ActiveX control and the Web page. This article will walk you through the development of ActiveX controls in Microsoft Visual Studio 2005.

To create an ActiveX control

To create an ActiveX control, start Visual Studio 2005 first, and perform the following steps:

1. Select the "File" menu and select "New", "project".

2. In the New Project dialog box, as shown in 1, select MFC under the Visual C + + category. Then select MFC ActiveX controls as the project template.

3. Name the new project "Myactivex" and select the project's save directory, then click the "OK" button to close the "New Project" dialog box and go to the MFC ActiveX Control Wizard.


Figure 1 New Project dialog box

4. In the MFC ActiveX Control Wizard, as shown in 2, select control settings on the left to enter the control parameter Settings page

5. Select "STATIC" in the "Create Control based on" drop-down list. This means that we are using a static control because we are simply displaying the output without receiving input.

6. In all the options for "Additional features" below, we confirm that the options "activates when visible" and "Flicker-free Activation" are selected, while confirming that the "has a about box Dialog "option is not selected.


Figure 2 MFC ActiveX Control Wizard dialog box

7. Click the "Finish" button to close the entire MFC ActiveX Control Wizard dialog box, and the wizard will create the source code for the entire project for us. By default, the wizard creates projects that use shared MFC DLLs, and here we need to change this setting. Because an MFC DLL is not installed on the target machine, the ActiveX control does not run. We often see that some of the ActiveX controls on the Web page are displayed as Red forks, many of which are caused by this reason.

To solve this problem, we use a static MFC DLL. From the menu in Visual Studio, choose Project, Properties, and then switch to configuration Properties, normal, and in the configuration page, in the use of MFC drop-down list, switch the application MFC in a shared DLL to A Static Library "is OK.

8. The wizard created the following three classes:
cmyactivexapp– This is an ActiveX control application class derived from COleControlModule. With this class, we can get an OLE control object that contains a member function that initializes the control (InitInstance) and cleans the control (exitinstance).
Cmyactivexctrl– This class is derived from COleControl. We will implement the main functionality of the control in this class.
Cmyactivexproppage– This class is derived from COlePropertyPage. It is used to manage the Properties dialog box for a control. The MFC ActiveX Control Wizard has created a default Properties dialog box for us, and we just need to modify it on its basis.

To create a Web page for an ActiveX control

It is not possible to finish the ActiveX control because the ActiveX control cannot run alone, so we also need to create the appropriate test page for the ActiveX control. If you want to create a default test page for our ActiveX controls, the best tool is the Microsoft ActiveX control Pad. This is a free tool that we can download from Microsoft's website.
We need to download the ActiveX Control pad and install it on our development machine. To simplify testing, we also need to install Microsoft's IIS server as a Web server on the same machine.

When we first run the ActiveX Control Pad, it will create a default HTML page. Next, we need to insert the ActiveX control into this page. Right-click somewhere within the <BODY> tab, choose Insert ActiveX Control, and in the Insert ActiveX Control dialog box that pops up, select the ActiveX control Myactivex that we just created, Then click "OK" to close the dialog box and insert the ActiveX control into the page.


Figure 5 Inserting an ActiveX control

The ActiveX Control Pad provides two dialog boxes that let us modify and edit the various properties of the ActiveX controls. The Properties dialog box is used to modify the properties of the ActiveX control. The Edit ActiveX Control dialog box is used to manually modify the ActiveX control. Of course, we can also directly edit the HTML code without using the dialog box to achieve the same effect. Find the object ID tag in your code, as shown in 6, and modify its size parameter to "width=350" and "height=50". When you have finished modifying the code, save the file as myactivex.htm and place it under the site root of the IIS server Wwwroot.


Figure 6 Editing the page in the ActiveX Control pad

Now, we can use IE to open http://localhost/myactivex.htm to test the ActiveX control. In the process of loading the ActiveX control, if you get some warning message, click "OK" to skip it. Under normal circumstances, you will see a progress bar gif animation displayed on the page. If you don't see an animation, or simply display a red fork in the location of the ActiveX control, this may be your browser's security settings, preventing the ActiveX control from loading and running. To resolve this issue, modify your browser's security settings so that all of its options about ActiveX are in the "allowed" state.


Figure 7 Myactivex control in the browser

Next, we'll make our control a "signed" or "safe" control to resolve the warning messages that are generated during the ActiveX control's loading process. Build a "signed" ActiveX control. To create a "signed" ActiveX control, you must obtain a code-signing certificate from a number of certification authorities, such as Thawte,verisign and GeoTrust, for example Signing Certificate. With this service, they will authenticate you and provide you with a certificate file for signing the ActiveX control. These certificate files for application signing include MYCERT.SPC and mykey.pvk. two files.

In order to sign an ActiveX application, we need to package all the components of this program into a CAB file. The CAB file will be downloaded from the Web site to the target machine, and the ActiveX control will be installed on the target machine via the download CAB file. To enable this feature, we must define the OleSelfRegister variable in the version information VERSIONINFO structure of the ActiveX control. This variable is automatically added to VS 2003 and its previous Visual Studio. However, in Visual Studio 2005, the Versioninfo struct does not contain this variable by default, and we need to add oleselfregister this variable manually by following the example:

Vs_version_info Versioninfo
FileVersion 1,0,0,1
ProductVersion 1,0,0,1
Fileflagsmask 0X3FL
#ifdef _DEBUG
FileFlags 0x1l
#else
FileFlags 0x0l
#endif
Fileos 0x4l
FILETYPE 0x2l
Filesubtype 0x0l
BEGIN
BLOCK "Stringfileinfo"
BEGIN
BLOCK "040904e4"
BEGIN
VALUE "CompanyName", "TODO: <company name>"
VALUE "FileDescription", "TODO: <file description>"
VALUE "FileVersion", "1.0.0.1"
VALUE "InternalName", "Myactivex.ocx"
VALUE "Legalcopyright",
"TODO: (c) <company Name>. All rights reserved. "
VALUE "OleSelfRegister", "+"
VALUE "OriginalFilename", "Myactivex.ocx"
VALUE "ProductName", "TODO: <product name>"
VALUE "ProductVersion", "1.0.0.1"
END
END
BLOCK "Varfileinfo"
BEGIN
VALUE "Translation", 0x409, 1252
END
END

Before signing an ActiveX control, the ActiveX control should be packaged as a CAB file. This CAB file also contains an INF file that will be used to install your ActiveX control. To create a CAB file, you need the Cabarc.exe in Microsoft Cabinet Software Development Kit. The following INF file demonstrates how to package our Myactivex control into a CAB file. For the CLSID, you need to change it to the same value as the object ID in the HTML page we created with the ActiveX Control pad.

[Add.Code]
Myactivex.ocx=myactivex.ocx
Myactivex.inf=myactivex.inf
[Myactivex.ocx]
File=thiscab
clsid={36299202-09ef-4abf-adb9-47c599dbe778}
Registerserver=yes
fileversion=1,0,0,0
[Myactivex.inf]
File=thiscab

We can create a CAB file with the following DOS command. It is important to note that the OCX files and INF files must be in the same directory as the Cabarc.exe you execute, otherwise the CAB files will not be decompressed properly after being downloaded. This is also an important cause of the ActiveX control being displayed as a red fork.
Cabarc-s 6144 N myactivex.cab myactivex.ocx myactivex.inf to sign the CAB file we just created, we need Microsoft MSDN to provide us with the tools Signcode.exe. (See resources at the end of this article related to "Signing and Checking with Authenticode"). With this tool, we can sign the CAB file with the code signing certificate that we obtained from the certification authority. Here is an example of using Signcode to sign Myactivex.cab:

Signcode-n "Myactivex"-I.
HTTP://WWW.MYACTIVEX.COM-SPC mycert.spc-v mykey.pvk-t Http://timestamp.verisign.com/scripts/timstamp.dll Myactivex.cab

In the example above, http://www.myactivex.com should be replaced with the description page of your ActiveX control, which allows you to provide more information about the control to the user.

To use the post-signed CAB file, first we need to copy the Myactivex.cab to a suitable directory on the Web server, then we modify the object ID tag in the Web page, add a parameter codebase point to the CAB file. The specific code is shown in 8. This way, when you use IE to open the page, the CAB file will be downloaded and the ActiveX control will be properly installed on your machine. During the installation, you will never be prompted for warning messages for unsigned ActiveX controls.

[ActiveX] ActiveX Web Control development for C + + [go]

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.