AI摘要
本文介绍了如何在程序运行时自动注册OCX或DLL文件。示例代码使用Delphi语言,首先检查OCX文件是否存在,然后加载库并获取注册函数,调用该函数完成注册。如果注册失败,程序会显示错误消息并终止。最后,程序创建并运行主窗体。
本文介绍了如何在程
[php]
program RegOcxSample;
uses
Forms,
SysUtils,
Dialogs,
ActiveX,
Windows,
Unit1 in 'Unit1.pas' {Frm_Main};
{$R *.res}
var
OCXHand:THandle;
RegFunc:TDLLRegisterServer;
sfile:string;
begin
Application.Initialize;
//运行程序前先注册OCX文件
sfile:=ExtractFilePath(Application.ExeName)+myocx.ocx';
if not FileExists(sfile) then
begin
ShowMessage(myocx.ocx文件丢失,请重新安装程序!');
Application.Terminate;
end;
try
OCXHand:=LoadLibrary(PChar(sfile));
RegFunc:=GetProcAddress(OCXHand, 'DllRegisterServer');
if RegFunc <> 0 then
begin
ShowMessage('myocx.ocx注册失败!');
Application.Terminate;
end;
finally
FreeLibrary(OCXHand);
end;
Application.CreateForm(TFrm_Main, Frm_Main);
Application.Run;
end.
[/php]