forked from rareMaxim/Delphi-Inter-Process-Communication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPC.pas
609 lines (542 loc) · 16.4 KB
/
IPC.pas
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
unit IPC;
interface
uses
Winapi.Windows,
System.SysUtils,
System.Classes;
type
TServerRecieveIpcDataEvent = procedure(Sender: TObject; var ClientName:
WideString; var ClientWaitingForResponse: Boolean; var Data: Pointer) of object;
TClientRecieveResponseIpcDataEvent = procedure(Sender: TObject; var Data: Pointer) of object;
TIPCServer = class;
TIPCClient = class;
TServerThread = class;
TIPCServer = class(TComponent)
private
FServerHandle: THandle;
FServerThread: TServerThread;
FOnRecieveIpcData: TServerRecieveIpcDataEvent;
FServerThreadEnabled: Boolean;
FLastError: Integer;
protected
function ReadData(var ClientName: WideString; var ClientWaitingForResponse:
Boolean; var Data: Pointer): Boolean;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function CreateServer(ServerName: WideString): Boolean;
function FreeServer: Boolean;
function SendIpcData(ClientName: WideString; Data: Pointer; DataSize: DWORD): Boolean;
public
property OnRecieveIpcData: TServerRecieveIpcDataEvent read FOnRecieveIpcData
write FOnRecieveIpcData;
property LastError: Integer read FLastError;
end;
TServerThread = class(TThread)
private
FThreadOwner: TIPCServer;
protected
procedure DoReadData;
procedure Execute; override;
public
constructor Create(AThreadOwner: TIPCServer);
destructor Destroy; override;
end;
TIPCClient = class(TComponent)
private
FClientName: WideString;
FResponseServerHandle: THandle;
FOnRecieveIpcData: TClientRecieveResponseIpcDataEvent;
FLastError: Integer;
protected
function ReadData(ServerHandle: THandle; var ClientName: WideString; var
Data: Pointer): Boolean;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function CreateClient(ClientName: WideString): Boolean;
function FreeClient: Boolean;
function Send<T>(ServerName: WideString; Data: T; WaitForResponse: Boolean;
ResponseTimeout: ULONG; var ResponseData: Pointer): Boolean;
public
property OnRecieveResponseIpcData: TClientRecieveResponseIpcDataEvent read
FOnRecieveIpcData write FOnRecieveIpcData;
property LastError: Integer read FLastError;
end;
type
TClientNameData = packed record
ClientName: array[0..255] of WideChar;
ClientWaitingForResponse: Boolean;
end;
implementation
type
TConvertStringSecurityDescriptorToSecurityDescriptorW = function(StringSecurityDescriptor:
LPCWSTR; StringSDRevision: DWORD; var SecurityDescriptor: Pointer;
SecurityDescriptorSize: PULONG): BOOL; stdcall;
var
OSVersion: DWORD;
ConvertStringSecurityDescriptorToSecurityDescriptorW:
TConvertStringSecurityDescriptorToSecurityDescriptorW;
function GetOSVersion: DWORD;
begin
Result := (TOSVersion.Major.ToString + TOSVersion.Minor.ToString).ToInteger;
end;
constructor TIPCServer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FServerHandle := 0;
end;
destructor TIPCServer.Destroy;
begin
FreeServer;
inherited;
end;
const
LOW_INTEGRITY_SDDL_SACL = 'D:' + '(A;OICI;GRGW;;;AU)' + '(A;OICI;GRGW;;;BA)' +
'(A;OICI;GRGW;;;AN)' + '(A;OICI;GRGW;;;BG)' + 'S:(ML;;NW;;;LW)';
SDDL_REVISION_1 = 1;
function TIPCServer.CreateServer(ServerName: WideString): Boolean;
var
SecurityAttributes: TSecurityAttributes;
SecurityDescriptor: TSecurityDescriptor;
SecurityDescriptor_V: PSECURITY_DESCRIPTOR;
BufferSize: DWORD;
begin
Result := False;
try
if GetMailslotInfo(FServerHandle, nil, BufferSize, nil, nil) then
begin
FLastError := ERROR_ALREADY_EXISTS;
Exit;
end;
if OSVersion >= 60 then
begin
FillChar(SecurityDescriptor_V, SizeOf(PSECURITY_DESCRIPTOR), 0);
if not ConvertStringSecurityDescriptorToSecurityDescriptorW(LOW_INTEGRITY_SDDL_SACL,
SDDL_REVISION_1, SecurityDescriptor_V, nil) then
begin
FLastError := GetLastError;
Exit;
end;
SecurityAttributes.nLength := SizeOf(TSecurityAttributes);
SecurityAttributes.lpSecurityDescriptor := SecurityDescriptor_V;
SecurityAttributes.bInheritHandle := True;
end
else if (OSVersion >= 50) and (OSVersion < 60) then
begin
FillChar(SecurityDescriptor, SECURITY_DESCRIPTOR_MIN_LENGTH, 0);
if InitializeSecurityDescriptor(@SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION) then
begin
if SetSecurityDescriptorDacl(@SecurityDescriptor, True, nil, False) then
begin
SecurityAttributes.nLength := SizeOf(TSecurityAttributes);
SecurityAttributes.lpSecurityDescriptor := @SecurityDescriptor;
SecurityAttributes.bInheritHandle := True;
end;
end;
end;
FServerHandle := CreateMailslotW(PWideChar('\\.\mailslot\' + ServerName), 0,
0, @SecurityAttributes);
if FServerHandle = INVALID_HANDLE_VALUE then
begin
FLastError := GetLastError;
Exit;
end;
if (FServerThreadEnabled = False) then
begin
FServerThread := TServerThread.Create(Self);
FServerThreadEnabled := True;
end;
Result := True;
FLastError := ERROR_SUCCESS;
except
end;
end;
function TIPCServer.FreeServer: Boolean;
begin
Result := False;
try
if FServerThreadEnabled = True then
begin
FServerThread.Terminate;
end;
if FServerHandle <> 0 then
begin
CloseHandle(FServerHandle);
FServerHandle := 0;
end;
FLastError := ERROR_SUCCESS;
Result := True;
except
end;
end;
function TIPCServer.ReadData(var ClientName: WideString; var
ClientWaitingForResponse: Boolean; var Data: Pointer): Boolean;
var
BufferSize, NumberOfBytesWritten: DWORD;
MemoryStream: TMemoryStream;
ClientNameData: TClientNameData;
DataBuffer, ClientDataBuffer: PVOID;
ReadStatus: Boolean;
begin
Result := False;
try
if GetMailslotInfo(FServerHandle, nil, BufferSize, nil, nil) then
begin
if BufferSize <> MAILSLOT_NO_MESSAGE then
begin
GetMem(DataBuffer, BufferSize);
GetMem(ClientDataBuffer, BufferSize);
GetMem(Data, BufferSize);
try
if (DataBuffer <> nil) and (ClientDataBuffer <> nil) and (Data <> nil) then
begin
ReadStatus := ReadFile(FServerHandle, DataBuffer^, BufferSize,
NumberOfBytesWritten, nil);
if ReadStatus then
begin
MemoryStream := TMemoryStream.Create;
try
MemoryStream.Write(DataBuffer^, BufferSize);
MemoryStream.Position := 0;
MemoryStream.Read(ClientDataBuffer^, BufferSize - SizeOf(TClientNameData));
CopyMemory(Data, ClientDataBuffer, BufferSize - SizeOf(TClientNameData));
MemoryStream.Position := BufferSize - SizeOf(TClientNameData);
MemoryStream.Read(ClientNameData, SizeOf(TClientNameData));
ClientName := WideString(ClientNameData.ClientName);
ClientWaitingForResponse := ClientNameData.ClientWaitingForResponse;
finally
MemoryStream.Free;
end;
Result := True;
end;
end;
finally
FreeMem(Data);
FreeMem(ClientDataBuffer);
FreeMem(DataBuffer);
end;
end;
end;
except
end;
end;
function TIPCServer.SendIpcData(ClientName: WideString; Data: Pointer; DataSize: DWORD): Boolean;
var
MemoryStream: TMemoryStream;
NumberOfBytesWritten: DWORD;
ClientData: TClientNameData;
Buffer: Pointer;
ServerHandle: THandle;
begin
Result := False;
try
ServerHandle := CreateFileW(PWideChar('\\.\mailslot\' + ClientName),
GENERIC_WRITE, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if ServerHandle = INVALID_HANDLE_VALUE then
begin
FLastError := GetLastError;
Exit;
end;
try
lstrcpynW(ClientData.ClientName, PWideChar(ClientName), 255);
MemoryStream := TMemoryStream.Create;
try
MemoryStream.Write(Data^, DataSize);
MemoryStream.Write(ClientData, SizeOf(TClientNameData));
MemoryStream.Position := 0;
GetMem(Buffer, MemoryStream.Size);
try
MemoryStream.Read(Buffer^, MemoryStream.Size);
if WriteFile(ServerHandle, Buffer^, MemoryStream.Size, NumberOfBytesWritten, nil) then
begin
FLastError := ERROR_SUCCESS;
Result := True;
end
else
FLastError := GetLastError;
finally
FreeMem(Buffer);
end;
finally
MemoryStream.Free;
end;
finally
CloseHandle(ServerHandle);
end;
except
end;
end;
procedure TServerThread.DoReadData;
var
Data: Pointer;
ClientName: WideString;
ClientWaitResponse: Boolean;
begin
try
Data := nil;
if FThreadOwner.ReadData(ClientName, ClientWaitResponse, Data) then
begin
if Data <> nil then
begin
if Assigned(FThreadOwner.FOnRecieveIpcData) then
FThreadOwner.FOnRecieveIpcData(Self, ClientName, ClientWaitResponse, Data);
end;
end;
except
end;
end;
procedure TServerThread.Execute;
begin
while not Terminated do
begin
Sleep(10);
SYNCHRONIZE(DoReadData);
end;
FThreadOwner.FServerThreadEnabled := False;
end;
constructor TServerThread.Create(AThreadOwner: TIPCServer);
begin
inherited Create(False);
FThreadOwner := AThreadOwner;
FreeOnTerminate := True;
case GetThreadPriority(GetCurrentThread) of
THREAD_PRIORITY_ABOVE_NORMAL:
Priority := tpHigher;
THREAD_PRIORITY_BELOW_NORMAL:
Priority := tpLower;
THREAD_PRIORITY_HIGHEST:
Priority := tpHighest;
THREAD_PRIORITY_IDLE:
Priority := tpIdle;
THREAD_PRIORITY_LOWEST:
Priority := tpLowest;
THREAD_PRIORITY_NORMAL:
Priority := tpNormal;
THREAD_PRIORITY_TIME_CRITICAL:
Priority := tpTimeCritical;
end;
end;
destructor TServerThread.Destroy;
begin
inherited;
end;
// Client
constructor TIPCClient.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FResponseServerHandle := 0;
end;
destructor TIPCClient.Destroy;
begin
FreeClient;
inherited;
end;
function TIPCClient.CreateClient(ClientName: WideString): Boolean;
var
SecurityAttributes: TSecurityAttributes;
SecurityDescriptor: TSecurityDescriptor;
SecurityDescriptor_V: PSECURITY_DESCRIPTOR;
BufferSize: DWORD;
begin
Result := False;
try
if GetMailslotInfo(FResponseServerHandle, nil, BufferSize, nil, nil) then
begin
FLastError := ERROR_ALREADY_EXISTS;
Exit;
end;
if OSVersion >= 60 then
begin
FillChar(SecurityDescriptor_V, SizeOf(PSECURITY_DESCRIPTOR), 0);
if not ConvertStringSecurityDescriptorToSecurityDescriptorW(LOW_INTEGRITY_SDDL_SACL,
SDDL_REVISION_1, SecurityDescriptor_V, nil) then
begin
FLastError := GetLastError;
Exit;
end;
SecurityAttributes.nLength := SizeOf(TSecurityAttributes);
SecurityAttributes.lpSecurityDescriptor := SecurityDescriptor_V;
SecurityAttributes.bInheritHandle := True;
end
else if (OSVersion >= 50) and (OSVersion < 60) then
begin
FillChar(SecurityDescriptor, SECURITY_DESCRIPTOR_MIN_LENGTH, 0);
if InitializeSecurityDescriptor(@SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION) then
begin
if SetSecurityDescriptorDacl(@SecurityDescriptor, True, nil, False) then
begin
SecurityAttributes.nLength := SizeOf(TSecurityAttributes);
SecurityAttributes.lpSecurityDescriptor := @SecurityDescriptor;
SecurityAttributes.bInheritHandle := True;
end;
end;
end;
FResponseServerHandle := CreateMailslotW(PWideChar('\\.\mailslot\' +
ClientName), 0, 0, @SecurityAttributes);
if FResponseServerHandle = INVALID_HANDLE_VALUE then
begin
FLastError := GetLastError;
Exit;
end;
Result := True;
FClientName := ClientName;
FLastError := ERROR_SUCCESS;
except
end;
end;
function TIPCClient.FreeClient: Boolean;
begin
Result := False;
try
if FResponseServerHandle <> 0 then
begin
CloseHandle(FResponseServerHandle);
FResponseServerHandle := 0;
end;
FLastError := ERROR_SUCCESS;
Result := True;
except
end;
end;
function TIPCClient.ReadData(ServerHandle: THandle; var ClientName: WideString;
var Data: Pointer): Boolean;
var
BufferSize, NumberOfBytesWritten: DWORD;
MemoryStream: TMemoryStream;
ClientNameData: TClientNameData;
DataBuffer, ClientDataBuffer: Pointer;
begin
Result := False;
try
if GetMailslotInfo(ServerHandle, nil, BufferSize, nil, nil) then
begin
if BufferSize <> MAILSLOT_NO_MESSAGE then
begin
GetMem(DataBuffer, BufferSize);
GetMem(ClientDataBuffer, BufferSize);
GetMem(Data, BufferSize);
try
if (DataBuffer <> nil) and (ClientDataBuffer <> nil) and (Data <> nil) then
begin
if ReadFile(ServerHandle, DataBuffer^, BufferSize, NumberOfBytesWritten, nil) then
begin
MemoryStream := TMemoryStream.Create;
try
MemoryStream.Write(DataBuffer^, BufferSize);
MemoryStream.Position := 0;
MemoryStream.Read(ClientDataBuffer^, BufferSize - SizeOf(TClientNameData));
CopyMemory(Data, ClientDataBuffer, BufferSize - SizeOf(TClientNameData));
MemoryStream.Position := BufferSize - SizeOf(TClientNameData);
MemoryStream.Read(ClientNameData, SizeOf(TClientNameData));
ClientName := WideString(ClientNameData.ClientName);
finally
MemoryStream.Free;
end;
Result := True;
end;
end;
finally
FreeMem(Data);
FreeMem(ClientDataBuffer);
FreeMem(DataBuffer);
end;
end;
end;
except
end;
end;
function TIPCClient.Send<T>(ServerName: WideString; Data: T; WaitForResponse:
Boolean; ResponseTimeout: ULONG; var ResponseData: Pointer): Boolean;
var
MemoryStream: TMemoryStream;
i, NumberOfBytesWritten: DWORD;
ClientNameData: TClientNameData;
Buffer: Pointer;
ServerHandle: THandle;
ClientName: WideString;
BufferSize: DWORD;
begin
Result := False;
try
ServerHandle := CreateFileW(PWideChar('\\.\mailslot\' + ServerName),
GENERIC_WRITE, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if ServerHandle = INVALID_HANDLE_VALUE then
begin
FLastError := GetLastError;
Exit;
end;
try
lstrcpynW(ClientNameData.ClientName, PWideChar(FClientName), 255);
ClientNameData.ClientWaitingForResponse := WaitForResponse;
MemoryStream := TMemoryStream.Create;
try
MemoryStream.Write(Data, SizeOf(Data) {DataSize});
MemoryStream.Write(ClientNameData, SizeOf(TClientNameData));
MemoryStream.Position := 0;
GetMem(Buffer, MemoryStream.Size);
try
MemoryStream.Read(Buffer^, MemoryStream.Size);
if not WriteFile(ServerHandle, Buffer^, MemoryStream.Size, NumberOfBytesWritten, nil) then
begin
FLastError := GetLastError;
Exit;
end;
finally
FreeMem(Buffer);
end;
finally
MemoryStream.Free;
end;
finally
CloseHandle(ServerHandle);
end;
FLastError := ERROR_SUCCESS;
Result := True;
if (WaitForResponse = True) and (ResponseTimeout > 0) then
begin
try
i := 0;
while True do
begin
if ReadData(FResponseServerHandle, ClientName, ResponseData) then
begin
if ResponseData <> nil then
begin
if ClientName = FClientName then
begin
if Assigned(FOnRecieveIpcData) then
FOnRecieveIpcData(Self, ResponseData);
Exit;
end;
end;
end;
Inc(i);
Sleep(1);
if i >= ResponseTimeout then
begin
Exit;
end;
end;
except
Exit;
end;
end;
except
end;
end;
procedure _Initialize;
var
hLibrary: HMODULE;
begin
OSVersion := GetOSVersion;
hLibrary := LoadLibrary('advapi32.dll');
if (hLibrary <> 0) and (OSVersion >= 60) then
begin
@ConvertStringSecurityDescriptorToSecurityDescriptorW := GetProcAddress(hLibrary,
'ConvertStringSecurityDescriptorToSecurityDescriptorW');
end;
end;
initialization
_Initialize;
end.