1,184
社区成员
发帖
与我相关
我的任务
分享
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
WM_DATA = WM_USER + 1024;
type
PShareMem = ^TShareMem;
TShareMem = record
Data: array[0..255] of char;
end;
TMyForm = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure getShareInfo(var Msg: TMessage); message WM_DATA; {处理WM_DATA}
end;
var
MyForm: TMyForm;
PShare: PShareMem;
MapHandle: THandle;
implementation
{$R *.DFM}
procedure TMyForm.getShareInfo(var Msg: TMessage); {处理WM_DATA}
begin
if msg.LParam=1 then {是我们设定的消息参数}
Memo1.Text := PShare^.Data;
end;
procedure TMyForm.FormCreate(Sender: TObject);
begin
MapHandle := OpenFileMapping(FILE_MAP_WRITE, {获取完全访问映射文件}
False, {不可继承的}
pchar('Map Name')); {映射文件名字}
if MapHandle = 0 then
begin
ShowMessage('不能定位内存映射文件块!');
Halt;
end;
PShare := PShareMem(MapViewOfFile(MapHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0));
if PShare = nil then
begin
CloseHandle(MapHandle);
ShowMessage('Can''t View Memory Map');
Application.Terminate;
exit;
end;
FillChar(PShare^, SizeOf(TShareMem), 0);
end;
procedure TMyForm.Button1Click(Sender: TObject);
begin
CloseHandle(MapHandle);
close;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
WM_DATA = WM_USER + 1024;
type
PShareMem = ^TShareMem;
TShareMem = record
Data: array[0..255] of char;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
PShare: PShareMem;
implementation
{$R *.DFM}
var
HMapping: THandle;
HMapMutex: THandle;
const
MAPFILESIZE = 1000;
REQUEST_TIMEOUT = 1000;
procedure OpenMap;
begin
{创建一个文件映射内核对象}
HMapping := CreateFileMapping(
$FFFFFFFF,
nil,
PAGE_READWRITE,
0,
SizeOf(TShareMem),
{这个文件映射对象的名字用于与其他进程共享该对象,}
pchar('Map Name')
);
if (hMapping = 0) then
begin
ShowMessage('不能创建内存映射文件');
Application.Terminate;
exit;
end;
{将文件数据映射到进程的地址空间}
{当创建了一个文件映射对象之后,仍然必须让系统为文件的数据保留
一个地址空间区域,并将文件的数据作为映射到该区域的物理存储器进行提交。
}
PShare := PShareMem(MapViewOfFile(HMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0));
if PShare = nil then
begin
CloseHandle(HMapping);
ShowMessage('Can''t View Memory Map');
Application.Terminate;
exit;
end;
{既然我们通过pvFile得到了映象视图的起始地址,那么可以对视图做一些操作了}
end;
procedure CloseMap;
begin
if PShare <> nil then
{从进程的地址空间中撤销文件数据的映象}
UnMapViewOfFile(PShare);
if HMapping <> 0 then
CloseHandle(HMapping);
end;
function LockMap: Boolean;
begin
Result := true;
{创建互斥对象}
HMapMutex := CreateMutex(nil, false,
pchar('MY MUTEX NAME GOES HERE'));
if HMapMutex = 0 then
begin
ShowMessage('不能创建互斥对象');
Result := false;
end else begin
if WaitForSingleObject(HMapMutex, REQUEST_TIMEOUT)
= WAIT_FAILED then
begin
ShowMessage('不能对互斥对象加锁!');
Result := false;
end
end
end;
procedure UnlockMap;
begin
{关闭文件映射对象和文件对象}
ReleaseMutex(HMapMutex);
CloseHandle(HMapMutex);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
str: pchar;
begin
str := pchar('简单的共享内存的例子');
CopyMemory(@(pShare^.data), Str, Length(str));
{发送消息表明有数据}
PostMessage(FindWindow(nil, 'MyForm'), WM_DATA, 1, 1)
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnlockMap;
CloseMap;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
OpenMap;
LockMap;
end;