The ActiveX control is automatically registered when the program is running.

Source: Internet
Author: User

When compiling Delphi applications, ActiveX controls are often encountered [Note: OCX controls or DLL-type libraries], which provide a simple operation path for application development. However, these programs are faced with ActiveX Control registration issues while being released.

Solution: 1. Using the installer to create a package file will usually increase the size of the program itself. When the system reinstalls the package, it must be re-installed; 2. Use the program itself to Write Functions in the form of code.

There are two methods:

1. directly put the visualized ActiveX control into the program;

2. Real-Time Creation as needed during runtime.

If it is directly used, the application will automatically find and create the required ActiveX Control during initialization. If the control is not registered, the initialization program will generate an exception, capture and handle this exception.

Add a new method to the form program:

Unituautoregactivex;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, comobj; // Add comobj Unit
Type
Tautoregactivexfrm = Class (tform)
Procedureformcreate (Sender: tobject );
Private
{Privatedeclarations}
Public
{Publicdeclarations}
Protected
{Publicdeclarations}
Procedurecheckexception (Sender: tobject; eabort: exception );
End;
VaR
Autoregactivexfrm: tautoregactivexfrm;
Implementation
{$ R *. DFM}

{-------------------------------------------------

Standard ActiveX controls have two functions: dllregisterserver and dllunregisterserver. functions are used to register controls and uninstall controls. We can use loadlibrary to load dll/uninstall to register and uninstall ActiveX controls.

--------------------------------------------------}

{-------------------------------------------------

Parameter description:

Solefilename: a DLL or ocx file name;

Oleaction indicates the registration operation type: 1 indicates registration, 0 indicates uninstall

Return Value: True indicates that the operation is successful. False indicates that the operation failed.

--------------------------------------------------}

Functionoleregister (solefilename: string; oleaction: byte): Boolean;
Const
Registerole = 1; // register
Unregisterole = 0; // uninstall
Type
Toleregisterfunction = function: hresult; // register or uninstall the function prototype.
VaR
Hlibraryhandle: thandle; // DLL or OCX handle returned by loadlibray
Hfunctionaddress: tfarproc; // function handle in DLL or ocx, Which is returned by getproaddress
Regfunction: toleregisterfunction; // register or uninstall the function pointer
Begin
Result: = false;
// Open the file and return the DLL or OCX handle
Hlibraryhandle: = loadlibrary (pchar (solefilename ));
If (hlibraryhandle> 0) Then // The dllakgocx handle is correct
Try
// Return the registration or uninstallation function pointer
If (oleaction = registerole) Then // return the registration function pointer
Hfunctionaddress: = getprocaddress (hlibraryhandle, pchar ('dlregisterserver '))
Else // returns the unmount function pointer
Hfunctionaddress: = getprocaddress (hlibraryhandle, pchar ('dllunregisterserver '));
If (hfunctionaddress <> nil) Then // determines whether the registration or uninstallation function exists
Begin
Regfunction: = toleregisterfunction (hfunctionaddress); // gets the pointer of the Operation Function
Ifregfunction> = 0 then // execute the registration or uninstall operation. The return value> = 0 indicates that the execution is successful.
Result: = true;
End;
Finally
Freelibrary (hlibraryhandle); // close an opened file
End;
End;
{Tautoregactivexfrm}
Proceduretautoregactivexfrm. checkexception (Sender: tobject;
Eabort: exception );
Begin
Ifeabortiseolesyserrorthen
Begin
Ifhresult (eolesyserror (eabort). errorcode) = regdb_e_classnotregthen
Oleregister ('d: Flash. ocx ', 1 );
End
Else
Application. showexception (eabort );
End;
// Assign the checkexception Method to the system application variable in the oncreate event of the main form.
Proceduretautoregactivexfrm. formcreate (Sender: tobject );
VaR
Demoocx: variant; // variable Declaration
Begin
Application. onexception: = checkexception;
// Whether a class name string error is generated
Try
Demoocx: = createoleobject ('demo. demo ');
Except
Oneabort: eolesyserrordo
Ifhresult (eabort. errorcode) = co_e_classstringthen
Begin
Ifoleregister ('d: Flash. ocx ', 1) then
Demoocx: = createoleobject ('demo. demo ')
Else
Begin
Application. MessageBox ('control registration failed, the program will not run properly ', pchar ('register control'), mb_ OK + mb_iconerror );
Application. Terminate;
End;
End;
End;
End;
End.

 

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.