VC + + How to register and unload the OCX Control (code) in the program with code

Source: Internet
Author: User

  • method One:Run under DOS or Windows command line: regsvr32 ocxname.ocx Register

    Example:
    regsvr32 netshare.ocx//Register Netshare.ocx controls
    regsvr32/u netshare.ocx//de-Registration of Netshare.ocx controls

    If the Netshare.ocx file is not in the system directory, you must also include the full path in front of the file name. In addition to the Regsvr32 application, Microsoft also provides a program called RegClean.exe on its web site that can examine the registry and remove entries from the missing components in the registry. RegClean.exe in addition to removing the "hanging" entry, it also provides an "undo" file to help restore the deletion of the error.

    Registering ActiveX controls with Regsvr32.exe is simple, but requires manual registration by the user, and manual de-registration when not in use.

    Method Two:With the install shield, in the inside select "Self-registered", the installation program will be automatically registered;


    Method Three:API function Registration

    This approach is designed by loading the ActiveX control into memory, verifying its validity, and finally executing its registration function (DllRegisterServer, DllUnregisterServer) directly in the memory address.
    The programming method is:

    1. Use Windows API function LoadLibrary to load ActiveX controls;
    2. Use the GetProcAddress function to get the registration function DllRegisterServer (logoff function is DllUnregisterServer) pointer in the ActiveX control;
    3. Use the CallWindowProc function to execute an ActiveX control registration function (DllRegisterServer, DllUnregisterServer) loaded into memory.

    Example:

    typedef int (callback* myfunction) (void);
    MyFunction myfunction = null;
    HINSTANCE handle = NULL;
    Handle = LoadLibrary ("Trdragdroptreectrl.ocx");

    if (handle! = NULL)
    {
    MyFunction = GetProcAddress (handle, "DllRegisterServer");
    if (myfunction! = null)
    {
    MyFunction ();
    }
    }

    Small Experience:
    1, sometimes in the program to use the OCX control failure, check whether the call between the execution of AfxOleInit (). If a container is used, you also need to ensure that AfxEnableControlContainer () is executed.
    2. Using the ActiveX Control Test Container can easily test the produced OCX (from the Start menu-"program-" Microsoft Visual Studio 6.0, Microsoft Visual Studi o 6.0 tools to see the tool)
    3, sometimes control production is not a problem, the registration method is correct, but the registration of the time did not fail, possibly the version of the DLL is incorrect, use dependencies to see if the OCX control depends on the DLL version of the DLL is the same as now used.

    ////////////////////////////////////////////////////////////////////////////////////////////////////

    Transferred from: http://www.cctry.com/thread-2418-1-1.html

    An ActiveX control is a dynamic-link library that acts as a COM-based server and can be embedded in a package container host application, where the ActiveX control is formerly the OLE control. Because ActiveX controls are independent of the development platform, ActiveX controls that are developed in one programming language can be used in another programming language without any modifications, as they do with Windows Universal controls. such as: in VC + + developed ActiveX control, do not make any changes can be applied to VB. This shows that the use of ActiveX controls enables rapid and small component reuse, code sharing, which improves programming efficiency.

    What should users do to properly use ActiveX controls downloaded from the Internet, or ActiveX controls obtained from other sources? In general, if a foreign ActiveX control is to be used correctly in Windows, the control file (*.ocx) must first be copied to the hard disk, then registered in Windows, and ActiveX controls that are not registered in Windows are not available. Registering ActiveX controls in general there are three ways, the scope of application is different, each has its advantages, the following are discussed separately.

       I. Registering an ActiveX control with the Regsvr32.exe program

    ActiveX controls that have not been registered can be registered with this software, and foreign ActiveX controls must be registered to be applied to their own programs. The file is located under the System subdirectory of the Windows directory. Here's how: click "Start" → "Run" and enter the following command in the Run dialog box:
    regsvr32 < file name > register an ActiveX control
    regsvr32/u< file name > de-Registration of an ActiveX control
    When we use some applications with ActiveX controls, there are times when it's not possible to run a situation where the ActiveX control is not registered, so try using the above command. In addition, VC + + users who are interested in Regsvr32.exe coding, can be in the VC + + online Help to find its source code. If VC ++6.0 is used, it is in the/sample/vc98/mfc/controls/regsvr of the MSDN CD.

       second, the use of the installation program

    Production software: InstallShield

    Using Regsvr32.exe to register ActiveX controls is simple, but requires manual registration by the user, and manual de-registration when not in use, so this is not a good solution for an application. Large applications typically have a setup program that resolves ActiveX control registrations in the installer. Using InstallShield, you can create professional-grade setups and register ActiveX controls in them, and automatically log out of previously registered ActiveX controls when you load the software later. Here's how:
    Start InstallShield and use the project Wizard to build a new project;
    Create a new "file group" to put the ActiveX control files that need to be registered in this "file group";
    Set the "Self-registered" property of the "File Group" above to "Yes".
    The above steps are only a few steps to set up automatic registration of ActiveX controls in the authoring setup, as for how to use InstallShield to make a complete setup outside the scope of this article, please refer to the books.

       Third, add the registration code in the application

    For small programs not to take the second method, a better way is to embed the registration code in the program, implementation of application self-registration. The programming method is:

    1. Use Windows API function LoadLibrary to load ActiveX controls;
    2. Use the GetProcAddress function to get the registration function DllRegisterServer (logoff function is DllUnregisterServer) pointer in the ActiveX control;
    3. Call the registration function DllRegisterServer (or unregister function DllUnregisterServer).

    The following is an example of a Regactivex program written by VC ++6.0, which describes how to automatically register (unregister) ActiveX controls in a program. The main frame of Regactivex is automatically generated by VC ++6.0 AppWizard, which adds two menu items: "Register ActiveX Control", "Unregister ActiveX control". When the Register ActiveX controls menu is selected, the control Toweratl.ocx (a Sino-Connaught Game) is registered and the registration of the control Toweratl.ocx is dismissed when the Unregister ActiveX Control menu is selected.

    1, using VC ++6.0 to establish a single-document application Regactivex, other projects accept the default settings;
    2. Add two menu items to the application: "Register ActiveX Controls", "Unregister ActiveX Controls", and follow these steps:
    A, click "Resoureview" in the left window of VC ++6.0, double-click the Idr_mainframe under the menu item to open the Menus editor;
    B, in the menu editor, add a main Menu "register", under the "Registration" main Menu, add two menu items: "Register ActiveX Control", "Unregister ActiveX control";
    3. Add the response function to the new menu item "Register ActiveX control" and "Unregister ActiveX control":
    b, in the Resource Editor, double-click Idr_mainframe in menu to open the Menus editor, click Register ActiveX controls, and press CTRL +W to open the MFC Class Wizard;
    C, double-click the command in the Messages box, add the response function Onregisterreg () to the Register ActiveX controls menu item, and click the right "Edit" button to add the following code to the Onregisterreg () function:
    void Cmainframe::onregisterreg ()
    {
    The path and file name of the ActiveX control
    LPCTSTR pszdllname= "Toweratl.ocx";
    Loading ActiveX controls
    HINSTANCE hlib = LoadLibrary (pszdllname);
    if (Hlib < (HINSTANCE) hinstance_error)
    {
    MessageBox ("Cannot load DLL file!",
    "Result", MB_OK);
    Return
    }
    Get Registration function DllRegisterServer address
    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 ("Registration Successful", "result", MB_OK);
    }
    Else
    MessageBox ("Call DllRegisterServer failed!") ",
    "Result", MB_OK);
    }

    C, the "Logout ActiveX Control" menu item response function is written in the same way, the code is similar, just will "Lpdllentrypoint=getprocaddress (hlib,_t (" DllRegisterServer ")); Change to:
    Lpdllentrypoint = GetProcAddress (hlib,_t
    ("DllUnregisterServer")).
    4, compile and join the above program, you can get an executable file. Also note that you should copy the ActiveX control file Toweratl.ocx to the Windows system directory. The control file Toweratl.ocx can be found in the CD attached to the Visual C ++6.0 Programmer's Guide published by Microsoft.
    5. Test the above procedure: Run the GAME2 program on the CD attached to the Visual C ++6.0 Programmer's Guide, if you use Regactivex to register the Toweratl.ocx control, the program can run normally; if you use Regactivex Game2 cannot run if the Toweratl.ocx control is unregistered.

VC + + How to register and unload the OCX Control (code) in the program with code

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.