function RegisterAssembly(FileName: string): Boolean;
type
TRegAction = (raReg, raUnreg);
type
TAssemblyRegProc = function(FileName: PChar;Reserved: Pointer) : Boolean stdcall;
const
STRegAsm = 'tregasm.dll';
SLoadFail = '加载文件%s失败';
SRegFail = '在"%s"中调用%s失败';
SCantFindProc = '在"%s"中无法找到 %s 过程';
AssemblyProcName: array[TRegAction] of PChar = ('RegisterAssembly', 'UnregisterAssembly');
var
RegProc: TAssemblyRegProc;
SavedCW: Word;
LibHandle: THandle;
RegAction: TRegAction;
begin
Result := False;
RegAction:=raReg;
SavedCW := Get8087CW;
try
Set8087CW($027f);
LibHandle := LoadLibrary(PChar(STRegAsm));
if LibHandle = 0 then raise Exception.CreateFmt(SLoadFail, [STRegAsm]);
try
@RegProc := GetProcAddress(LibHandle, PAnsiChar(AnsiString(AssemblyProcName[RegAction])));
if @RegProc = nil then
raise Exception.CreateFmt(SCantFindProc, [AssemblyProcName[RegAction],
STRegAsm]);
if not RegProc(PChar(FileName), nil) then
raise Exception.CreateFmt(SRegFail, [AssemblyProcName[RegAction], STRegAsm]);
Result := True;
finally
FreeLibrary(LibHandle);
end;
finally
Set8087CW(SavedCW);
end;
end;