//建立共享内存 参数1:共享内存名 参数2:块大小 返回 句柄
Function CreateShareMem(pName:Pchar;Size:Cardinal):Cardinal;
begin
Result:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,Size,pName);
end;
//释放共享内存 参数:句柄
Procedure FreeShareMem(hMapFile:Cardinal);
var
pBuffer:Pointer;
begin
pBuffer:=MapViewOfFile(hMapFile,FILE_MAP_ALL_ACCESS,0,0,0);
if pBuffer <> nil then
UnmapViewOfFile(pBuffer);
if hMapFile <> 0 then
CloseHandle(hMapFile);
end;
{读取共享内存数据
参数1:共享内存名
参数2:存放数据缓存
参数3:读取长度
返回:成功返回true
}
Function ReadShareMem(pName:PChar;var Buffer;Len:Cardinal):Bool;
var
hMapFile:Cardinal;
pBuf:Pointer;
begin
Result:=False;
hMapFile:=OpenFileMapping(FILE_MAP_ALL_ACCESS,false,pName);
if hMapFile <> 0 then
begin
pBuf:=MapViewOfFile(hMapFile,FILE_MAP_READ,0,0,0);
if pBuf <> nil then
begin
CopyMemory(@Buffer,pBuf,Len);
Result:=True;
end;
CloseHandle(hMapFile);
end;
end;
{写入共享内存
参数1:共享内存名
参数2:数据指针
参数3:长度
返回:成功返回true
}
Function WriteShareMem(pName:PChar;Buffer:Pointer;Len:Cardinal):Bool;
var
hMapFile:Cardinal;
pBuf:Pointer;
begin
Result:=False;
hMapFile:=OpenFileMapping(FILE_MAP_ALL_ACCESS,false,pName);
if hMapFile <> 0 then
begin
pBuf:=MapViewOfFile(hMapFile,FILE_MAP_WRITE,0,0,0);
if pBuf <> nil then
begin
CopyMemory(pBuf,Buffer,Len);
Result:=True;
end;
CloseHandle(hMapFile);
end;
end;
可以用于进程间的通讯