第一個問題好辦,在線程里動態加載dll,找入口,調用. 例如:
type
TDataBox= function (var GetData:TDateTime):Boolean;stdcall;
var
DllPath
Char;
Lib:THandle;
DataBox:TDataBox;
SetDate:TDateTime;
begin
GetMem(DllPath,100);
GetSystemDirectory(DllPath,100);
SysDir:=PChar(String(DllPath)+'/DateBox');
Lib:=LoadLibrary(DllPath);
try
if Lib<>0 then
begin
DataBox:=GetProcAddress(Lib,'DateBox');
if @DataBox<>nil then
begin
SetDate:=Date;
if DataBox(SetDate) then
ShowMessage(FormatDateTime('yyyy/mm/dd',SetDate));
end else
ShowMessage(Format('不能加載%s',[DllPath]));
end;
finally
FreeLibrary(Lib);
end;
第二個問題:
在線程外定義一些公共變量.兩個線程在讀寫時採用一些同步/互斥措施.
參考一下我寫過的一個類(採用臨界區),里面定義一個數據區,對外提供了
一數據區的指針,數據區的大小,讀與寫的方法.適合多個線程訪問而互不影響.
你也可以根據要求采用互斥對像或同步對像.
TBuffer=class
private
FPointer
ointer;
FSize:Integer;
FCritical:TCriticalSection;
procedure SetSize(const Value: Integer);
protected
public
constructor Create(ASize:Integer);
destructor Destroy;
procedure Write(const Source;
Pos,Count:Integer);
procedure Read(var Dest;
Pos,Count:Integer);
property Memory: Pointer read FPointer;
property Size: Integer read FSize write SetSize;
end;
{ TBuffer }
constructor TBuffer.Create(ASize:Integer);
begin
GetMem(FPointer,FSize);
FCritical:=TCriticalSection.Create;
end;
destructor TBuffer.Destroy;
begin
FreeMem(FPointer,FSize);
FCritical.Free;
end;
procedure TBuffer.Read(var Dest;
Pos, Count: Integer);
begin
FCritical.Enter;
try
Move(Pointer(LongInt(FPointer)+Pos)^,Dest,Count);
finally
FCritical.Release;
end;
end;
procedure TBuffer.SetSize(const Value: Integer);
begin
FCritical.Enter;
try
ReallocMem(FPointer,FSize);
FSize := Value;
finally
FCritical.Release;
end;
end;
procedure TBuffer.Write(const Source;
Pos, Count: Integer);
begin
FCritical.Enter;
try
if Count>FSize then
Size:=Count;
Move(Source,Pointer(LongInt(FPointer)+Pos)^,Count);
finally
FCritical.Release;
end;
end;