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