How to register ActiveX controls in Windows-programming implementation

Source: Internet
Author: User
Since Microsoft launched ActiveX technology in 1996, ActiveX technology has received support and response from many software companies and has incorporated ActiveX technology into its products. ActiveX controls, as one of ActiveX technologies, have also developed rapidly. Not only have major software companies launched a number of ActiveX controls, but ActiveX controls have become a favorite of developers. So what is ActiveX Control? ActiveX control is a dynamic link library that operates on the COM server and can be embedded in the package container Host application. Program The predecessor of ActiveX control Is ole control. Because ActiveX controls are irrelevant to the development platform Programming Language ActiveX controls developed on can be used in another programming language without any modification. The effect is the same as that of Windows universal controls. For example, ActiveX controls developed in VC ++ can be applied to VB without any modification. It can be seen that using ActiveX controls can achieve rapid small Component Reuse and implementation Code Sharing to improve programming efficiency. It can also be seen from the above why ActiveX controls are so widely welcomed by developers.

The reusability of ActiveX controls, coupled with the popularity of the Internet, has greatly improved ActiveX controls. Currently, a considerable number of ActiveX controls can be obtained from the Internet, with a wide variety, the tasks are almost all-encompassing. For general developers, ActiveX controls that meet their requirements can be found on the Internet with patience.

What can I do to use ActiveX controls downloaded from the internet or ActiveX controls obtained from other sources to correctly use them or apply them to my own programs? Generally, to use a foreign ActiveX control correctly in Windows, you must first set the control file (*. OCX) to the hard disk, and then register it in windows. ActiveX controls that have not been registered in Windows cannot be used. Generally, there are three ways to register ActiveX controls:

1. Use the regsvr32.exe program to register ActiveX controls;

2. Use the installer to create the software: InstallShield;

3. Add the registration code to the application.

The above methods have different applicability and have different advantages. We will discuss them separately.

1. Use the regsvr32.exe program to register ActiveX Controls

You can use this software to register unregistered ActiveX controls. Foreign ActiveX controls must also be registered when applied to their own programs. The file is located in the system subdirectory of the Windows directory. Use the following command: Click Start> Run. In the run dialog box, enter the following command:

Regsvr32 <File Name> registers an ActiveX Control
Regsvr32/u <File Name> unregister an ActiveX Control
Some applications with ActiveX controls may sometimes fail to run. This may be because their built-in ActiveX controls are not registered. In this case, use the preceding command to try. In addition, if you are interested in regsvr32.exe encoding, you can find it in the online help of VC ++.Source code. If VC ++ 6.0 is used, go to the samplevc98mfccontrols egsvr on the msdn disc.
Ii. Use the installer to create the software: InstallShield

Although it is simple to use regsvr32.exe to register ActiveX controls, you must manually register them and manually unregister them when they are not in use. Therefore, this is not a good solution for an application. Large applications generally have an installer. It is an ideal solution to solve ActiveX Control registration in the installer. You can use InstallShield to create Professional-level setup and register ActiveX controls. In addition, ActiveX controls registered earlier are automatically deregistered when software is carried in the future. The method is as follows:

1. Start InstallShield and Use Project Wizard to create a new project;

2. Create a new "file group" and put the ActiveX control file to be registered into this "file group;

3. Set the "self-registered" attribute of the above "file group" to "yes ".

The above steps are only a few steps to set up automatic registration of ActiveX controls in setup. As to how to create a complete setup Using InstallShield, this article does not cover the scope. Please refer to the relevant books.

3. Add registration code to the application

The second method is not suitable for small programs. A better method is to embed registration code in the program to implement self-registration of the application. The programming method is as follows:

1. Use the Windows API function loadlibrary to load ActiveX controls;

2. Use the getprocaddress function to obtain the dllregisterserver (the deregistering function is dllunregisterserver) pointer in the ActiveX control;

3. Call the registration function dllregisterserver (or deregister the function dllunregisterserver ).

The following uses the regactivex program compiled in VC ++ 6.0 as an example to describe how to automatically register (deregister) the ActiveX control in the program. The main framework of regactivex is automatically generated by the Appwizard of VC ++ 6.0. Based on the framework, two menu items are added: "register ActiveX control" and "deregister ActiveX control ". When you select the "register ActiveX Control" menu, the control toweratl. OCX (A Hanno game) is registered. When you select the "deregister ActiveX Control" menu, the toweratl control is removed. OCX registration.

1. Create a single-document application regactivex using VC ++ 6.0, and accept the default settings for other projects;

2. Add two menu items to the application: "register ActiveX control" and "deregister ActiveX Control". The steps are as follows:

① Click "resoureview" in the left-side window of VC ++ 6.0, and double-click idr_mainframe under the menu project to open the menu editor;

② In the menu Editor, add a Main Menu "register" and add two menu items under the "register" Main Menu: "register ActiveX control" and "deregister ActiveX control ";

3. Add a response function to the newly created menu items "register ActiveX control" and "deregister ActiveX Control:

① In resource editing, double-click idr_mainframe in the menu to open the menu Editor, click Register ActiveX control, and press Ctrl + W to open the MFC class wizard;

② Double-click the command in the messages box, add the response function onregisterreg () to the "register ActiveX Control" menu item, and click the "edit" button on the right to add the following code to the onregisterreg () function:

Void cmainframe: onregisterreg ()
{
// ActiveX control path and file name
Lpctstr pszdllname = "toweratl. ocx ";
// Load ActiveX Controls
Hinstance hlib = loadlibrary (pszdllname );
If (hlib <(hinstance) hinstance_error)
{
MessageBox ("cannot load DLL files! "," Result ", mb_ OK );
Return;
}
// Obtain the address of the registration function dllregisterserver
Farproc lpdllentrypoint;
Lpdllentrypoint = getprocaddress
(Hlib, _ T ("dllregisterserver "));
// Call the registration function dllregisterserver
If (lpdllentrypoint! = NULL)
{
If (failed (* lpdllentrypoint )()))
{
MessageBox ("Call dllregisterserver
Failed! "," Result ", mb_ OK );
Freelibrary (hlib );
Return;
};
MessageBox ("registered successfully", "result", mb_ OK );
}
Else
MessageBox ("failed to call dllregisterserver! "
, "Result", mb_ OK );
}
③ The method for compiling the response function of the menu item "deregister ActiveX control" is the same as above, and the code is similar.
"Lpdllentrypoint = getprocaddress
(Hlib, _ T ("dllregisterserver"); ":
Lpdllentrypoint = getprocaddress
(Hlib, _ T ("dllunregisterserver ")).
4. Compile and join the above program to get an executable file. Note that the ActiveX control file toweratl. ocx should be copied to the Windows System directory. The toweratl. ocx control file can be found on the CD attached to the Visual C ++ 6.0 programmer guide published by Microsoft.
5. test the above program: run the game2 program in the CD attached to Visual C ++ 6.0 programmer guide. If toweratl is registered using regactivex. OCX control, the program can run normally; If regactivex is used to cancel toweratl. the OCX control cannot run game2.

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.