1
0
mirror of https://bitbucket.org/Dennis07/lina-components.git synced 2024-11-24 08:02:12 +02:00
lina-components/Source/uSysTools.pas
Dennis07 39c0916f1c Version 1.0 DEV 1.0
Signed-off-by: Dennis07 <den.goehlert@t-online.de>
2014-08-31 19:12:32 +02:00

187 lines
4.3 KiB
ObjectPascal

unit uSysTools;
//////////////////////////////////////
/// Lina System Tools Unit ///
/// **************************** ///
/// (c) 2014 Dennis Göhlert a.o. ///
//////////////////////////////////////
interface
{ Da Delphi vor Version 2009 noch keine Generics kannte, musste die
TIntegerList-,TBooleanList-,TFloatList- und TPointList-Deklaration auf
Compiler-Versionen 20.0 oder höher beschränkt werden.
Meines Wissens nach gibt es keine einfache Möglichkeit, Typisierte Listen
auch in ältere Versionen zu integrieren. }
uses
{ Standard-Units }
Classes, SysUtils, Math, Windows,
{$IFNDEF NOGENERIC}
Types, Generics.Collections,
{$ENDIF}
{ Andere Package-Units }
uFileTools, uBase;
type
{ Fehlermeldungen }
EWinUserInformation = class(Exception);
type
{$IFNDEF NOGENERIC}
TIntegerList = TList<Integer>;
TBooleanList = TList<Boolean>;
TFloatList = TList<Extended>;
TPointList = TList<TPoint>;
{$ENDIF}
function BoolToInt(B: Boolean): Integer;
function IntToBool(Value: Integer): Boolean;
function StrIsInt(const S: String): Boolean;
function StrIsFloat(const S: String): Boolean;
function StrIsBool(const S: String): Boolean;
function FloatIsInt(Value: Extended): Boolean;
function SystemLanguage: String;
function WinUserName: String;
function WinUserDirectory: String;
function WinUserAdmin: Boolean;
function WinUserExists(UsrNme: String): Boolean;
implementation
function BoolToInt(B: Boolean): Integer;
begin
if B then
begin
Result := not 0;
end else
begin
Result := 0;
end;
end;
function IntToBool(Value: Integer): Boolean;
begin
Result := (Value <> 0);
end;
function StrIsInt(const S: String): Boolean;
var
TryMethodBuffer: Integer;
begin
Result := TryStrToInt(S,TryMethodBuffer);
end;
function StrIsFloat(const S: String): Boolean;
var
TryMethodBuffer: Extended;
begin
Result := TryStrToFloat(S,TryMethodBuffer);
end;
function StrIsBool(const S: String): Boolean;
var
TryMethodBuffer: Boolean;
begin
Result := TryStrToBool(S,TryMethodBuffer);
end;
function FloatIsInt(Value: Extended): Boolean;
begin
Result := (Ceil(Value) = Floor(Value));
end;
function SystemLanguage: String;
begin
Result := Languages.NameFromLocaleID[Languages.UserDefaultLocale];
end;
function WinUserName: String;
var
Buffer: array [0..255] of Char;
Size: DWord;
begin
Size := SizeOf(Buffer);
if not GetUserName(Buffer, Size) then
begin
raise EWinUserInformation.Create('Could not collect information on user name');
end;
SetString(Result,Buffer,Size - 1);
end;
function WinUserDirectory: String;
begin
Result := GetEnvironmentVariable('USERPROFILE');
end;
function WinUserAdmin: Boolean;
const
SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = (Value: (0,0,0,0,0,5));
SECURITY_BUILTIN_DOMAIN_RID = $00000020;
DOMAIN_ALIAS_RID_ADMINS = $00000220;
var
hAccessToken: THandle;
ptgGroups: PTokenGroups;
dwInfoBufferSize: DWORD;
psidAdministrators: PSID;
X: Integer;
bSuccess: BOOL;
begin
Result := False;
bSuccess := False;
ptgGroups := nil;
psidAdministrators := nil;
try
bSuccess := OpenThreadToken(GetCurrentThread,TOKEN_QUERY,True,hAccessToken);
if not bSuccess then
begin
if GetLastError = ERROR_NO_TOKEN then
begin
bSuccess := OpenProcessToken(GetCurrentProcess,TOKEN_QUERY,hAccessToken);
end;
end;
if bSuccess then
begin
GetMem(ptgGroups,1024);
bSuccess := GetTokenInformation(hAccessToken,TokenGroups,ptgGroups,1024,dwInfoBufferSize);
if bSuccess then
begin
AllocateAndInitializeSid(SECURITY_NT_AUTHORITY,2,SECURITY_BUILTIN_DOMAIN_RID,DOMAIN_ALIAS_RID_ADMINS,0,0,0,0,0,0,psidAdministrators);
{$R-}
for X := 0 to ptgGroups.GroupCount - 1 do
begin
if EqualSid(psidAdministrators,ptgGroups.Groups[X].Sid) then
begin
Result := True;
Break;
end;
end;
{$R+}
end;
end;
finally
if bSuccess then
begin
CloseHandle(hAccessToken);
end;
if Assigned(ptgGroups) then
begin
FreeMem(ptgGroups);
end;
if Assigned(psidAdministrators) then
begin
FreeSid(psidAdministrators);
end;
end;
end;
function WinUserExists(UsrNme: String): Boolean;
begin
//...
end;
end.