该单元转自武稀松的博客

稍作修改,使其支持Delphi7

{
共享内存封装.
封装成了MemoryStream的形式.
用法如下:
var
ms : TShareMemStream;
ms := TShareMemStream.Create('Global\test', FILE_MAP_ALL_ACCESS, 4096);
if (ms.Memory <> nil)(*and(ms.AlreadyExists)*) then
//如果创建失败Memory指针是空指针
//AlreadyExists表示已经存在了,也就是之前被别人(也许是别的进程)创建过了.
begin
//获取锁,多个进程线程访问安全访问
if ms.GetLock(INFINITE) then
begin
ms.read(...);
ms.write(...);
//释放锁
ms.ReleaseLock();
end;
end;
ms.free;
}
unit ShareMemoryStream; interface uses
SysUtils, Classes, Syncobjs, Windows; type
TShareMemStream = class(TCustomMemoryStream)
private
FFile: THandle;
FSize: Int64;
FEvent: TEvent;
FAlreadyExists: Boolean;
protected
property Event: TEvent read FEvent;
public
constructor Create(const ShareName: string; ACCESS: DWORD = FILE_MAP_ALL_ACCESS; ASize: Int64 = 16 * 1024 * 1024);
destructor Destroy; override; function Write(const Buffer; Count: Integer): Longint; override; function GetLock(ATimeOut: DWORD = INFINITE): Boolean;
procedure ReleaseLock(); property AlreadyExists: Boolean read FAlreadyExists;
end; implementation procedure InitSecAttr(var sa: TSecurityAttributes; var sd: TSecurityDescriptor);
begin
sa.nLength := sizeOf(sa);
sa.lpSecurityDescriptor := @sd;
sa.bInheritHandle := false;
InitializeSecurityDescriptor(@sd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@sd, true, nil, false);
end; { TShareMem } constructor TShareMemStream.Create(const ShareName: string; ACCESS: DWORD; ASize: Int64);
var
sa: TSecurityAttributes;
sd: TSecurityDescriptor;
lprotect: DWORD;
e: Integer;
begin
FEvent := TEvent.Create(nil, false, true, ShareName + '_TShareMemStream_Event');
FSize := ASize;
InitSecAttr(sa, sd); ACCESS := ACCESS and (not SECTION_MAP_EXECUTE); if (ACCESS and FILE_MAP_WRITE) = FILE_MAP_WRITE then
lprotect := PAGE_READWRITE
else if (ACCESS and FILE_MAP_READ) = FILE_MAP_READ then
lprotect := PAGE_READONLY; FFile := CreateFileMapping(INVALID_HANDLE_VALUE, @sa, lprotect, Int64Rec(FSize).Hi, Int64Rec(FSize).Lo, PChar(ShareName));
e := GetLastError;
MessageBox(0,PChar(IntToStr(e)),0,0);
if FFile = 0 then
Exit;
FAlreadyExists := e = ERROR_ALREADY_EXISTS;
SetPointer(MapViewOfFile(FFile, ACCESS, 0, 0, Int64Rec(FSize).Lo), Int64Rec(FSize).Lo);
end; destructor TShareMemStream.Destroy;
begin
if Memory <> nil then
begin
UnmapViewOfFile(Memory);
SetPointer(nil, 0);
Position := 0;
end;
if FFile <> 0 then
begin
CloseHandle(FFile);
FFile := 0;
end;
FEvent.Free;
inherited Destroy;
end; function TShareMemStream.GetLock(ATimeOut: DWORD): Boolean;
var
wr: TWaitResult;
begin
wr := FEvent.WaitFor(ATimeOut);
Result := wr = wrSignaled;
end; procedure TShareMemStream.ReleaseLock;
begin
FEvent.SetEvent;
end; function TShareMemStream.Write(const Buffer; Count: Integer): Longint;
begin
Result := 0;
if (Size - Position) >= Count then
begin
System.Move(Buffer, Pointer(Longint(Memory) + Position)^, Count);
Position := Position + Count;
Result := Count;
end;
end; end.



测试Demo下载

最新文章

  1. Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象
  2. H5版定点投篮游戏(1)--物理模型抽象
  3. jquery easy ui 1.3.4 布局layout(4)
  4. 2016.03.31,英语,《Vocabulary Builder》Unit 08
  5. php数组遍历
  6. 学习HTML5之新特性标签一览(详细)
  7. MSSQL Server Transaction 数据库事务回滚的用法
  8. Linux – RedHat7 / CentOS 7 忘记root密码修改
  9. 团队项目&#183;冰球模拟器——cmake 自动化构建系统的配置文件的编写
  10. WPF笔记(2.2 DockPanel)——Layout
  11. iOS回顾笔记(07) -- UITableView的使用和性能优化
  12. web 服务发布注意事项
  13. vue watch 可以监听子组件props里面属性的改变
  14. Java-Runoob-高级教程-实例-数组:14. Java 实例 – 在数组中查找指定元素
  15. [UE4]Switch on String,根据字符串决定条件分支,类似于高级语言中的switch语句
  16. 虚拟机中安装Ubuntu 16.04
  17. JavaScript窗口打开与关闭及如何使用opener使子窗口对父窗口进行操作
  18. Myeclipse最全快捷键
  19. svn &amp; git 问题汇总
  20. setInterva()调用

热门文章

  1. Zstack 鼎阳SDS6204示波器和Archiver Appliance的重度测试2
  2. ASP.NET Core - 依赖注入(一)
  3. LinkedList内部实现原理
  4. 微信小程序的全局弹窗以及全局实例
  5. 基于Hexo的GitHub Pages个人博客搭建
  6. KingbaseES DBLink 介绍
  7. 重要内置函数、常见内置函数(了解)、可迭代对象、迭代器对象、for循环原理、异常捕获
  8. Ubuntu命令安装默认支持的Qt5版本
  9. bash 和 zsh 中while循环的方式
  10. springmvc关于通过使用路径占位符出现中文乱码解决办法