>
result := -1;
finally
UnLock;
end;
end;
constructor TDllManager.Create;
begin
FDllClass := TDll;
InitializeCriticalSection(FLock);
end;
destructor TDllManager.Destroy;
begin
DeleteCriticalSection(FLock);
inherited;
end;
function TDllManager.GetDlls(const Index: Integer): TDll;
begin
Lock;
try
if (Index >=0) and (Index <= Count - 1) then
result := Items[Index]
else
raise EDllError.CreateFmt(''''Error Index of GetDlls, Value: %d, Total Count: %d'''', [Index, Count]);
finally
UnLock;
end;
end;
function TDllManager.GetDllsByName(const FileName: String): TDll;
var
I: Integer;
begin
Lock;
try
I := IndexOf(FileName);
if I >= 0 then
result := Dlls[I]
else
result := nil;
finally
UnLock;
end;
end;
function TDllManager.IndexOf(const FileName: String): Integer;
var
I: Integer;
begin
result := -1;
Lock;
try
for I := 0 to Count - 1 do
if CompareText(FileName, Dlls[I].FileName) = 0 then
begin
result := I;
break;
end;
finally
UnLock;
end;
end;
procedure TDllManager.Lock;
begin
OutputDebugString(Pchar(''''TRLock DM'''' + IntToStr(GetCurrentThreadId) + '''':'''' + IntToStr(DWORD(Self))));
EnterCriticalSection(FLock);
OutputDebugString(Pchar(''''Locked DM'''' + IntToStr(GetCurrentThreadId) + '''':'''' + IntToStr(DWORD(Self))));
end;
procedure TDllManager.Notify(Ptr: Pointer; Action: TListNotification);
begin
if Action = lnDeleted then
begin
//若TDll(Ptr).Owner和Self不同,则
//表明由 TDll.Destroy 触发;
if TDll(Ptr).Owner = Self then
begin
//防止FOwner设置为nil之后相关事件不能触发
TDll(Ptr).DoBeforeDllUnLoaded;
TDll(Ptr).FOwner := nil;
TDll(Ptr).Free;
end;
end
else
if Action = lnAdded then
TDll(Ptr).FOwner := Self;
inherited;
end;
function TDllManager.Remove(const FileName: String): Integer;
var
I: Integer;
begin
result := -1;
Lock;
try
I := IndexOf(FileName);
if I >= 0 then
result := Remove(Dlls[I])
else
result := -1;
finally
UnLock;
end;
end;
procedure TDllManager.UnLock;
begin
LeaveCriticalSection(FLock);
OutputDebugString(Pchar(''''UnLock DM'''' + IntToStr(GetCurrentThreadId) + '''':'''' + IntToStr(DWORD(Self))));
end;
end.