When writing a Delphi application, some ActiveX controls often encounter [Note: Extension of the OCX control or DLL type library], which provides a simple operational way for applications. However, these programs are posted on the issue of ActiveX controls.
Solution: First, use the installer to make packaging files, often give the increase in the size of the program itself, the system must reinstall it;
Use two types:
First, put the visualized ActiveX control in the program directly;
Second, the runtime is established in real time as needed.
If you are in use, the application will automatically find during initialization, create the required ActiveX control, if the control is not registered, the initializer generates an exception, captures and process this exception.
Add a new method in the program Form:
Unit uautoregactivex;
Interface
Uses
Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Comobj; // Join ComoBJ unit
Type
TautoregActiveXFRM = Class (TFORM)
Procedure formcreate (Sender: TOBJECT);
Private
{Private Declarations}
public
{Public declarations}
protected
{Public declarations}
Procedure Checkexception (Sender: Tobject; Eabort: Exception);
END;
VAR
AutoregActivexFRM: TautoregActivexFrm;
IMPLEMENTATION
{$ R * .dfm}
{-------------------------------------
In the standard ActiveX control, there are two functions DllregisterServer, DllunRegisterServer call, where functions are used to register controls for uninstall controls. We can load DLL / OCX files with loadLibrary, get DllRocaddress to get a pointer to two functions of DllRegisterServer, and then run these two functions directly to implement the operation of the ActiveX control, which replaces the REGSVR32.EXE for the Windows system. ActiveX Registration and uninstallation of the control.
-------------------------------------------------- }
{-------------------------------------
Parameter Description:
Solefilename A DLL or OCX file name;
OleAction means registration operation type: 1 means registration, 0 means uninstall
Return value: true indicates that the operation is executed, FALSE means the operation failed
-------------------------------------------------- }
Function OleRegister (solefilename: string; oleaction: byte): boolean
Const
Registerole = 1; // Register
Unregisterole = 0; // Uninstall
Type
ToleRegisterFunction = function: hresult; // Register or unload function prototype
VAR
HlibraryHandle: Thandle; // Returned by LoadLibray or OCX handle
HFunctionAddress: tfarProc; // DLL or OCX function handle, returned by getProaddress
Regfunction: toleregisterFunction; // Register or unload function pointer
Begin
Result: = FALSE;
// Open the file, return to the DLL or OCX handle
HlibraryHandle: = loadingLibrary (pchar (solefilename));
IF (HlibraryHandle> 0) THEN // DLLAKG OCX handle correct
Try
/ / Return to registration or unload function pointer
IF (OleAction = registerole) THEN // Return to the registration function pointer
HfunctionAddress: = getProcadDress (HlibraryHandle, Pchar ('DLLREGISTERSERVER')))
ELSE // Return to Unloading Functions Pointer
HfunctionAddress: = getProcadDress (HlibraryHandle, Pchar ('Dllunregisterserver');
IF (HFunctionAddress <> nil) THEN / / Judgment Registration or Uninstall Function
Begin
Regfunction: = ToleReGisterFunction (HFunctionAddress); // Get a pointer to the action function
If regfunction> = 0 THEN / / Perform registration or uninstall operation, return value> = 0 means execution success
RESULT: = true;
END;
Finally
Freelibrary (HLibraryHandle); // Close the open file
END;
END;
{Tautoregactivexfrm}
Procedure tautoregactivexfrm.checkexception (sender: TOBJECT)
Eabort: Exception;
Begin
IF eabort is eolesyserror dam
Begin
IF HRESULT (EolesySerror (eabort). ErrorCode = regdb_e_classnotreg the
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.
Procedure tautoregactivexfrm.formcreate (sender: TOBJECT);
VAR
Demoocx: Variant; // Variable declaration
Begin
Application.onexception: = Checkexception;
/ / Do you generate a class name string error
Try
Demoocx: = CreateoleObject ('Demo.demo');
Except
ON Eabort: EolesySerror DO
If HRESULT (Eabort.Errorcode) = CO_E_CLASSSTRING THEN
Begin
IF OLEGIster ('d: /flash.ocx' ,1) THEN
Demoocx: = CreateoleObject ('Demo.demo')
Else
Begin
Application.MessageBox ('control registration failed, the program will not function properly', PCHAR ('registration control'), MB_OK MB_ICONERROR); Application.Terminate;
END;
END;
END;
END;
End.