共享一个之前封装的多进程共享内存的MemoryStream

具体用处呢,有很多,比如多进程浏览器共享Cookie啦,多个进程传送点数据啦.

{
共享内存封装.
封装成了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;
  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, PByte(Memory)[Position], Count);
    Position := Position + Count;
    Result := Count;
  end;
end;

end.
此条目发表在Delphi分类目录,贴了, , 标签。将固定链接加入收藏夹。

共享一个之前封装的多进程共享内存的MemoryStream》有190条回应

  1. keinvo说:

    能否有简单的 两个进程共享通信使用这个的demo?

  2. 宋风寒说:

    非常喜欢老大的万能查询控件WnQuery,一直使用在D7下,现在想学习Delphi Xe5,不知道您的WnQuery有没有Xe5版本可用的啊?若有,给我邮箱发个吧!songfenghanzh@qq.com,不胜感谢!!

    • admin说:

      这个是十年前的作品了,十年没维护过了.这个控件是直连数据库的,不适合多层架构的产品,又是ADO的,借鉴可以.实战的话建议你重新写一个,适用于多层架构的,和数据库引擎无关的.

      • 宋风寒说:

        不支持多层架构吗?我水平有限啊,多层架构的自己写不出来….如果没有新版本,老大能否推荐一个合适多层的查询控件呢?

  3. locet说:

    这里面最重要的一个就是Global

  4. Pingback引用通告: 43ytr.icu/j/GPoAr

  5. Pingback引用通告: Èãðà ïðåñòîëîâ 8 ñåçîí

  6. Pingback引用通告: abisko.ru

  7. Pingback引用通告: 2021

  8. Pingback引用通告: 2020-2020-2020

  9. Pingback引用通告: glyxar.ru

  10. Pingback引用通告: tv onlayn

  11. Pingback引用通告: Watch

  12. Pingback引用通告: video

  13. Pingback引用通告: wwin-tv.com

  14. Pingback引用通告: 00-tv.com

  15. Pingback引用通告: hs;br

  16. Pingback引用通告: tureckie_serialy_na_russkom_jazyke

  17. Pingback引用通告: tureckie_serialy

  18. Pingback引用通告: +1+

  19. Pingback引用通告: æóêè+2+ñåðèÿ

  20. Pingback引用通告: Ñìîòðåòü ñåðèàëû îíëàéí âñå ñåðèè ïîäðÿä

  21. Pingback引用通告: Ñìîòðåòü âñå ñåðèè ïîäðÿä

  22. Pingback引用通告: ++++++

  23. Pingback引用通告: HD-720

  24. Pingback引用通告: guardians+of+the+galaxy+2

  25. Pingback引用通告: strong woman do bong soon

  26. Pingback引用通告: my id is gangnam beauty

  27. Pingback引用通告: 2020

  28. Pingback引用通告: kpop+star+season+6+ep+9

  29. Pingback引用通告: 1 2 3 4 5 6 7 8 9 10

  30. Pingback引用通告: Watch TV Shows

  31. Pingback引用通告: Kinokrad 2019 Kinokrad Hd

  32. Pingback引用通告: Kinokrad

  33. Pingback引用通告: filmy-kinokrad

  34. Pingback引用通告: kinokrad-2019

  35. Pingback引用通告: filmy-2019-kinokrad

  36. Pingback引用通告: serial

  37. Pingback引用通告: youtube2019.ru

  38. Pingback引用通告: dorama hdrezka

  39. Pingback引用通告: movies hdrezka

  40. Pingback引用通告: HDrezka

  41. Pingback引用通告: kinosmotretonline

  42. Pingback引用通告: LostFilm HD 720

  43. Pingback引用通告: trustedmdstorefy.com

  44. Pingback引用通告: bofilm ñåðèàë

  45. Pingback引用通告: bofilm

  46. Pingback引用通告: 1 seriya

  47. Pingback引用通告: Êîíñóëüòàöèÿ ïñèõîëîãà

  48. Pingback引用通告: topedstoreusa.com

  49. Pingback引用通告: hqcialismht.com

  50. Pingback引用通告: viagramdtrustser.com

  51. Pingback引用通告: 4serial.com

  52. Pingback引用通告: See-Season-1

  53. Pingback引用通告: Evil-Season-1

  54. Pingback引用通告: Evil-Season-2

  55. Pingback引用通告: Evil-Season-3

  56. Pingback引用通告: Evil-Season-4

  57. Pingback引用通告: Dollface-Season-1

  58. Pingback引用通告: Queer-Eye-We-re-in-Japan-Season-1

  59. Pingback引用通告: serial 2020

  60. Pingback引用通告: Dailymotion

  61. Pingback引用通告: Watch+movies+2020

  62. Pingback引用通告: serial-video-film-online

  63. Pingback引用通告: tvrv.ru

  64. Pingback引用通告: 1plus1serial.site

  65. Pingback引用通告: #1plus1

  66. Pingback引用通告: 1plus1

  67. Pingback引用通告: Watch Movies Online

  68. Pingback引用通告: Film

  69. Pingback引用通告: Film 2020

  70. Pingback引用通告: Film 2021

  71. Pingback引用通告: watch online TV LIVE

  72. Pingback引用通告: parazity-oskar-2020

  73. Pingback引用通告: human design

  74. Pingback引用通告: human design human design

  75. Pingback引用通告: viagra

  76. Pingback引用通告: viagra online

  77. Pingback引用通告: +

  78. Pingback引用通告: cherkassy film

  79. Pingback引用通告: ¯jak Son³k

  80. Pingback引用通告: astrolog

  81. Pingback引用通告: film-kalashnikov-watch

  82. Pingback引用通告: cialis

  83. Pingback引用通告: cialis online

  84. Pingback引用通告: strelcov 2020

  85. Pingback引用通告: film t-34

  86. Pingback引用通告: canadian online pharmacies

  87. Pingback引用通告: canadian pharmacy

  88. Pingback引用通告: Beograd film 2020

  89. Pingback引用通告: psiholog

  90. Pingback引用通告: psixolog

  91. Pingback引用通告: psyhelp_on_line

  92. Pingback引用通告: coronavirus

  93. Pingback引用通告: PSYCHOSOCIAL

  94. Pingback引用通告: rasstanovka hellinger

  95. Pingback引用通告: Cherekasi film 2020

  96. Pingback引用通告: film doktor_liza

  97. Pingback引用通告: djoker film

  98. Pingback引用通告: t.me/psyhell

  99. Pingback引用通告: Ïñèõîëîã îíëàéí

  100. Pingback引用通告: bitly.com

  101. Pingback引用通告: viagra 100mg

  102. Pingback引用通告: viagra price

  103. Pingback引用通告: viagra generic

  104. Pingback引用通告: viagra coupon

  105. Pingback引用通告: cheap viagra

  106. Pingback引用通告: generic cialis

  107. Pingback引用通告: cialis coupon

  108. Pingback引用通告: canadian pharmacy cialis

  109. Pingback引用通告: cialis 5mg

  110. Pingback引用通告: rlowcostmd.com

  111. Pingback引用通告: bitly

  112. Pingback引用通告: movies-tekstmovies-tekst

  113. Pingback引用通告: Zemlyane 2005 smotret onlajn

  114. Pingback引用通告: smotret onlajn besplatno v kachestve hd 1080

  115. Pingback引用通告: gusmeasu.com

  116. Pingback引用通告: movies-unhinged-film

  117. Pingback引用通告: malenkie-zhenshhiny-2020

  118. Pingback引用通告: dom 2

  119. Pingback引用通告: zoom-psykholog

  120. Pingback引用通告: zoom-viber-skype

  121. Pingback引用通告: Vratar Galaktiki Film, 2020

  122. Pingback引用通告: Vratar

  123. Pingback引用通告: Cherkassy 2020

  124. Pingback引用通告: chernobyl-hbo-2019-1-sezon

  125. Pingback引用通告: moskva-psiholog

  126. Pingback引用通告: batmanapollo.ru

  127. Pingback引用通告: 323

  128. Pingback引用通告: 525

  129. Pingback引用通告: dom2-ru

  130. Pingback引用通告: Tenet Online

  131. Pingback引用通告: psy psy psy psy

  132. Pingback引用通告: krsmi.ru

  133. Pingback引用通告: like-v.ru

  134. Pingback引用通告: CFOSPUK

  135. Pingback引用通告: MAMprEj

  136. Pingback引用通告: fgu0ygW

  137. Pingback引用通告: batmanapollo

  138. Pingback引用通告: tsoy

  139. Pingback引用通告: 44548

  140. Pingback引用通告: 44549

  141. Pingback引用通告: hod-korolevy-2020

  142. Pingback引用通告: HD

  143. Pingback引用通告: 158444

  144. Pingback引用通告: groznyy-serial-2020

  145. Pingback引用通告: 38QvPmk

  146. Pingback引用通告: bitly.com/doctor-strange-hd

  147. Pingback引用通告: bitly.com/eternals-online

  148. Pingback引用通告: bitly.com/maior-grom

  149. Pingback引用通告: matrica-film

  150. Pingback引用通告: dzhonuikfilm4

  151. Pingback引用通告: bitly.com/batman20212022

  152. Pingback引用通告: bitly.com/venom-2-smotret-onlajn

  153. Pingback引用通告: bitly.com/nevremyaumirat

  154. Pingback引用通告: bitly.com/kingsmankingsman

  155. Pingback引用通告: bitly.com/3zaklyatie3

  156. Pingback引用通告: bitly.com/1dreykfilm

  157. Pingback引用通告: bitly.com/topgunmavericktopgun

  158. Pingback引用通告: bitly.com/flash2022

  159. Pingback引用通告: bitly.com/fantasticheskietvari3

  160. Pingback引用通告: bitly.com/wonderwoman1984hd

  161. Pingback引用通告: 1444

  162. Pingback引用通告: cleantalkorg2.ru

  163. Pingback引用通告: 232dfsad

  164. Pingback引用通告: cleantalkorg2.ru/sitemap.xml

  165. Pingback引用通告: join vk

  166. Pingback引用通告: vk login

  167. Pingback引用通告: svaty—7—sezon

  168. Pingback引用通告: svaty 7 sezon

  169. Pingback引用通告: svaty 7

  170. Pingback引用通告: tik tok

  171. Pingback引用通告: 666

  172. Pingback引用通告: The Revenant

  173. Pingback引用通告: 2021

  174. Pingback引用通告: D4

  175. Pingback引用通告: 777

  176. Pingback引用通告: link

  177. Pingback引用通告: 4569987

  178. Pingback引用通告: news news news

  179. Pingback引用通告: psy

  180. Pingback引用通告: psy2022

  181. Pingback引用通告: projectio-freid

  182. Pingback引用通告: kinoteatrzarya.ru

  183. Pingback引用通告: topvideos

  184. Pingback引用通告: afisha-kinoteatrov.ru

  185. Pingback引用通告: Ukrainskie-serialy

  186. Pingback引用通告: site

  187. Pingback引用通告: top

  188. Pingback引用通告: soderzhanki-3-sezon-2021.online

评论已关闭。