1 | unit UCommon;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | {$IFDEF Windows}Windows,{$ENDIF}
|
---|
9 | Classes, SysUtils, StrUtils, Dialogs, Process, LCLIntf,
|
---|
10 | FileUtil; //, ShFolder, ShellAPI;
|
---|
11 |
|
---|
12 | type
|
---|
13 | TArrayOfByte = array of Byte;
|
---|
14 | TArrayOfString = array of string;
|
---|
15 | TExceptionEvent = procedure(Sender: TObject; E: Exception) of object;
|
---|
16 |
|
---|
17 | TUserNameFormat = (
|
---|
18 | unfNameUnknown = 0, // Unknown name type.
|
---|
19 | unfNameFullyQualifiedDN = 1, // Fully qualified distinguished name
|
---|
20 | unfNameSamCompatible = 2, // Windows NT® 4.0 account name
|
---|
21 | unfNameDisplay = 3, // A "friendly" display name
|
---|
22 | unfNameUniqueId = 6, // GUID string that the IIDFromString function returns
|
---|
23 | unfNameCanonical = 7, // Complete canonical name
|
---|
24 | unfNameUserPrincipal = 8, // User principal name
|
---|
25 | unfNameCanonicalEx = 9,
|
---|
26 | unfNameServicePrincipal = 10, // Generalized service principal name
|
---|
27 | unfDNSDomainName = 11);
|
---|
28 |
|
---|
29 | var
|
---|
30 | ExceptionHandler: TExceptionEvent;
|
---|
31 | DLLHandle1: HModule;
|
---|
32 |
|
---|
33 | {$IFDEF Windows}
|
---|
34 | GetUserNameEx: procedure (NameFormat: DWORD;
|
---|
35 | lpNameBuffer: LPSTR; nSize: PULONG); stdcall;
|
---|
36 | {$ENDIF}
|
---|
37 |
|
---|
38 | function IntToBin(Data: Int64; Count: Byte): string;
|
---|
39 | function BinToInt(BinStr: string): Int64;
|
---|
40 | function TryHexToInt(Data: string; var Value: Integer): Boolean;
|
---|
41 | function TryBinToInt(Data: string; var Value: Integer): Boolean;
|
---|
42 | function BinToHexString(Source: AnsiString): string;
|
---|
43 | //function DelTree(DirName : string): Boolean;
|
---|
44 | //function GetSpecialFolderPath(Folder: Integer): string;
|
---|
45 | function BCDToInt(Value: Byte): Byte;
|
---|
46 | function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
|
---|
47 | function GetUserName: string;
|
---|
48 | function LoggedOnUserNameEx(Format: TUserNameFormat): string;
|
---|
49 | function SplitString(var Text: string; Count: Word): string;
|
---|
50 | function GetBit(Variable: QWord; Index: Byte): Boolean;
|
---|
51 | procedure SetBit(var Variable: QWord; Index: Byte; State: Boolean); overload;
|
---|
52 | procedure SetBit(var Variable: Cardinal; Index: Byte; State: Boolean); overload;
|
---|
53 | procedure SetBit(var Variable: Word; Index: Byte; State: Boolean); overload;
|
---|
54 | function AddLeadingZeroes(const aNumber, Length : integer) : string;
|
---|
55 | function LastPos(const SubStr: String; const S: String): Integer;
|
---|
56 | function GenerateNewName(OldName: string): string;
|
---|
57 | function GetFileFilterItemExt(Filter: string; Index: Integer): string;
|
---|
58 | procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
|
---|
59 | procedure DeleteFiles(APath, AFileSpec: string);
|
---|
60 | procedure OpenWebPage(URL: string);
|
---|
61 | procedure OpenFileInShell(FileName: string);
|
---|
62 | procedure ExecuteProgram(CommandLine: string);
|
---|
63 | procedure FreeThenNil(var Obj);
|
---|
64 |
|
---|
65 |
|
---|
66 | implementation
|
---|
67 |
|
---|
68 | function BinToInt(BinStr : string) : Int64;
|
---|
69 | var
|
---|
70 | i : byte;
|
---|
71 | RetVar : Int64;
|
---|
72 | begin
|
---|
73 | BinStr := UpperCase(BinStr);
|
---|
74 | if BinStr[length(BinStr)] = 'B' then Delete(BinStr,length(BinStr),1);
|
---|
75 | RetVar := 0;
|
---|
76 | for i := 1 to length(BinStr) do begin
|
---|
77 | if not (BinStr[i] in ['0','1']) then begin
|
---|
78 | RetVar := 0;
|
---|
79 | Break;
|
---|
80 | end;
|
---|
81 | RetVar := (RetVar shl 1) + (byte(BinStr[i]) and 1) ;
|
---|
82 | end;
|
---|
83 |
|
---|
84 | Result := RetVar;
|
---|
85 | end;
|
---|
86 |
|
---|
87 | function BinToHexString(Source: AnsiString): string;
|
---|
88 | var
|
---|
89 | I: Integer;
|
---|
90 | begin
|
---|
91 | for I := 1 to Length(Source) do begin
|
---|
92 | Result := Result + LowerCase(IntToHex(Ord(Source[I]), 2));
|
---|
93 | end;
|
---|
94 | end;
|
---|
95 |
|
---|
96 |
|
---|
97 | procedure DeleteFiles(APath, AFileSpec: string);
|
---|
98 | var
|
---|
99 | SearchRec: TSearchRec;
|
---|
100 | Find: Integer;
|
---|
101 | Path: string;
|
---|
102 | begin
|
---|
103 | Path := IncludeTrailingPathDelimiter(APath);
|
---|
104 |
|
---|
105 | Find := FindFirst(UTF8Decode(Path + AFileSpec), faAnyFile xor faDirectory, SearchRec);
|
---|
106 | while Find = 0 do begin
|
---|
107 | DeleteFileUTF8(Path + UTF8Encode(SearchRec.Name));
|
---|
108 |
|
---|
109 | Find := SysUtils.FindNext(SearchRec);
|
---|
110 | end;
|
---|
111 | FindClose(SearchRec);
|
---|
112 | end;
|
---|
113 |
|
---|
114 |
|
---|
115 | function GetFileFilterItemExt(Filter: string; Index: Integer): string;
|
---|
116 | var
|
---|
117 | List: TStringList;
|
---|
118 | begin
|
---|
119 | try
|
---|
120 | List := TStringList.Create;
|
---|
121 | List.Text := StringReplace(Filter, '|', #10, [rfReplaceAll]);
|
---|
122 | Result := List[Index * 2 + 1];
|
---|
123 | finally
|
---|
124 | List.Free;
|
---|
125 | end;
|
---|
126 | end;
|
---|
127 |
|
---|
128 | procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
|
---|
129 | var
|
---|
130 | FileExt: string;
|
---|
131 | begin
|
---|
132 | FileExt := GetFileFilterItemExt(FileDialog.Filter, FileDialog.FilterIndex - 1);
|
---|
133 | Delete(FileExt, 1, 1); // Remove symbol '*'
|
---|
134 | if FileExt <> '.*' then
|
---|
135 | FileDialog.FileName := ChangeFileExt(FileDialog.FileName, FileExt)
|
---|
136 | end;
|
---|
137 |
|
---|
138 | function GenerateNewName(OldName: string): string;
|
---|
139 | var
|
---|
140 | I: Integer;
|
---|
141 | Number: Integer;
|
---|
142 | begin
|
---|
143 | Number := 1;
|
---|
144 | // Find number on end
|
---|
145 | if Length(OldName) > 0 then begin
|
---|
146 | I := Length(OldName);
|
---|
147 | while (I > 1) and ((OldName[I] >= '0') and (OldName[I] <= '9')) do Dec(I);
|
---|
148 | TryStrToInt(Copy(OldName, I + 1, Length(OldName) - I), Number);
|
---|
149 | Inc(Number)
|
---|
150 | end;
|
---|
151 | Result := Copy(OldName, 1, I) + IntToStr(Number);
|
---|
152 | end;
|
---|
153 |
|
---|
154 | (*function DelTree(DirName : string): Boolean;
|
---|
155 | var
|
---|
156 | SHFileOpStruct : TSHFileOpStruct;
|
---|
157 | DirBuf : array [0..255] of char;
|
---|
158 | begin
|
---|
159 | DirName := UTF8Decode(DirName);
|
---|
160 | try
|
---|
161 | Fillchar(SHFileOpStruct,Sizeof(SHFileOpStruct),0) ;
|
---|
162 | FillChar(DirBuf, Sizeof(DirBuf), 0 ) ;
|
---|
163 | StrPCopy(DirBuf, DirName) ;
|
---|
164 | with SHFileOpStruct do begin
|
---|
165 | Wnd := 0;
|
---|
166 | pFrom := @DirBuf;
|
---|
167 | wFunc := FO_DELETE;
|
---|
168 | fFlags := FOF_ALLOWUNDO;
|
---|
169 | fFlags := fFlags or FOF_NOCONFIRMATION;
|
---|
170 | fFlags := fFlags or FOF_SILENT;
|
---|
171 | end;
|
---|
172 | Result := (SHFileOperation(SHFileOpStruct) = 0) ;
|
---|
173 | except
|
---|
174 | Result := False;
|
---|
175 | end;
|
---|
176 | end;*)
|
---|
177 |
|
---|
178 | function LastPos(const SubStr: String; const S: String): Integer;
|
---|
179 | begin
|
---|
180 | Result := Pos(ReverseString(SubStr), ReverseString(S));
|
---|
181 | if (Result <> 0) then
|
---|
182 | Result := ((Length(S) - Length(SubStr)) + 1) - Result + 1;
|
---|
183 | end;
|
---|
184 |
|
---|
185 | function BCDToInt(Value: Byte): Byte;
|
---|
186 | begin
|
---|
187 | Result := (Value shr 4) * 10 + (Value and 15);
|
---|
188 | end;
|
---|
189 |
|
---|
190 | (*function GetSpecialFolderPath(Folder: Integer): string;
|
---|
191 | const
|
---|
192 | SHGFP_TYPE_CURRENT = 0;
|
---|
193 | var
|
---|
194 | Path: array[0..MAX_PATH] of Char;
|
---|
195 | begin
|
---|
196 | Result := 'C:\Test';
|
---|
197 | if SUCCEEDED(SHGetFolderPath(0, Folder, 0, SHGFP_TYPE_CURRENT, @path[0])) then
|
---|
198 | Result := path
|
---|
199 | else
|
---|
200 | Result := '';
|
---|
201 | end;*)
|
---|
202 |
|
---|
203 | function IntToBin(Data: Int64; Count: Byte): string;
|
---|
204 | var
|
---|
205 | I: Integer;
|
---|
206 | begin
|
---|
207 | Result := '';
|
---|
208 | for I := 0 to Count - 1 do
|
---|
209 | Result := IntToStr((Data shr I) and 1) + Result;
|
---|
210 | end;
|
---|
211 |
|
---|
212 | function IntToHex(Data: Cardinal; Count: Byte): string;
|
---|
213 | const
|
---|
214 | Chars: array[0..15] of Char = '0123456789ABCDEF';
|
---|
215 | var
|
---|
216 | I: Integer;
|
---|
217 | begin
|
---|
218 | Result := '';
|
---|
219 | for I := 0 to Count - 1 do
|
---|
220 | Result := Result + Chars[(Data shr (I * 4)) and 15];
|
---|
221 | end;
|
---|
222 |
|
---|
223 | function TryHexToInt(Data: string; var Value: Integer): Boolean;
|
---|
224 | var
|
---|
225 | I: Integer;
|
---|
226 | begin
|
---|
227 | Data := UpperCase(Data);
|
---|
228 | Result := True;
|
---|
229 | Value := 0;
|
---|
230 | for I := 0 to Length(Data) - 1 do begin
|
---|
231 | if (Data[I + 1] >= '0') and (Data[I + 1] <= '9') then
|
---|
232 | Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1) * 4)
|
---|
233 | else if (Data[I + 1] >= 'A') and (Data[I + 1] <= 'F') then
|
---|
234 | Value := Value or (Ord(Data[I + 1]) - Ord('A') + 10) shl ((Length(Data) - I - 1) * 4)
|
---|
235 | else Result := False;
|
---|
236 | end;
|
---|
237 | end;
|
---|
238 |
|
---|
239 | function TryBinToInt(Data: string; var Value: Integer): Boolean;
|
---|
240 | var
|
---|
241 | I: Integer;
|
---|
242 | begin
|
---|
243 | Result := True;
|
---|
244 | Value := 0;
|
---|
245 | for I := 0 to Length(Data) - 1 do begin
|
---|
246 | if (Data[I + 1] >= '0') and (Data[I + 1] <= '1') then
|
---|
247 | Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1))
|
---|
248 | else Result := False;
|
---|
249 | end;
|
---|
250 | end;
|
---|
251 |
|
---|
252 | function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
|
---|
253 | var
|
---|
254 | I: Integer;
|
---|
255 | begin
|
---|
256 | if Length(Data1) = Length(Data2) then begin
|
---|
257 | Result := True;
|
---|
258 | for I := 0 to Length(Data1) - 1 do begin
|
---|
259 | if Data1[I] <> Data2[I] then begin
|
---|
260 | Result := False;
|
---|
261 | Break;
|
---|
262 | end
|
---|
263 | end;
|
---|
264 | end else Result := False;
|
---|
265 | end;
|
---|
266 |
|
---|
267 | function Explode(Separator: char; Data: string): TArrayOfString;
|
---|
268 | begin
|
---|
269 | SetLength(Result, 0);
|
---|
270 | while Pos(Separator, Data) > 0 do begin
|
---|
271 | SetLength(Result, Length(Result) + 1);
|
---|
272 | Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1);
|
---|
273 | Delete(Data, 1, Pos(Separator, Data));
|
---|
274 | end;
|
---|
275 | SetLength(Result, Length(Result) + 1);
|
---|
276 | Result[High(Result)] := Data;
|
---|
277 | end;
|
---|
278 |
|
---|
279 | {$IFDEF Windows}
|
---|
280 | function GetUserName: string;
|
---|
281 | const
|
---|
282 | MAX_USERNAME_LENGTH = 256;
|
---|
283 | var
|
---|
284 | L: LongWord;
|
---|
285 | begin
|
---|
286 |
|
---|
287 | L := MAX_USERNAME_LENGTH + 2;
|
---|
288 | SetLength(Result, L);
|
---|
289 | if Windows.GetUserName(PChar(Result), L) and (L > 0) then begin
|
---|
290 | SetLength(Result, StrLen(PChar(Result)));
|
---|
291 | Result := UTF8Encode(Result);
|
---|
292 | end else Result := '';
|
---|
293 | end;
|
---|
294 |
|
---|
295 | function GetVersionInfo: TOSVersionInfo;
|
---|
296 | begin
|
---|
297 | Result.dwOSVersionInfoSize := SizeOf(Result);
|
---|
298 | if GetVersionEx(Result) then begin
|
---|
299 | end;
|
---|
300 | end;
|
---|
301 |
|
---|
302 | function LoggedOnUserNameEx(Format: TUserNameFormat): string;
|
---|
303 | const
|
---|
304 | MaxLength = 1000;
|
---|
305 | var
|
---|
306 | UserName: array[0..MaxLength] of Char;
|
---|
307 | VersionInfo: TOSVersionInfo;
|
---|
308 | Size: DWORD;
|
---|
309 | begin
|
---|
310 | VersionInfo := GetVersionInfo;
|
---|
311 | if VersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
|
---|
312 | Size := MaxLength;
|
---|
313 | GetUserNameEx(Integer(Format), @UserName, @Size);
|
---|
314 | //ShowMessage(SysErrorMessage(GetLastError));
|
---|
315 | if GetLastError = 0 then Result := UTF8Encode(UserName)
|
---|
316 | else Result := GetUserName;
|
---|
317 | end else Result := GetUserName;
|
---|
318 | end;
|
---|
319 | {$ELSE}
|
---|
320 | function GetUserName: string;
|
---|
321 | begin
|
---|
322 | Result := '';
|
---|
323 | end;
|
---|
324 |
|
---|
325 | function LoggedOnUserNameEx(Format: TUserNameFormat): string;
|
---|
326 | begin
|
---|
327 | Result := '';
|
---|
328 | end;
|
---|
329 |
|
---|
330 | {$ENDIF}
|
---|
331 |
|
---|
332 | function SplitString(var Text: string; Count: Word): string;
|
---|
333 | begin
|
---|
334 | Result := Copy(Text, 1, Count);
|
---|
335 | Delete(Text, 1, Count);
|
---|
336 | end;
|
---|
337 |
|
---|
338 | function GetBit(Variable:QWord;Index:Byte):Boolean;
|
---|
339 | begin
|
---|
340 | Result := ((Variable shr Index) and 1) = 1;
|
---|
341 | end;
|
---|
342 |
|
---|
343 | procedure SetBit(var Variable:QWord;Index:Byte;State:Boolean); overload;
|
---|
344 | begin
|
---|
345 | Variable := (Variable and ((1 shl Index) xor QWord($ffffffffffffffff))) or (QWord(State) shl Index);
|
---|
346 | end;
|
---|
347 |
|
---|
348 | procedure SetBit(var Variable:Cardinal;Index:Byte;State:Boolean); overload;
|
---|
349 | begin
|
---|
350 | Variable := (Variable and ((1 shl Index) xor Cardinal($ffffffff))) or (Cardinal(State) shl Index);
|
---|
351 | end;
|
---|
352 |
|
---|
353 | procedure SetBit(var Variable:Word;Index:Byte;State:Boolean); overload;
|
---|
354 | begin
|
---|
355 | Variable := (Variable and ((1 shl Index) xor Word($ffff))) or (Word(State) shl Index);
|
---|
356 | end;
|
---|
357 |
|
---|
358 | function AddLeadingZeroes(const aNumber, Length : integer) : string;
|
---|
359 | begin
|
---|
360 | Result := SysUtils.Format('%.*d', [Length, aNumber]) ;
|
---|
361 | end;
|
---|
362 |
|
---|
363 | procedure LoadLibraries;
|
---|
364 | begin
|
---|
365 | {$IFDEF Windows}
|
---|
366 | DLLHandle1 := LoadLibrary('secur32.dll');
|
---|
367 | if DLLHandle1 <> 0 then
|
---|
368 | begin
|
---|
369 | @GetUserNameEx := GetProcAddress(DLLHandle1, 'GetUserNameExA');
|
---|
370 | end;
|
---|
371 | {$ENDIF}
|
---|
372 | end;
|
---|
373 |
|
---|
374 | procedure FreeLibraries;
|
---|
375 | begin
|
---|
376 | {$IFDEF Windows}
|
---|
377 | if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
|
---|
378 | {$ENDIF}
|
---|
379 | end;
|
---|
380 |
|
---|
381 | procedure ExecuteProgram(CommandLine: string);
|
---|
382 | var
|
---|
383 | Process: TProcess;
|
---|
384 | begin
|
---|
385 | try
|
---|
386 | Process := TProcess.Create(nil);
|
---|
387 | Process.CommandLine := CommandLine;
|
---|
388 | Process.Options := [poNoConsole];
|
---|
389 | Process.Execute;
|
---|
390 | finally
|
---|
391 | Process.Free;
|
---|
392 | end;
|
---|
393 | end;
|
---|
394 |
|
---|
395 | procedure FreeThenNil(var Obj);
|
---|
396 | begin
|
---|
397 | TObject(Obj).Free;
|
---|
398 | TObject(Obj) := nil;
|
---|
399 | end;
|
---|
400 |
|
---|
401 | procedure OpenWebPage(URL: string);
|
---|
402 | var
|
---|
403 | Process: TProcess;
|
---|
404 | Browser, Params: string;
|
---|
405 | begin
|
---|
406 | OpenURL(URL);
|
---|
407 | {try
|
---|
408 | Process := TProcess.Create(nil);
|
---|
409 | Browser := '';
|
---|
410 | //FindDefaultBrowser(Browser, Params);
|
---|
411 | //Process.Executable := Browser;
|
---|
412 | //Process.Parameters.Add(Format(Params, [ApplicationInfo.HomePage]);
|
---|
413 | Process.CommandLine := 'cmd.exe /c start ' + URL;
|
---|
414 | Process.Options := [poNoConsole];
|
---|
415 | Process.Execute;
|
---|
416 | finally
|
---|
417 | Process.Free;
|
---|
418 | end;}
|
---|
419 | end;
|
---|
420 |
|
---|
421 | procedure OpenFileInShell(FileName: string);
|
---|
422 | begin
|
---|
423 | ExecuteProgram('cmd.exe /c start "' + FileName + '"');
|
---|
424 | end;
|
---|
425 |
|
---|
426 | initialization
|
---|
427 |
|
---|
428 | LoadLibraries;
|
---|
429 |
|
---|
430 |
|
---|
431 | finalization
|
---|
432 |
|
---|
433 | FreeLibraries;
|
---|
434 |
|
---|
435 | end.
|
---|