Register DLL and ActiveX Controls From a Delphi Application

Putting in some overtime
Getty Images/Yuri_Arcurs

A popular feature of Delphi is the project deployment of an application with an executable file (exe).  However, if the DLL or ActiveX controls in your project are not registered on the users’ machines, an “EOleSysError” will be displayed in response to running the exe file. To avoid this, use the regsvr32.exe command-line tool.

RegSvr32.exe Command

Manually using regsvr32.exe (Windows.Start - Run) will register and unregister self-registerable  DLL and ActiveX controls on a system. Regsvr32.exe instructs the system to attempt to load the component and call its DLLSelfRegister function. If this attempt is successful, Regsvr32.exe displays a dialog indicating success.

RegSvr32.exe has the following command-line options: 

Regsvr32 [/u] [/s] [/n] [/i[:cmdline]] dllname
/s - Silent; display no message boxes
/u - Unregister server
/i - Call DllInstall passing it an optional [cmdline];
when used with /u calls dll uninstall
/n - do not call DllRegisterServer; this option must       be used with /i 

Call RegSvr32.exe Within Delphi code

To call the regsvr32 tool within Delphi code, use the “RegisterOCX” function to execute a file and wait for the execution to finish.

This is how the 'RegisterOCX' procedure could look:

procedure RegisterOCX;
TRegFunc = function : HResult; stdcall;
ARegFunc : TRegFunc;
aHandle : THandle;
ocxPath : string;
ocxPath := ExtractFilePath(Application.ExeName) + 'Flash.ocx';
aHandle := LoadLibrary(PChar(ocxPath));
if aHandle 0 then
ARegFunc := GetProcAddress(aHandle,'DllRegisterServer');
if Assigned(ARegFunc) then
ExecAndWait('regsvr32','/s ' + ocxPath);
ShowMessage(Format('Unable to register %s', [ocxPath]));

Note: the ocxPath variable points to the 'Flash.ocx' Macromedia OCX.

To be able to register itself, an OCX must implement the DllRegisterServer function to create registry entries for all the classes inside the control. Do not worry about the DllRegisterServer function, just make sure it is there. For the sake of simplicity, it is presumed that the OCX is located in the same folder as where the application is.

The ExecAndWait line in the above code calls the regsvr32 tool by passing the "/s" switch along with the full path to the OCX. The function is ExecAndWait.

uses shellapi;
function ExecAndWait(const ExecuteFile, ParamString : string): boolean;
SEInfo: TShellExecuteInfo;
ExitCode: DWORD;
FillChar(SEInfo, SizeOf(SEInfo), 0);
SEInfo.cbSize := SizeOf(TShellExecuteInfo);
with SEInfo do begin
Wnd := Application.Handle;
lpFile := PChar(ExecuteFile);
lpParameters := PChar(ParamString);
nShow := SW_HIDE;
if ShellExecuteEx(@SEInfo) then
GetExitCodeProcess(SEInfo.hProcess, ExitCode);
until (ExitCode STILL_ACTIVE) or Application.Terminated;
else Result:=False;

The ExecAndWait function uses the ShellExecuteEx API call to execute a file on a system. For more examples of executing any file from Delphi, check out how to execute and run applications and files from Delphi code.

Flash.ocx Inside Delphi Exe

If there is a need to register an ActiveX control on ​the user’s machine, then make sure the user has the OCX the program requires by placing the entire ActiveX (or DLL) inside the application’s exe as a resource. When the OCX is stored inside the exe, it is easy to extract, save to disk, and call the RegisterOCX procedure.

mla apa chicago
Your Citation
Gajic, Zarko. "Register DLL and ActiveX Controls From a Delphi Application." ThoughtCo, Aug. 27, 2020, Gajic, Zarko. (2020, August 27). Register DLL and ActiveX Controls From a Delphi Application. Retrieved from Gajic, Zarko. "Register DLL and ActiveX Controls From a Delphi Application." ThoughtCo. (accessed January 22, 2021).