Delphi 共享内存的应用


 Delphi 共享内存的应用
  1. //建立共享内存 参数1:共享内存名 参数2:块大小 返回 句柄  
  2. Function CreateShareMem(pName:Pchar;Size:Cardinal):Cardinal;  
  3. begin  
  4.   Result:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,Size,pName);  
  5. end;  
  6.   
  7. //释放共享内存  参数:句柄  
  8. Procedure FreeShareMem(hMapFile:Cardinal);  
  9. var  
  10.   pBuffer:Pointer;  
  11. begin  
  12.   pBuffer:=MapViewOfFile(hMapFile,FILE_MAP_ALL_ACCESS,0,0,0);  
  13.   if pBuffer <> nil then  
  14.     UnmapViewOfFile(pBuffer);  
  15.   if hMapFile <> 0 then  
  16.     CloseHandle(hMapFile);  
  17. end;  
  18.   
  19.   
  20. {读取共享内存数据 
  21.  参数1:共享内存名 
  22.  参数2:存放数据缓存 
  23.  参数3:读取长度 
  24.  返回:成功返回true 
  25. }  
  26. Function ReadShareMem(pName:PChar;var Buffer;Len:Cardinal):Bool;  
  27. var  
  28.   hMapFile:Cardinal;  
  29.   pBuf:Pointer;  
  30. begin  
  31.   Result:=False;  
  32.   hMapFile:=OpenFileMapping(FILE_MAP_ALL_ACCESS,false,pName);  
  33.   if hMapFile <> 0 then  
  34.     begin  
  35.        pBuf:=MapViewOfFile(hMapFile,FILE_MAP_READ,0,0,0);  
  36.        if pBuf <> nil then  
  37.          begin  
  38.            CopyMemory(@Buffer,pBuf,Len);  
  39.            Result:=True;  
  40.          end;  
  41.        CloseHandle(hMapFile);  
  42.     end;  
  43. end;  
  44.   
  45.   
  46. {写入共享内存 
  47.  参数1:共享内存名 
  48.  参数2:数据指针 
  49.  参数3:长度 
  50.  返回:成功返回true 
  51. }  
  52. Function WriteShareMem(pName:PChar;Buffer:Pointer;Len:Cardinal):Bool;  
  53. var  
  54.   hMapFile:Cardinal;  
  55.   pBuf:Pointer;  
  56. begin  
  57.   Result:=False;  
  58.   hMapFile:=OpenFileMapping(FILE_MAP_ALL_ACCESS,false,pName);  
  59.   if hMapFile <> 0 then  
  60.     begin  
  61.        pBuf:=MapViewOfFile(hMapFile,FILE_MAP_WRITE,0,0,0);  
  62.        if pBuf <> nil then  
  63.          begin  
  64.            CopyMemory(pBuf,Buffer,Len);  
  65.            Result:=True;  
  66.          end;  
  67.        CloseHandle(hMapFile);  
  68.     end;  
  69. end


可以用于进程间的通讯
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi 中少见的第三方内存管理器,QStrings 的作者 Andrew Driazgov 发布 QMemory is a new memory manager. You can use it as a replacement of the default system memory manager. To do this simply add QMemory unit in your project (as the first unit listed in the project file). Don‘t forget to call the QMemDecommitOverstock function when your application is idle. This subroutine decommits the unused memory blocks (it‘s only way for program to return the memory to the operation system). All allocated memory blocks are 32 byte aligned. The minimum size of the block is 32 bytes. As it is necessary to store some information with each block a dword is attached to the front of each block at -4 the aligned address. Thus, memory request for up to 28 bytes allocates a 32-bytes block, request for 29 to 60 bytes allocates a 64-bytes block, etc (as power of 2). This idea was adopted from HPMM project of Robert Lee (rhlee@optimalcode.com). The memory is committed and decommitted in 64K blocks. The maximum amount of the memory is specified when QMemInstall function is called (from the initialization section of the unit). You can‘t change this value later. If some parts of your program implemented as DLLs you have to use ShareQmm instead of QMemory unit. ShareQmm must be the first unit in your library‘s USES clause AND your project‘s (select Project-View Source) USES clause if some parts of your program have done as separate DLLs (especially if they export any procedures or functions that pass strings as parameters or function results). ShareQmm is the interface unit to the QMM.DLL shared memory manager, which must be deployed along with your DLL. You can‘t combine using of the standard ShareMem and custom ShareQmm memory managers in the same program project (including EXE-file with all DLLs). You can use either ShareMem or ShareQmm, but not both together. ShareQmm memory manager works at the same speed as a ShareMem memory manager with the blocks up to 4096 bytes and much more faster with the larger blocks.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值