본문 바로가기
Delphi, RadStudio

[delphi] Registering DLL and ActiveX controls from code

by SB리치퍼슨 2011. 11. 28.
[delphi] Registering DLL and ActiveX controls from code

How to register (and unregister) OLE controls such as dynamic-link library (DLL) or ActiveX Controls (OCX) files from a Delphi application.

RegSvr32.exe
The regsvr32.exe command-line tool registers dll and ActiveX controls on a system. You can manually use the Regsvr32.exe (Windows.Start - Run) to register and unregister OLE controls such as dynamic link library (DLL) or ActiveX Controls (OCX) files that are self-registerable. 
When you use Regsvr32.exe, it attempts 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


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;






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;
반응형

댓글