본문 바로가기
Delphi, RadStudio

[delphi] OCX 등록 및 해제

by SB리치퍼슨 2011. 11. 28.
[delphi] OCX 등록 및 해제

// (COM Server) OCX 등록 및 해제 
// usage:
// RegOCX('C:\Test.ocx');


unit ocxUtils; 

interface 

uses 
  Windows, SysUtils, ComObj; 

procedure RegOCX(const DLLName: string); 
procedure UnRegOCX(const DLLName: string); 

implementation 

procedure RegOCX(const DLLName: string); 
begin 
if FileExists(DLLName) then
RegisterComServer(DLLName); 
end; 

procedure UnRegOCX(const DLLName: string); 
type 
  TRegProc = function: HResult; stdcall; 
const 
  RegProcName = 'DllUnregisterServer'; 
var 
  Handle: THandle; 
  RegProc: TRegProc; 
begin 
  Handle := SafeLoadLibrary(DLLName); 
  if Handle <= HINSTANCE_ERROR then 
    raise Exception.CreateFmt('%s: %s', [SysErrorMessage(GetLastError), DLLName]); 
  try 
    RegProc := GetProcAddress(Handle, RegProcName); 
    if Assigned(RegProc) then OleCheck(RegProc) else RaiseLastOSError; 
  finally 
    FreeLibrary(Handle); 
  end; 
end; 

end.

--------------------------------------------------------------
// DLL 레지스트리에 등록
// DllRegisterServer 함수 : Olectl.h
// 
DllUnregisterServer 함수 참조 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  OCXHand: THandle;
begin
  OCXHand:=LoadLibrary('c:\windows\system\test.ocx');
  if (GetProcAddress(OCXHand, 'DllRegisterServer')<>nil) then
    ShowMessage('Error!');
  FreeLibrary(OCXHand);
end;

end.

반응형

댓글