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