打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
delphi 跨进程传输数据

delphi 跨进程传输数据

作者: BccSafe 分类: Code[Delphi] 发布时间: 2014-03-17 22:49 ? 6没有评论

最近需要用到,就写了一个类方便以后调用

主要就利用了内存映射文件来实现

 

UnitShareMemory单元文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
unit UnitShareMemory;
//Writed By BccSafe
//http://www.bccsafe.com
interface
uses
  Winapi.Windows, System.StrUtils;
type
  PSharedData = ^TSharedData;
  TSharedData = Record
    MyStr: string[255];
    //MyStr: array [0..1024] of Char;
    MyInt: Integer;
  end;
  TShareMemory = class
  private
    FMapHandle:THandle;
    FMutexHandle:THandle;
    FLockMutexHandle:THandle;
    FData: PSharedData;
    FMapName: string;
    FMutexName: string;
    FLockMutexName: string;
    FMapExists: Boolean;
    FOpened: Boolean;
    FConnected: Boolean;
    FLocked: Boolean;
    procedure CloseShareMap;
    procedure CreateShareMap;
    procedure OpenShareMap;
    procedure ConnectShareMap;
    procedure DisconnectShareMap;
    function ReadMyStr: string;
    procedure SetMystr(const Value: string);
    function ReadMyInt: Integer;
    procedure SetMyInt(const Value: Integer);
    procedure SetLocked(const Value: Boolean);
  public
    constructor Create(MapName, MutexName, LockMutexName: string);
    destructor Destroy;override;
  published
    property MyStr: string read ReadMyStr write SetMyStr;
    property MyInt: integer read ReadMyInt write SetMyInt;
    property Locked: Boolean read FLocked write SetLocked;
  end;
const
  DefaultShareMapName = 'BccSafe_ShareMemory_FileMap';
  DefaultShareMutex = 'BccSafe_ShareMemory_Mutex';
  DefaultLockMemMutex = 'BccSafe_LockShareMemory_Mutex';
implementation
{ TShareMemory }
constructor TShareMemory.Create(MapName, MutexName, LockMutexName: string);
begin
  inherited Create;
  FLocked          := False;
  FMapExists       := False;
  FConnected       := False;
  FOpened          := False;
  FMapName         := IfThen(MapName = '', DefaultShareMapName, MapName);
  FMutexName       := IfThen(FMutexName = '', DefaultShareMutex, FMutexName);
  FLockMutexName   := IfThen(FLockMutexName = '', DefaultLockMemMutex, LockMutexName);
  FMutexHandle:= CreateMutex(nil, True, Pchar(FMutexName));
  if GetLastError <> ERROR_ALREADY_EXISTS then
    CreateShareMap else OpenShareMap;
end;
destructor TShareMemory.Destroy;
begin
  CloseShareMap;
  CloseHandle(FMutexHandle);
  inherited Destroy;
end;
procedure TShareMemory.CreateShareMap;
begin
  FMapHandle:= CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0,
                 SizeOf(TSharedData), PChar(FMapName));
  if FMapHandle <> 0 then
  begin
    FMapExists:= True;
    ConnectShareMap;
  end else Halt;
end;
procedure TShareMemory.OpenShareMap;
begin
  FMapHandle:= OpenFileMapping(FILE_MAP_ALL_ACCESS,
                 False, PChar(FMapName));
  if FMapHandle <> 0 then
  begin
    FOpened:= True;
    ConnectShareMap;
  end;// else Halt;
end;
function TShareMemory.ReadMyInt: Integer;
begin
  if FData <> nil then
  begin
    Locked:= True;
    Result:= FData.MyInt;
    Locked:= False;
  end;
end;
function TShareMemory.ReadMyStr: string;
begin
  if FData <> nil then
  begin
    Locked:= True;
    Result:= FData.MyStr;
    Locked:= False;
  end;
end;
procedure TShareMemory.SetMyInt(const Value: Integer);
begin
  if FData <> nil then
  begin
    Locked:= True;
    FData.MyInt:= Value;
    Locked:= False;
  end;
end;
procedure TShareMemory.SetMystr(const Value: string);
begin
  if FData <> nil then
  begin
    Locked:= True;
    FData.MyStr:= Value;  //Strcopy(FData.MyStr ,pchar(Value));
    Locked:= False;
  end;
end;
procedure TShareMemory.SetLocked(const Value: Boolean);
begin
  if Value then
  begin
    FLockMutexHandle:= CreateMutex(nil, True, PChar(FLockMutexName));
    if FLockMutexHandle <> 0 then
      FLocked:= True
    else FLocked:= False;
  end else
  begin
    FLockMutexHandle:= OpenMutex(MUTEX_ALL_ACCESS, False, PChar(FLockMutexName));
    if FLockMutexHandle  <> 0 then
    begin
      CloseHandle(FLockMutexHandle);
      FLockMutexHandle:= 0;
      FLocked:= False;
    end;
  end;
end;
procedure TShareMemory.CloseShareMap;
begin
  if FMapHandle <> 0 then
  begin
    CloseHandle(FMapHandle);
    FMapHandle:= 0;
  end;
end;
procedure TShareMemory.ConnectShareMap;
begin
  if Not (FMapExists or FOpened) then Exit;
  FData:= MapViewOfFile(FMapHandle,
            FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TSharedData));
  if FData <> nil then FConnected:= True;
end;
procedure TShareMemory.DisconnectShareMap;
begin
  if FData <> nil then
  begin
    UnmapViewOfFile(FData);
    FData:= nil;
    FConnected:= False;
  end;
end;
end.

 

 

调用方法

1
2
3
4
5
6
7
8
9
//ShareMemory: TShareMemory;
//project1
  ShareMemory:= TShareMemory.Create('', '', '');
  ShareMemory.str:= 'test';//写入数据
//project2
   ShareMemory:= TShareMemory.Create('', '', '');
  showmessage(ShareMemory.str);//读取数据

 

本文出自 BccSafe's Blog,转载时请注明出处及相应链接。

本文永久链接: http://www.bccsafe.com/?p=483

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Delphi内存映射文件例子
delphi中的键值对, 自制Hashtable
扩展DelphiXE IDE白皮书
汉字与区位码(1)
设计模式之singleton
delphi基础开发技巧
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服