Solution: 1. Using the installer to create a package file will often increase the size of the program itself. When the system is reinstalled, it must be reinstalled; 2. Use the program itself to write functions to process it.
There are two ways to use it:
1. Directly put the visual ActiveX control into the program;
2. The runtime is established in real time as needed.
If it is used directly, the application will automatically search and create the required ActiveX control during the initialization process. If the control is not registered, the initializer will generate an exception, catching and handling the exception.
Add a new method to the program Form:
unit UAutoRegActiveX;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComObj; //Add to the 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 standard ActiveX controls, there are two functions DLLRegisterServer and DLLUnRegisterServer that can be called, where the function is used to register the control and uninstall the control. We can use LoadLibrary to load DLL/OCX files, use GetProcAddress to get pointers to DLLRegisterServer and DLLUnRegisterServer, and then run these two functions directly to implement the operation of registering and uninstalling ActiveX controls, thereby replacing the Windows system's RegSvr32.exe to implement ActiveX Register and uninstall the control.
-------------------------------------------------- }
{-------------------------------------------------
Parameter description:
sOleFileName A DLL or OCX file name;
OleAction indicates the registration operation type: 1 indicates registration, 0 indicates uninstall
Return value: True means the operation is executed successfully, False means the operation is executed failed
-------------------------------------------------- }
function OLERegister(sOleFileName: String; OleAction: Byte):Boolean;
const
RegisterOle = 1; //Register
UnRegisterOle = 0; //Uninstall
type
TOleRegisterFunction = function: HResult; //Register or uninstall function prototype
var
hLibraryHandle: THandle; //DLL or OCX handle returned by LoadLibray
hFunctionAddress: TFarProc; // Function handle in DLL or OCX, returned by GetProAddress
RegFunction: TOleRegisterFunction; //Register or uninstall function pointer
Begin
Result := False;
//Open the file and return the DLL or OCX handle
hLibraryhandle := LoadLibrary(PChar(SOleFileName));
if (hLibraryHandle > 0) then //DLLakg OCX handle is correct
try
//Return to the registration or uninstall function pointer
if (OleAction = RegisterOle) then //Return the registered function pointer
hFunctionAddress := GetProcAddress(hLibraryhandle,PChar(''DLLRegisterServer''))
else //Return the uninstall function pointer
hFunctionAddress := GetProcAddress(hLibraryhandle,PChar(''DLLUnRegisterServer''));
if (hFunctionAddress <> nil) then //Judge whether the registration or uninstall function exists
Begin
RegFunction := TOleRegisterFunction(hFunctionAddress); //Get pointer to the operation function
if RegFunction >=0 then //Execute the registration or uninstall operation, the return value >=0 means the execution is successful
/tResult := True;
end;
Finally
FreeLibrary(hLibraryHandle); //Close the opened file
end;
end;
{ TAutoRegActiveXFrm }
procedure TAutoRegActiveXFrm.CheckException(Sender: TObject;
EAbort: Exception);
Begin
if EAbort is EOleSysError then
Begin
if HResult(EOleSysError(EAbort).ErrorCode) = REGDB_E_CLASSNOTREG then
OleRegister(''D:/Flash.ocx'',1);
end
else
Application.ShowException(EAbort);
end;
// Assign 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;
//Is the class name string error generated?
try
DemoOcx := CreateOleObject(''Demo.Demo'');
except
on EAbort:EOleSysError do
if HResult(EAbort.ErrorCode) = CO_E_CLASSSSTRING then
Begin
if OleRegister(''D:/Flash.ocx'',1) then
/tDemoOcx := CreateOleObject(''Demo.Demo'')
else
Begin
/tApplication.MessageBox(''Control registration failed, the program will not run normally'',PChar(''Register control''),MB_OK+MB_IConERROR);
/tApplication.Terminate;
end;
end;
end;
end;
end.