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;
type
 TRegFunc = function : HResult; stdcall;
var
 ARegFunc : TRegFunc;
 aHandle : THandle;
 ocxPath : string;
begin
 try
 ocxPath := ExtractFilePath(Application.ExeName) + 'Flash.ocx';
 aHandle := LoadLibrary(PChar(ocxPath));
 if aHandle 0 then
 begin
 ARegFunc := GetProcAddress(aHandle,'DllRegisterServer');
 if Assigned(ARegFunc) then
 begin
 ExecAndWait('regsvr32','/s ' + ocxPath);
 end;
 FreeLibrary(aHandle);
 end;
 except
 ShowMessage(Format('Unable to register %s', [ocxPath]));
 end;
end;

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;
var
 SEInfo: TShellExecuteInfo;
 ExitCode: DWORD;
begin
 FillChar(SEInfo, SizeOf(SEInfo), 0);
 SEInfo.cbSize := SizeOf(TShellExecuteInfo);
 with SEInfo do begin
 fMask := SEE_MASK_NOCLOSEPROCESS;
 Wnd := Application.Handle;
 lpFile := PChar(ExecuteFile);
 lpParameters := PChar(ParamString);
 nShow := SW_HIDE;
 end;
 if ShellExecuteEx(@SEInfo) then
 begin
 repeat
 Application.ProcessMessages;
 GetExitCodeProcess(SEInfo.hProcess, ExitCode);
 until (ExitCode STILL_ACTIVE) or Application.Terminated;
 Result:=True;
 end
 else Result:=False;
end;

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.

Format
mla apa chicago
Your Citation
Gajic, Zarko. "Register DLL and ActiveX Controls From a Delphi Application." ThoughtCo, Sep. 8, 2017, thoughtco.com/register-dll-and-activex-controls-from-a-delphi-application-1058233. Gajic, Zarko. (2017, September 8). Register DLL and ActiveX Controls From a Delphi Application. Retrieved from https://www.thoughtco.com/register-dll-and-activex-controls-from-a-delphi-application-1058233 Gajic, Zarko. "Register DLL and ActiveX Controls From a Delphi Application." ThoughtCo. https://www.thoughtco.com/register-dll-and-activex-controls-from-a-delphi-application-1058233 (accessed January 22, 2018).