delphi - 如何在 Delphi XE4 中注册 Active X .ocx 库

标签 delphi activex delphi-xe4

我正在尝试在 Delphi 程序中注册 Active X .ocx 库,我尝试了以下代码,但没有成功,没有错误,并且程序运行了所有代码,但是当它完成时,Active X 库还没有运行已注册。我究竟做错了什么 ?

    procedure RegisterOCX;
    type
      TRegFunc = function : HResult; stdcall;

    var
      ARegFunc : TRegFunc;
      aHandle  : THandle;
      ocxPath,AppPath  : string;
      begin
      GetDir(0, AppPath);
       try
        ocxPath := AppPath + '\VOIP.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('Unable to register ');
        end; 
      end;

    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;   

最佳答案

使用 regsvr32 让您的生活变得艰难。你已经完成了 99% 的事,不再需要任何东西。无需调用 regsvr32,只需调用 DllRegisterServer。毕竟,这就是 regsvr32 要做的全部事情!

您的代码变为:

if Assigned(ARegFunc) then
  OleCheck(ARegFunc());

然后您可以完全删除 ExecAndWait。这很好,因为它让我不必讨论繁忙的循环和泄漏的句柄!

将您称为 ARegFunc 的变量重命名为 DllRegisterServer 对我来说是有意义的。因此代码可能如下所示:

aHandle := LoadLibrary(PChar(ocxPath));
if aHandle = 0 then
  RaiseLastWin32Error;
try
  DllRegisterServer := GetProcAddress(aHandle,'DllRegisterServer');
  if Assigned(DllRegisterServer) then
    OleCheck(DllRegisterServer());
finally
  FreeLibrary(aHandle);
end;

调用 DllRegisterServer 最可能的失败模式是无法运行提升的注册代码。

顺便说一句,LoadLibrary 返回 HMODULE 而不是 THandle

关于delphi - 如何在 Delphi XE4 中注册 Active X .ocx 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946814/

相关文章:

activex - 在 Electron 中调用 ActiveX

javascript - 使用 ActiveX 获取用户名

delphi - F2063 无法编译使用的单元 'QuickPDF0724.pas' ' Delphi XE4 中的错误

ios - 打开 pdf 文件时,TwebBrowser 缩放/手势不适用于 XE4 中的 firemonkey/iO

windows - 使用 COM/Ole/ActiveX 进行回调/事件管理

delphi - 从delphi 7迁移到delphi XE4时出现TSQLDataset错误

delphi - 如何临时使用与本地注册同名的字体(在 "use"中)

Delphi RTTI 通过属性值设置值

delphi - 如何绘制旋转的圆角矩形

forms - Delphi:带 fsStayOnTop 的气球窗体在 Win7 中不起作用