forked from alphaonex86/Supercopier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCAPICommon.pas
167 lines (134 loc) · 5.06 KB
/
SCAPICommon.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
{
This file is part of SuperCopier.
SuperCopier is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
SuperCopier is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
}
unit SCAPICommon;
{$MODE Delphi}
interface
uses Windows,Classes,SysUtils,
SCWin32, lclproc;
type
TApiError=(aeNone=0,aeBadHandle=1,aeWrongHandleType=2,aeEmptyBaseList=3,aeBadLocStringId=4);
TApiFunction=(afNone=0,afObjectFree=1,afGetLastError=2,afErrorMessage=3,afObjectExists=4,afGetLocString=5,
afNewBaseList=10,afBaselistAddItem=11,
afIsEnabled=20,afProcessBaseList=21,afIsSameVolumeMove=22,
afNewCopy=30,afCopyAddBaseList=31);
TFileMappingStream=class(TStream)
private
FFileMapping:Pointer;
FSize,FPosition:Integer;
public
constructor Create(AHandle:THandle;ASize:Integer);
destructor Destroy;override;
function Read(var Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
procedure WriteInteger(AValue:Integer);
procedure WriteWideString(AValue:WideString);
function ReadInteger:Integer;
function ReadWideString:WideString;
end;
const
SC2_API_ID='SuperCopier API';
SC2_API_SEMAPHORE_ID='Semaphore'; // le sйmaphore est utilisй pour s'assurer qu'une seule instance de l'API tourne pour une session
SC2_API_MUTEX_ID='Mutex'; // le mutex est utilisй pour empйcher 2 clients utilisant l'API de se corrompre mutuellement
SC2_API_FILEMAPPING_ID='FileMapping'; // le filemapping est utilisй pour transfйrer des donnйes depuis et vers les clients utilisant l'API
SC2_API_CLIENTEVENT_ID='ClientEvent'; // l'event est utilisй pour notifier que des donnйes d'un client sont prкtes dans le filemapping
SC2_API_APIEVENT_ID='APIEvent'; // l'event est utilisй pour notifier que des donnйes de l'API sont prкtes dans le filemapping
SC2_API_FILEMAPPING_SIZE=128*1024;
SC2_API_ERRORS_NAMES:array[TApiError] of WideString=('No error','Bad handle','Wrong handle type','Empty Baselist','Bad LocString ID');
SC2_API_INVALID_HANDLE=-1;
function SessionUniqueAPIIdentifier(AObject:WideString):WideString;
implementation
{ TFileMappingStream }
constructor TFileMappingStream.Create(AHandle: THandle; ASize: Integer);
begin
inherited Create;
FFileMapping:=nil;
if AHandle<>0 then
FFileMapping:=MapViewOfFile(AHandle,FILE_MAP_ALL_ACCESS,0,0,ASize);
Assert(FFileMapping<>nil);
FPosition:=0;
FSize:=ASize;
end;
destructor TFileMappingStream.Destroy;
begin
UnmapViewOfFile(FFileMapping);
inherited;
end;
function TFileMappingStream.Read(var Buffer; Count: Integer): Longint;
var EndPos:Integer;
begin
Result:=Count;
EndPos:=FPosition+Count;
Assert((Count>=0) and (EndPos<=FSize));
Move(Pointer(Longint(FFileMapping)+FPosition)^,Buffer,Result);
FPosition:=EndPos;
end;
function TFileMappingStream.ReadInteger: Integer;
begin
Read(Result,SizeOf(Result));
end;
function TFileMappingStream.ReadWideString: WideString;
var Len:Integer;
begin
Read(Len,SizeOf(Len));
SetLength(Result,Len);
Read(Result[1],Len*SizeOf(WideChar));
Result := Result;
end;
function TFileMappingStream.Seek(Offset: Integer; Origin: Word): Longint;
begin
case Origin of
soFromBeginning: FPosition:=Offset;
soFromCurrent: Inc(FPosition,Offset);
soFromEnd: FPosition:=FSize+Offset;
end;
Result := FPosition;
Assert((FPosition>=0) and (FPosition<=FSize));
end;
function TFileMappingStream.Write(const Buffer; Count: Integer): Longint;
var EndPos:Integer;
begin
Result:=Count;
EndPos:=FPosition+Count;
Assert((Count>=0) and (EndPos<=FSize));
Move(Buffer,Pointer(Longint(FFileMapping)+FPosition)^,Result);
FPosition:=EndPos;
end;
procedure TFileMappingStream.WriteInteger(AValue: Integer);
begin
Write(AValue,SizeOf(AValue));
end;
procedure TFileMappingStream.WriteWideString(AValue: WideString);
begin
WriteInteger(Length(AValue));
Write(AValue[1],Length(AValue)*SizeOf(WideChar));
end;
//******************************************************************************
function SessionUniqueAPIIdentifier(AObject:WideString): WideString;
var UserName:array of WideChar;
Size:Cardinal;
begin
Result:='';
UserName:=nil;
Size:=0;
if SCWin32.GetUserName(nil,Size) or (GetLastError<>ERROR_INSUFFICIENT_BUFFER) then
Exit;
SetLength(UserName,Size);
try
if not SCWin32.GetUserName(@UserName[0],Size) then
Exit;
Result:=SC2_API_ID+' '+AObject+' '+PWideChar(@UserName[0]);
finally
SetLength(UserName,0);
end;
end;
end.