Changeset 245 for Common/UCommon.pas


Ignore:
Timestamp:
May 23, 2011, 10:59:04 AM (13 years ago)
Author:
george
Message:
  • Added: Thread safe global thread list management.
  • Added: DeleteFiles routine.
  • Modified: GetUserNameEx use dynamic loaded library.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/UCommon.pas

    r240 r245  
    11unit UCommon;
    22
     3{$mode delphi}
     4
    35interface
    46
    57uses
    6   Windows, Classes, SysUtils, SpecializedList, StrUtils, Dialogs; //, ShFolder, ShellAPI;
     8  Windows, Classes, SysUtils, SpecializedList, StrUtils, Dialogs,
     9  FileUtil; //, ShFolder, ShellAPI;
    710
    811type
     
    2528var
    2629  ExceptionHandler: TExceptionEvent;
     30  DLLHandle1: HModule;
     31  GetUserNameEx: procedure (NameFormat: DWORD;
     32    lpNameBuffer: LPSTR; nSize: PULONG); stdcall;
     33
    2734
    2835function IntToBin(Data: Cardinal; Count: Byte): string;
     
    4552function GetFileFilterItemExt(Filter: string; Index: Integer): string;
    4653procedure FileDialogUpdateFilterFileType(FileDialog: TOpenDialog);
     54procedure DeleteFiles(APath, AFileSpec: string);
    4755
    4856
    4957implementation
     58
     59procedure DeleteFiles(APath, AFileSpec: string);
     60var
     61  SearchRec: TSearchRec;
     62  Find: Integer;
     63  Path: string;
     64begin
     65  Path := IncludeTrailingPathDelimiter(APath);
     66
     67  Find := FindFirst(Path + AFileSpec, faAnyFile xor faDirectory, SearchRec);
     68  while Find = 0 do begin
     69    DeleteFile(Path + SearchRec.Name);
     70
     71    Find := SysUtils.FindNext(SearchRec);
     72  end;
     73  FindClose(SearchRec);
     74end;
     75
    5076
    5177function GetFileFilterItemExt(Filter: string; Index: Integer): string;
     
    226252end;
    227253
    228 procedure GetUserNameEx(NameFormat: DWORD;
    229   lpNameBuffer: LPSTR; nSize: PULONG); stdcall;
    230   external 'secur32.dll' Name 'GetUserNameExA';
    231 
     254function GetVersionInfo: TOSVersionInfo;
     255begin
     256  Result.dwOSVersionInfoSize := SizeOf(Result);
     257  if GetVersionEx(Result) then begin
     258  end;
     259end;
    232260
    233261function LoggedOnUserNameEx(Format: TUserNameFormat): string;
    234262var
    235263  UserName: array[0..250] of Char;
     264  VersionInfo: TOSVersionInfo;
    236265  Size: DWORD;
    237266begin
    238   Size := 250;
    239   GetUserNameEx(Integer(Format), @UserName, @Size);
    240   Result := UTF8Encode(UserName);
     267  VersionInfo := GetVersionInfo;
     268  if VersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
     269    Size := 250;
     270    GetUserNameEx(Integer(Format), @UserName, @Size);
     271    Result := UTF8Encode(UserName);
     272  end else Result := GetUserName;
    241273end;
    242274
     
    272304end;
    273305
     306procedure LoadLibraries;
     307begin
     308  DLLHandle1 := LoadLibrary('secur32.dll');
     309  if DLLHandle1 <> 0 then
     310  begin
     311    @GetUserNameEx := GetProcAddress(DLLHandle1, 'GetUserNameExA');
     312  end;
     313end;
     314
     315procedure FreeLibraries;
     316begin
     317  if DLLHandle1 <> 0 then FreeLibrary(DLLHandle1);
     318end;
     319
     320
     321initialization
     322
     323LoadLibraries;
     324
     325
     326finalization
     327
     328FreeLibraries;
     329
    274330end.
Note: See TracChangeset for help on using the changeset viewer.