function RegisterOleFile(strOleFileName: STRING; OleAction: Byte): BOOLEAN;
const
RegisterOle = 1; // 注册
UnRegisterOle = 0; // 卸载
type
TOleRegisterFunction = function: HResult; // 注册或卸载函数的原型
var
hLibraryHandle: THandle; // 由LoadLibrary返回的DLL或OCX句柄
hFunctionAddress: TFarProc; // DLL或OCX中的函数句柄,由GetProcAddress返回
RegFunction: TOleRegisterFunction; // 注册或卸载函数指针
begin
Result := FALSE;
// 打开OLE/DCOM文件,返回的DLL或OCX句柄
hLibraryHandle := LoadLibrary(PCHAR(strOleFileName));
if (hLibraryHandle > 0) then // DLL或OCX句柄正确
try
// 返回注册或卸载函数的指针
if (OleAction = RegisterOle) then // 返回注册函数的指针
hFunctionAddress := GetProcAddress(hLibraryHandle,
PCHAR('DllRegisterServer'))
else // 返回卸载函数的指针
hFunctionAddress := GetProcAddress(hLibraryHandle,
PCHAR('DllUnregisterServer'));
if (hFunctionAddress <> NIL) then // 注册或卸载函数存在
begin
RegFunction := TOleRegisterFunction(hFunctionAddress); // 获取操作函数的指针
if RegFunction >= 0 then // 执行注册或卸载操作,返回值>=0表示执行成功
Result := true;
end;
finally
FreeLibrary(hLibraryHandle); // 关闭已打开的OLE/DCOM文件
end;
end;