Frage

I have this code to register an OCX on Windows. Can the same code be used to register an ActiveX DLL?

Note that the program runs elevated after embedding the appropriate manifest.

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.
War es hilfreich?

Lösung

ActiveX controls are typically implemented in .dll or .ocx files. They self-register through their DllRegisterServer function. Which is what your code does.

The way you test the return value of DllRegisterServer is a little off. And there are a couple of other areas which could be improved. I would have the code like this:

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;

Although I might prefer a slightly different error reporting mechanism, I have stuck with your approach.

Andere Tipps

Or try directly embedding the registration info in the manifest:

 <file name="path\\some.ocx">
    <comClass description="some OCX" clsid="{OCX CLSID}" 
        progid="ocx.prog.etc" threadingModel="apartment" />
  </file>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top