关于线程间的数据传输!如何使用CreateFileMapping?(100分)

  • 主题发起人 主题发起人 wdwang
  • 开始时间 开始时间
W

wdwang

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在线程间进行数据传输? 不用全局变量!
我想用CreateFileMapping,请问具体怎么使用?如果能给出一点简单代码,感激不尽!
谢谢
 
我有别的方法。
{ TListEx }
//===============================================================
// ListExs by Randolph, 2000
// The TListEx calss like TList, but it suport multi-thread
//===============================================================
TListEx = class(TObject)
private
FList: TList;
FLock: TRTLCriticalSection;
FDuplicates: TDuplicates;
function GetCount: Integer;
protected
function GetPacket(Index: Integer): Pointer;
function IndexOf(Item: Pointer): Integer;
procedure Add(Item: Pointer);
procedure Insert(Index: Integer;
Item: Pointer);
procedure Remove(Item: Pointer);
procedure Delete(Index: Integer);
virtual;
procedure Clear;
virtual;
property Packets[Index: Integer]: Pointer read GetPacket;
default;
property Duplicates: TDuplicates read FDuplicates write FDuplicates;
public
constructor Create;
destructor Destroy;
override;
function LockList: TList;
procedure UnlockList;
property Count: Integer read GetCount;
end;

{ TListEx }
constructor TListEx.Create;
begin
inherited Create;
InitializeCriticalSection(FLock);
FList := TList.Create;
FDuplicates := dupIgnore;
end;

destructor TListEx.Destroy;
begin
LockList;
// Make sure nobody else
is inside the list.
try
FList.Free;
inherited Destroy;
finally
UnlockList;
DeleteCriticalSection(FLock);
end;
end;

function TListEx.LockList: TList;
begin
EnterCriticalSection(FLock);
Result := FList;
end;

procedure TListEx.UnlockList;
begin
LeaveCriticalSection(FLock);
end;

procedure TListEx.Add(Item: Pointer);
begin
LockList;
try
if (Duplicates = dupAccept) or
(FList.IndexOf(Item) = -1) then
FList.Add(Item)
else
if Duplicates = dupError then
FList.Error(@SDuplicateItem, Integer(Item));
finally
UnlockList;
end;
end;

procedure TListEx.Clear;
begin
LockList;
try
FList.Clear;
finally
UnlockList;
end;
end;

procedure TListEx.Delete(Index: Integer);
begin
LockList;
try
FList.Delete(Index);
finally
UnlockList;
end;
end;

function TListEx.GetPacket(Index: Integer): Pointer;
begin
LockList;
try
Result := FList[Index];
finally
UnlockList;
end;
end;

function TListEx.IndexOf(Item: Pointer): Integer;
begin
LockList;
try
Result := FList.IndexOf(Item);
finally
UnlockList;
end;
end;

procedure TListEx.Insert(Index: Integer;
Item: Pointer);
begin
LockList;
try
if (Duplicates = dupAccept) or
(FList.IndexOf(Item) = -1) then
FList.Insert(Index, Item)
else
if Duplicates = dupError then
FList.Error(@SDuplicateItem, Integer(Item));
finally
UnlockList;
end;
end;

procedure TListEx.Remove(Item: Pointer);
begin
LockList;
try
FList.Remove(Item);
finally
UnlockList;
end;
end;

function TListEx.GetCount: Integer;
begin
LockList;
try
Result := FList.Count;
finally
UnlockList;
end;
end;

 
TCreatFile:=CreateFile(FilePath,GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ or FILE_SHARE_WRITE,nil,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
TMapFile:=CreateFileMapping( TCreatFile,nil,Page_ReadWrite,0,
MapFileSize,MapFileName);
PMapFile:=MapViewOfFile(TMapFile,FILE_MAP_READ or FILE_MAP_WRITE,0,0,0);
 
最简单莫过来消息了。
type
PData = ^TData;
TData = record
size: Integer;
buffer: pchar;
end;

var
Data: TData;
begin
Data.Size := Size;
Data.Buffer := Buffer;
postThreadMessage(Thread1.ThreadID, WM_ThreadData, Integer(@Ddata));
end;

thread2;
var
Data: TData;
begin
while GetMessage(msg, 0, 0, 0)do
if msg.msg = WM_ThreadData then
begin
Data := PData(msg.LParam)^;
//proc data
end;

end;
 
如果你之一要用CreateFileMapping,那么我有个自己写的例子:
发送方:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
MapName = 'mophy map';
type
TMapInfo = record
info1: string[255];
info2: string[255];
end;

type
TMainForm = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
MainForm: TMainForm;
hMap: THandle;
pInfo: Pointer;
MapSize: Integer = SizeOf(TMapInfo);
implementation
{$R *.dfm}
procedure TMainForm.FormCreate(Sender: TObject);
begin
hMap := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE or SEC_COMMIT,
0, MapSize, MapName);
end;

procedure TMainForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
CloseHandle(hMap);
end;

procedure TMainForm.Button1Click(Sender: TObject);
var
MapInfo: TMapInfo;
begin
MapInfo.info1 := Edit1.Text;
MapInfo.info2 := Edit2.Text;
pInfo := MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, MapSize);
Move(MapInfo, pInfo^, MapSize);
UnmapViewOfFile(pInfo);
end;

end.

接受方:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
//MapSize = 1024;
MapName = 'mophy map';
type
TMapInfo = record
info1: string[255];
info2: string[255];
end;

type
TMainForm = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
MainForm: TMainForm;
hMap: THandle;
pInfo: Pointer;
MapSize: Integer = SizeOf(TMapInfo);
implementation
{$R *.dfm}
procedure TMainForm.Button1Click(Sender: TObject);
var
MapInfo: TMapInfo;
begin
hMap := OpenFileMapping(FILE_MAP_WRITE, True, MapName);
pInfo := MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, MapSize);
MapInfo := TMapInfo(pInfo^);
Edit1.Text := MapInfo.info1;
Edit2.Text := MapInfo.info2;
UnmapViewOfFile(pInfo);
CloseHandle(hMap);
end;

end.
 
后退
顶部