簡體   English   中英

從Delphi應用程序注冊Activex DLL

[英]Register Activex DLL from Delphi application

我有這段代碼可以在Windows上注冊OCX。 可以使用相同的代碼注冊ActiveX DLL嗎?

請注意,在嵌入適當的清單后,程序將提升運行。

program RegOCX;

{$APPTYPE CONSOLE}
{$R *.res}
{$R RegOCX.rec}

uses
  System.SysUtils, Vcl.OleCtnrs, OleCtl, Windows;

function CheckOCXReg: Boolean;
var
  X: TOleContainer;
begin
  Result := True;

  X := TOleContainer.Create(nil);
  try
    try
      X.Parent := nil;
      X.CreateObject('KSDHTMLEDLib.KSEditX', False);
    except
      Result := False;
    end;
  finally
    X.Free;
  end;

end;

procedure RegisterOCX;
var
  OCXFl: String;
  OCXHandle: THandle;
  RegFunc: TDllRegisterServer;
begin
  OCXFl := ExtractFilePath(ParamStr(0)) + 'KsDHTMLEDLib.ocx';

  if not FileExists(OCXFl) then
  begin
    WriteLn('Fatal Error - OCX file does not exist! Press Enter to continue..');
    Readln;
    Exit;
  end;

  OCXHandle := LoadLibrary(PChar(OCXFl));
  try
    if OCXHandle = 0 then
    begin
      WriteLn('Error registering OCX! Press Enter to continue..');
      Readln;
    end
    else
    begin
      RegFunc := GetProcAddress(OCXHandle, 'DllRegisterServer');
      if RegFunc <> 0 then
      begin
        WriteLn('Error registering OCX! Press Enter to continue..');
        Readln;
      end;
    end;
  finally
    FreeLibrary(OCXHandle);
  end;

end;

begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    RegisterOCX;
  except
    on E: Exception do
    begin
      WriteLn(E.ClassName, ': ', E.Message);
      Readln;
    end;
  end;

end.

ActiveX控件通常在.dll或.ocx文件中實現。 他們通過其DllRegisterServer函數進行自注冊。 您的代碼是做什么的。

測試DllRegisterServer的返回值的方法有點差。 還有其他兩個方面可以改進。 我會有這樣的代碼:

var
  hMod: HMODULE;
....
hMod := LoadLibrary(PChar(OCXFl));
if hMod = 0 then
begin
  WriteLn('Error registering OCX! Press Enter to continue..');
  Readln;
  exit;
end;
try
  RegFunc := GetProcAddress(hMod, 'DllRegisterServer');
  if not Assigned(RegFunc) then
  begin
    WriteLn('Error registering OCX! Press Enter to continue..');
    Readln;
    exit;
  end;
  if not Succeeded(RegFunc()) then
  begin
    WriteLn('Error registering OCX! Press Enter to continue..');
    Readln;
    exit;
  end;
finally
  FreeLibrary(hMod);
end;

盡管我可能希望使用稍微不同的錯誤報告機制,但是我堅持使用您的方法。

或嘗試將注冊信息直接嵌入清單中:

 <file name="path\\some.ocx">
    <comClass description="some OCX" clsid="{OCX CLSID}" 
        progid="ocx.prog.etc" threadingModel="apartment" />
  </file>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM