1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
| //==============================================================================
// uCrossPlateform.pas
//------------------------------------------------------------------------------
// Historique :
// 20/11/16 - BeanzMaster - Creation
//------------------------------------------------------------------------------
// Description :
// Unité regroupant des procedure et fonctions compatibles sur plusieur OS :
// Windows, Linux et Mac.
//------------------------------------------------------------------------------
// Credits :
//
//------------------------------------------------------------------------------
//==============================================================================
unit ucrossplateform;
//==============================================================================
{$I beanz.inc}
//==============================================================================
interface
//==============================================================================
uses
{$IFDEF WINDOWS}
Windows,
{$ENDIF}
{$IFDEF LINUX}
BaseUnix, UnixUtil, Unix,
{$ENDIF}
{$IFDEF X11_SUPPORT}
xlib,
{$ENDIF}
LCLVersion, LCLType, FileUtil, LazUtf8, lazfileutils, types,
Classes, SysUtils, DateUtils, // SyncObjs,
UCommonTypes;
//==============================================================================
{$IFNDEF FPC}
//----------------------------------------------------------------------------
// Ces nouveaux types ont été ajoutés pour être en mesure de gérer des pointeurs
// vers des entiers en mode 64 bits, car dans FPC 'Integer' le type est toujours
// 32 bits (ou 16 bits en mode Pascal), mais dans Delphi il est spécifique à la
// plate-forme et peut être 16 , 32 ou 64 bits.
//----------------------------------------------------------------------------
type
Int = {$IFDEF CPU64}Int64{$ELSE}LongInt{$ENDIF};
UInt = {$IFDEF CPU64}UInt64{$ELSE}LongWord{$ENDIF};
DWORD = Types.DWORD; {$NODEFINE DWORD}
TPoint = Types.TPoint;{$NODEFINE TPoint}
PPoint = Types.PPoint;{$NODEFINE PPoint}
TRect = Types.TRect; {$NODEFINE TRect}
PRect = Types.PRect; {$NODEFINE PRect}
{$ELSE}
type
Int = {$IFDEF CPU64}Int64{$ELSE}Integer{$ENDIF};
UInt = {$IFDEF CPU64}UInt64{$ELSE}Cardinal{$ENDIF};
DWORD = System.DWORD;
TPoint = Types.TPoint;
PPoint = ^TPoint;
TRect = Types.TRect;
PRect = ^TRect;
{$ENDIF}
Type
TPlatformInfo = record
Major: DWORD;
Minor: DWORD;
Revision: DWORD;
Version: string;
PlatformId :DWORD;
ID: string;
CodeName: string;
Description: string;
ProductBuildVersion: string;
end;
Type
TPlatformVersion =
(
pvUnknown,
pvWin95,
pvWin98,
pvWinME,
pvWinNT3,
pvWinNT4,
pvWin2000,
pvWinXP,
pvWin2003,
pvWinVista,
pvWinSeven,
pvWin2008,
pvWin8,
pvWin10,
pvLinuxArc,
pvLinuxDebian,
pvLinuxopenSUSE,
pvLinuxFedora,
pvLinuxGentoo,
pvLinuxMandriva,
pvLinuxRedHat,
pvLinuxTurboLinux,
pvLinuxUbuntu,
pvLinuxXandros,
pvLinuxOracle,
pvAppleMacOSX
);
Type
TDeviceCapabilities = record
Xdpi, Ydpi: integer; // Nombre de pixel logique par pouce.
Depth: integer; // profondeur de couleur (bit).
NumColors: integer; // Nombre d'entrées dans la table des couleurs de l'appareil.
end;
//Surveillance de la mémoire HEAP/RAM
// --- Rolf
Type
PHeapMRec = ^THeapMRec;
THeapMRec = record
Extra: string;
case integer of
// Heap
{$IFDEF FPC}
0: (MaxHeapSize: PtrUInt; // Taille maximum autorisée pour le tas, en octets
MaxHeapUsed: PtrUInt; // Taille maximum utilisée pour le tas, en octets
CurrHeapSize: PtrUInt; // Taiile du tas en cours, en octets
CurrHeapUsed: PtrUInt; // Taiile du tas utilisé en cours, en octets
CurrHeapFree: PtrUInt); // Taiile libre du tas en cours, en octets
{$ELSE}
0: (AllocatedSmallBlockCount: cardinal;
TotalAllocatedSmallBlockSize: NativeUInt;
ReservedSmallBlockAddressSpace: NativeUInt;
AllocatedMediumBlockCount: cardinal;
TotalAllocatedMediumBlockSize: NativeUInt;
ReservedMediumBlockAddressSpace: NativeUInt;
AllocatedLargeBlockCount: cardinal;
TotalAllocatedLargeBlockSize: NativeUInt;
ReservedLargeBlockAddressSpace: NativeUInt);
{$ENDIF}
// Ram
1: (MemoryLoad: cardinal;
TotalPhysical: UInt64;
AvailPhysical: UInt64;
TotalPageFile: UInt64;
AvailPageFile: UInt64;
TotalVirtual: UInt64;
AvailVirtual: UInt64);
end;
//==============================================================================
//------------------------------------------------------------------------------
// Fonctions sur les disques, dossiers et fichiers
//------------------------------------------------------------------------------
// Retourne les lettres des disques
function GetDriveLetters:string ;
// Retourne le nom d'un disque
function GetVolumeLabel (DriveChar: char): string;
// Retourne le dossier de l'application en cours
function GetAppPath : string;
// Defini le dossier de notre executable
procedure SetExeDirectory;
// Retourne le chemin relatif
function GetRelativePath(const S: string): string;
// Fix les problème de délimiteur de path suivant le systeme
procedure FixPathDelimiter(var S: string);
// Retrourne TRUE si l'acces en écriture d'un dossier est possible
function IsDirectoryWriteable(const AName: string): Boolean;
// Ouvre un fichier HTML ou une Url dans le navigateur par defaut
procedure ShowHTMLUrl(Url: string);
// Retourne le répertoire temporaire de l'OS
function GetTempFolderPath : string;
//------------------------------------------------------------------------------
// Fonctions d'informations sur le systeme
//------------------------------------------------------------------------------
// Renvoi le format de la decimal de notre systeme
function GetDecimalSeparator: Char;
// Definit le format décimal "." ou "," habituellement
procedure SetDecimalSeparator(AValue: Char);
// Retourne les infos sur l'OS
function GetPlatformInfo: TPlatformInfo;
// Retrourne la version de l'OS
function GetPlatformVersion : TPlatformVersion;
// Retrourne la version de l'OS sous forme de chaine de caratères
function GetPlatformVersionAsString : string;
// Retourne des informations sur la mémoire, sous forme de chaine de caratères
function GetMemoryStatusAsString:String;
// Retourne les carateristique d'affichage de l'appareil
function GetDeviceCapabilities: TDeviceCapabilities;
// Retourne le nombre de bit pour l'affichage
function GetCurrentColorDepth: Integer;
// Retourne la largeur de l'affichage en DPI
function GetDeviceLogicalPixelsX(device: HDC): Integer;
// Retourne les carateristique d'affichage de l'appareil sous forme de chaine de caratères
function GetDeviceCapabilitiesAsString: String;
// Retourne le nom de fichier de l'application en cours
function GetAppFileName : string;
// Retourne les lettres des disques sous forme de chaine de cractères séparé par une virgule
function GetVersionExeAsString: string;
// Retourne les informations sur l'application en cours
// /!\ Attention vous devez activer et definir ces infos dans les options votre projet,
// sinon c'est plantage direct
function GetAppInfosAsString:String;
// Retourne le nom du CPU, sous forme de chaine de caratères
function GetCPUName : string;
// Retourne le nombres de thread maximum (nombre de coeur du processeur)
function GetMaxThreads : byte;
// Retourne le nombre processeur (coeur)
function GetLogicalProcessorCount: Integer;
//------------------------------------------------------------------------------
// Fonctions Timers
//------------------------------------------------------------------------------
function RDTSC: Int64;
function Get_TickCount: int64;
function NowPrecise: TDateTime;
function StartPrecisionTimer: Int64;
function StopPrecisionTimer(const precisionTimer: Int64): Double;
function PrecisionTimerLap(const precisionTimer: Int64): Double;
var
PerformanceFrequency: Int64;
//==============================================================================
//==============================================================================
implementation
uses
{$IFDEF FPC}
resreader, fileinfo, resource, versionresource,process,forms,
{$IFDEF WINDOWS}
ShellApi,
JwaWinBase,{, JwaWinNt}
winpeimagereader; {need this for reading exe info}
{$ENDIF}
{$IFDEF LINUX}
LCLProc,
elfreader; {needed for reading ELF executables}
{$ENDIF}
{$IFDEF DARWIN}
XMLRead,
DOM,
machoreader; {needed for reading MACH-O executables};
{$ENDIF}
{$ELSE}
ShellApi;
{$ENDIF}
//==============================================================================
type
TInt4Array = array[0..3] of integer;
//==============================================================================
var
//NowPreciseLock: TCriticalSection;
vInvPerformanceCounterFrequency: Double;
vInvPerformanceCounterFrequencyReady: Boolean = False;
//==============================================================================
//------------------------------------------------------------------------------
// Retourne le chemin relatif
function GetRelativePath(const S: string): string;
var
{$IFNDEF FPC}
path: string;
{$ELSE}
path: UTF8String;
{$ENDIF}
begin
Result := S;
path := ExtractFilePath(s);
path := IncludeTrailingPathDelimiter(path);
if (Pos(path, S) = 1) then
system.Delete(Result, 1, Length(path));
end;
//------------------------------------------------------------------------------
// QueryPerformanceCounter
{$IFDEF UNIX}
var
vProgStartSecond: int64;
procedure Init_vProgStartSecond;
var
tz: timeval;
begin
fpgettimeofday(@tz, nil);
vProgStartSecond := tz.tv_sec;
end;
{$ENDIF}
procedure QueryPerformanceCounter(var val: Int64);
{$IFDEF WINDOWS}
begin
Windows.QueryPerformanceCounter(val);
end;
{$ENDIF}
{$IFDEF LINUX}
var
tz: timeval;
begin
fpgettimeofday(@tz, nil);
val := tz.tv_sec - vProgStartSecond;
val := val * 1000000;
val := val + tz.tv_usec;
end;
{$ENDIF}
//------------------------------------------------------------------------------
// Equivalent à GetTickCount/64 mais portable
function Get_TickCount: Int;
begin
{$IFDEF WINDOWS}
{$IFDEF CPU64}result := GetTickCount64{$ELSE}result := GetTickCount{$ENDIF};
{$ENDIF}
{$IFDEF LINUX}
QueryPerformanceCounter(result);
{$ENDIF}
end;
//------------------------------------------------------------------------------
// Version precise de Now(), renvoie un resultat en secondes
function NowPrecise: TDateTime;
var
{$IFDEF LINUX}T: TimeVal;{$ENDIF}
{$IFDEF WINDOWS}TimerValue: Int64;{$ENDIF}
begin
// Result := Now;
//try
//NowPreciseLock.Acquire;
{$IFDEF WINDOWS}
QueryPerformanceCounter(TimerValue);
//Result := Int64(TimeStampToMSecs(DateTimeToTimeStamp(Now)) * 1000) // an alternative Win32 timebase
Result := TimerValue / PerformanceFrequency;
{$ENDIF}
{$IFDEF LINUX}
fpgettimeofday(@t, nil);
// Build a 64 bit microsecond tick from the seconds and microsecond longints
Result := t.tv_sec + t.tv_usec / 1000000;
{$ENDIF}
Result := Result * OneSecond;
//Result := (Trunc(Now / OneSecond) + Frac(Result)) * OneSecond;
//finally
//NowPreciseLock.Release;
//end;
end;
//------------------------------------------------------------------------------
// QueryPerformanceFrequency
function QueryPerformanceFrequency(var val: Int64): Boolean;
{$IFDEF WINDOWS}
begin
Result := Boolean(Windows.QueryPerformanceFrequency(val));
end;
{$ENDIF}
{$IFDEF LINUX}
begin
val := 1000000;
Result := True;
end;
{$ENDIF}
//------------------------------------------------------------------------------
// StartPrecisionTimer
function StartPrecisionTimer: Int64;
begin
QueryPerformanceCounter(Result);
end;
//------------------------------------------------------------------------------
// PrecisionTimeLap
function PrecisionTimerLap(const precisionTimer: Int64): Double;
begin
Result := StopPrecisionTimer(precisionTimer);
end;
//------------------------------------------------------------------------------
// StopPrecisionTimer
function StopPrecisionTimer(const precisionTimer: Int64): Double;
var
cur, freq: Int64;
begin
QueryPerformanceCounter(cur);
if not vInvPerformanceCounterFrequencyReady then
begin
QueryPerformanceFrequency(freq);
vInvPerformanceCounterFrequency := 1.0 / freq;
vInvPerformanceCounterFrequencyReady := True;
end;
Result := (cur - precisionTimer) * vInvPerformanceCounterFrequency;
end;
//------------------------------------------------------------------------------
// RDTSC
function RDTSC: Int64;
{$IFDEF FPC}
begin
raise exception.create('Using uCrossPlatform.RDTSC with FPC is a bad idea!');
Result := 0;
end;
{$ELSE}
asm
db $0f, $31
end;
{$ENDIF}
//------------------------------------------------------------------------------
// Retourne le nombre processeur (coeur)
function GetLogicalProcessorCount: Integer;
{$IFDEF WINDOWS}
var
SystemInfo: _SYSTEM_INFO;
begin
GetSystemInfo(SystemInfo);
Result := SystemInfo.dwNumberOfProcessors;
end;
{$ELSE}
Begin
{$MESSAGE Warn 'Needs to be implemented'}
Result:=0;
end;
{$ENDIF}
//------------------------------------------------------------------------------
// Ouvre un fichier HTML ou une Url dans le navigateur par defaut
procedure ShowHTMLUrl(Url: string);
begin
{$IFDEF WINDOWS}
ShellExecute(0, 'open', PChar(Url), nil, nil, SW_SHOW);
{$ENDIF}
{$IFDEF LINUX}
fpSystem(PChar('env xdg-open ' + Url));
{$ENDIF}
end;
//------------------------------------------------------------------------------
// Retourne le dossier de l'application en cours
function GetAppPath : string;
var
{$IFNDEF FPC}
path: string;
{$ELSE}
path: UTF8String;
{$ENDIF}
begin
{$IFNDEF FPC}
path := ExtractFilePath(ParamStr(0));
path := IncludeTrailingPathDelimiter(path);
{$ELSE}
path := ExtractFilePath(ParamStrUTF8(0));
path := IncludeTrailingPathDelimiter(path);
{$ENDIF}
result:=path;
end;
//------------------------------------------------------------------------------
// Retourne le nom de fichier de l'application en cours
function GetAppFileName : string;
var
{$IFNDEF FPC}
path: string;
{$ELSE}
path: UTF8String;
{$ENDIF}
begin
{$IFNDEF FPC}
path := ExtractFileName(ParamStr(0));
{$ELSE}
path := ExtractFileName(ParamStrUTF8(0));
{$ENDIF}
result:=path;
end;
//------------------------------------------------------------------------------
// Defini le dossier de notre executable
procedure SetExeDirectory;
var
{$IFNDEF FPC}
path: string;
{$ELSE}
path: UTF8String;
{$ENDIF}
begin
path := GetAppPath();
{$IFNDEF FPC}
SetCurrentDir(path);
{$ELSE}
SetCurrentDirUTF8(path);
{$ENDIF}
end;
//------------------------------------------------------------------------------
// Renvoi le format de la decimal de notre systeme
function GetDecimalSeparator: Char;
begin
Result :=
{$IFDEF FPC}
{$IF (lcl_release > 29) }
DefaultFormatSettings.
{$IFEND}
{$ENDIF}
{$IFDEF GLS_DELPHI_XE_UP}
FormatSettings.
{$ENDIF}
DecimalSeparator;
end;
//------------------------------------------------------------------------------
// Definit le format décimal "." ou "," habituellement
procedure SetDecimalSeparator(AValue: Char);
begin
{$IFDEF FPC}
DefaultFormatSettings.DecimalSeparator := AValue;
{$ENDIF}
{$IFDEF DELPHI_XE_UP}
FormatSettings. DecimalSeparator := AValue;
{$ENDIF}
end;
//------------------------------------------------------------------------------
// Fix les problème de délimiteur de path suivant le systeme
procedure FixPathDelimiter(var S: string);
var
I: Integer;
begin
for I := Length(S) downto 1 do
if (S[I] = '/') or (S[I] = '\') then
S[I] := PathDelim;
end;
//------------------------------------------------------------------------------
// Retourne les infos sur l'OS
function GetPlatformInfo: TPlatformInfo;
var
{$IFDEF MSWINDOWS}
OSVersionInfo : windows.TOSVersionInfo;
//LPOSVERSIONINFOA; //
{$ENDIF}
{$IFDEF UNIX}
{$IFNDEF DARWIN}
ReleseList: TStringList;
{$ENDIF}
str: String;
{$IFDEF DARWIN}
Documento: TXMLDocument;
Child: TDOMNode;
i:integer;
{$ENDIF}
{$ENDIF}
begin
{$IFDEF WINDOWS}
With Result do
begin
OSVersionInfo.dwOSVersionInfoSize := sizeof(TOSVersionInfo);
if not windows.GetVersionEx(OSVersionInfo) then Exit;
Minor := OSVersionInfo.DwMinorVersion;
Major := OSVersionInfo.DwMajorVersion;
Revision := OSVersionInfo.dwBuildNumber;
PlatformId := OSVersionInfo.dwPlatformId;
Version := InttoStr(OSVersionInfo.DwMajorVersion)+'.'+InttoStr(OSVersionInfo.DwMinorVersion)+' Build : '+InttoStr(OSVersionInfo.dwBuildNumber);
end;
{$ENDIF}
{$IFDEF LINUX}
{$IFNDEF DARWIN}
ReleseList := TStringList.Create;
with Result,ReleseList do
begin
if FileExists('/etc/lsb-release') then
LoadFromFile('/etc/lsb-release')
else Exit;
ID := Values['DISTRIB_ID'];
Version := Values['DISTRIB_RELEASE'];
CodeName := Values['DISTRIB_CODENAME'];
Description := Values['DISTRIB_DESCRIPTION'];
Destroy;
end;
{$ENDIF}
{$IFDEF DARWIN}
if FileExists('System/Library/CoreServices/ServerVersion.plist') then
ReadXMLFile(Documento, 'System/Library/CoreServices/ServerVersion.plist')
else Exit;
Child := Documento.DocumentElement.FirstChild;
if Assigned(Child) then
begin
with Child.ChildNodes do
try
for i := 0 to (Count - 1) do
begin
if Item[i].FirstChild.NodeValue='ProductBuildVersion' then
Result.ProductBuildVersion:=Item[i].NextSibling.FirstChild.NodeValue;
if Item[i].FirstChild.NodeValue='ProductName' then
Result.ID:=Item[i].NextSibling.FirstChild.NodeValue;
if Item[i].FirstChild.NodeValue='ProductVersion' then
Result.Version:=Item[i].NextSibling.FirstChild.NodeValue;
end;
finally
Free;
end;
end;
{$ENDIF}
//Major.Minor.Revision
str:=Result.Version;
if str='' then Exit;
Result.Major:=StrtoInt( Copy(str, 1, Pos('.',str)-1) );
Delete(str, 1, Pos('.', str) );
//10.04
if Pos('.', str) = 0 then
begin
Result.Minor:=StrtoInt( Copy(str, 1, Length(str)) );
Result.Revision:=0;
end else
//10.6.5
begin
Result.Minor:=StrtoInt( Copy(str, 1, Pos('.',str)-1) );
Delete(str, 1, Pos('.', str) );
Result.Revision:=StrtoInt( Copy(str, 1, Length(str)) );
end;
{$ENDIF}
end;
//------------------------------------------------------------------------------
// Retrourne la version de l'OS
function GetPlatformVersion : TPlatformVersion;
{$IFDEF Unix}
var
i: integer;
const
VersStr : array[TPlatformVersion] of string = (
'', '', '', '', '', '',
'', '', '', '', '', '',
'', '',
'Arc',
'Debian',
'openSUSE',
'Fedora',
'Gentoo',
'Mandriva',
'RedHat',
'TurboLinux',
'Ubuntu',
'Xandros',
'Oracle',
'Mac OS X'
);
{$ENDIF}
begin
Result := pvUnknown;
{$IFDEF WINDOWS}
with GetPlatformInfo do
begin
if Version='' then Exit;
case Major of
0..2: Result := pvUnknown;
3: Result := pvWinNT3; // Windows NT 3
4: case Minor of
0: if PlatformId = VER_PLATFORM_WIN32_NT
then Result := pvWinNT4 // Windows NT 4
else Result := pvWin95; // Windows 95
10: Result := pvWin98; // Windows 98
90: Result := pvWinME; // Windows ME
end;
5: case Minor of
0: Result := pvWin2000; // Windows 2000
1: Result := pvWinXP; // Windows XP
2: Result := pvWin2003; // Windows 2003
end;
6: case Minor of
0: Result := pvWinVista; // Windows Vista
1: Result := pvWinSeven; // Windows Seven
2: Result := pvWin2008; // Windows 2008
3..4: Result := pvUnknown;
end;
7..8: Result := pvWin8;
9..10: Result := pvWin10;
end;
end;
{$ENDIF}
{$IFDEF LINUX}
with GetPlatformInfo do
begin
if Version='' then Exit;
For i:= 13 to Length(VersStr)-1 do
if ID=VersStr[TPlatformVersion(i)] then
Result := TPlatformVersion(i);
end;
{$ENDIF}
end;
//------------------------------------------------------------------------------
// Retrourne la version de l'OS sous forme de chaine de caratères
function GetPlatformVersionAsString : string;
const
VersStr : array[TPlatformVersion] of string = (
'Inconnu',
'Windows 95',
'Windows 98',
'Windows ME',
'Windows NT 3',
'Windows NT 4',
'Windows 2000',
'Windows XP',
'Windows 2003',
'Windows Vista',
'Windows Seven',
'Windows 2008',
'Windows 8',
'Windows 10',
'Linux Arc',
'Linux Debian',
'Linux openSUSE',
'Linux Fedora',
'Linux Gentoo',
'Linux Mandriva',
'Linux RedHat',
'Linux TurboLinux',
'Linux Ubuntu',
'Linux Xandros',
'Linux Oracle',
'Apple MacOSX');
begin
Result := VersStr[GetPlatformVersion];
end;
//------------------------------------------------------------------------------
// Retrourne TRUE si l'acces en écriture d'un dossier est possible
function IsDirectoryWriteable(const AName: string): Boolean;
var
LFileName: String;
{$IFDEF WINDOWS}
LHandle: THandle;
{$ENDIF}
begin
LFileName := IncludeTrailingPathDelimiter(AName) + 'chk.tmp';
{$IFDEF WINDOWS}
LHandle := CreateFile(PChar(LFileName), GENERIC_READ or GENERIC_WRITE, 0, nil,
CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY or FILE_FLAG_DELETE_ON_CLOSE, 0);
Result := LHandle <> INVALID_HANDLE_VALUE;
if Result then
CloseHandle(LHandle);
{$ELSE}
Result := fpAccess(PChar(LFileName), W_OK) <> 0;
{$ENDIF}
end;
//------------------------------------------------------------------------------
// Retourne les carateristique d'affichage de l'appareil
function GetDeviceCapabilities: TDeviceCapabilities;
{$IFDEF WINDOWS}
var
Device: HDC;
begin
Device := GetDC(0);
try
result.Xdpi := GetDeviceCaps(Device, LOGPIXELSX);
result.Ydpi := GetDeviceCaps(Device, LOGPIXELSY);
result.Depth := GetDeviceCaps(Device, BITSPIXEL);
result.NumColors := GetDeviceCaps(Device, NUMCOLORS);
finally
ReleaseDC(0, Device);
end;
end;
{$ELSE}
{$IFDEF X11_SUPPORT}
var
dpy: PDisplay;
begin
dpy := XOpenDisplay(nil);
Result.Depth := DefaultDepth(dpy, DefaultScreen(dpy));
XCloseDisplay(dpy);
Result.Xdpi := 96;
Result.Ydpi := 96;
Result.NumColors := 1;
end;
{$ELSE}
begin
{$MESSAGE Warn 'Needs to be implemented'}
end;
{$ENDIF}
{$ENDIF}
//------------------------------------------------------------------------------
// Retourne les carateristique d'affichage de l'appareil sous forme de chaine de caratères
function GetDeviceCapabilitiesAsString: String;
Var
s:String;
begin
s:='';
with GetDeviceCapabilities() do
begin
s:= 'Resolution : '+Inttostr(Screen.Width)+'x'+Inttostr(Screen.Height)+#13#10;
s:=s+'DPI : '+ Inttostr(Xdpi) +'x'+inttostr(Ydpi)+#13#10;
s:=s+'Format : '+ Inttostr(Depth)+' Bits'+#13#10;
//s:=s+'Couleurs : '+ Inttostr(NumColors);
end;
result:=s;
end;
//------------------------------------------------------------------------------
// Retourne la largeur de l'affichage en DPI
function GetDeviceLogicalPixelsX(device: HDC): Integer;
begin
result := GetDeviceCapabilities().Xdpi;
end;
//------------------------------------------------------------------------------
// Retourne le nombre de bit pour l'affichage
function GetCurrentColorDepth: Integer;
begin
result := GetDeviceCapabilities().Depth;
end;
//------------------------------------------------------------------------------
// Retourne les N° de version majeur et mineur de l'appli, le N° de révision, et
// le Build ;
function getCurrentNumVer: TInt4Array;
{$IFDEF WINDOWS}
var
vInfoSize,
aDword,
vValueSize: dword;
vInfo: pointer;
vValue: PVSFixedFileInfo;
begin
ZeroMemory(@Result,SizeOf(Result));
aDword:=0;
vValueSize:=0;
vValue:=nil;
try
{$IFDEF FPC}
vInfoSize:=GetFileVersionInfoSizeW(pWideChar(UTF8Decode(ParamStrUTF8(0))),aDword);
{$ELSE}
vInfoSize:=GetFileVersionInfoSizeW(pWideChar(UTF8Decode(ParamStr(0))),aDword);
{$ENDIF}
if vInfoSize=0 then exit;
GetMem(vInfo,vInfoSize);
{$IFDEF FPC}
GetFileVersionInfoW(pWideChar(UTF8Decode(ParamStrUTF8(0))),0,vInfoSize,vInfo);
{$ELSE}
GetFileVersionInfoW(pWideChar(UTF8Decode(ParamStr(0))),0,vInfoSize,vInfo);
{$ENDIF}
VerQueryValueW(vInfo,'\',pointer(vValue),vValueSize);
Result[0]:=vValue^.dwFileVersionMS shr 16;
Result[1]:=vValue^.dwFileVersionMS and $FFFF;
Result[2]:=vValue^.dwFileVersionLS shr 16;
Result[3]:=vValue^.dwFileVersionLS and $FFFF ;
FreeMem(vInfo,vInfoSize);
except
Result[0]:=-1;
end;
end;
{$ELSE}
begin
{$message warn : Not impleted on your OS }
Result[0]:=0;
Result[1]:=0;
Result[2]:=0;
Result[3]:=0;
end;
{$endif}
//------------------------------------------------------------------------------
// Retourne les N° de version de l'application en cours sous forme de chaine de caratères
function GetVersionExeAsString: string;
var
nVer: TInt4Array;
begin
nVer := getCurrentNumVer;
Result := IntToStr(nVer[0]) + '.' + IntToStr(nVer[1]) + '.'
+ IntToStr(nVer[2]) + '.' + IntToStr(nVer[3]);
end;
// Retourne les informations sur l'application en cours
function getAppInfosAsString:String;
var
FileVerInfo: TFileVersionInfo;
s:string;
begin
FileVerInfo:=TFileVersionInfo.Create(nil);
result:='Pas d''informations';
try
s:='';
FileVerInfo.FileName:=getAppFileName();
FileVerInfo.ReadFileInfo;
s:=s+'Nom originel : '+FileVerInfo.VersionStrings.Values['OriginalFilename']+#13#10;
s:=s+'Companie : '+FileVerInfo.VersionStrings.Values['CompanyName']+#13#10;
s:=s+'Description : '+FileVerInfo.VersionStrings.Values['FileDescription']+#13#10;
s:=s+'Version : '+FileVerInfo.VersionStrings.Values['FileVersion']+#13#10;
s:=s+'Copyright : '+FileVerInfo.VersionStrings.Values['LegalCopyright']+#13#10;
s:=s+'Nom interne : '+FileVerInfo.VersionStrings.Values['InternalName']+#13#10;
s:=s+'Nom du produit : '+FileVerInfo.VersionStrings.Values['ProductName']+#13#10;
s:=s+'Version du produit : '+FileVerInfo.VersionStrings.Values['ProductVersion']+#13#10;
finally
FileVerInfo.Free;
result:=s;
end;
end;
//------------------------------------------------------------------------------
// Retourne les lettres des disques sous forme de chaine de cractères séparé par une virgule
function GetDriveLetters:string ;
{$IFDEF WINDOWS}
var
vDrivesSize: cardinal;
vDrives: array[0..128] of char;
vDrive: pChar;
s:string;
begin
try
s:='';
vDrivesSize:=GetLogicalDriveStrings(SizeOf(vDrives),vDrives);
if vDrivesSize=0 then // no drive found, no further processing needed
begin
result:='';
exit;
end;
vDrive:=vDrives;
while vDrive^<>#0 do
begin
if (s<>'') then s:=s+', ';
s:=s+StrPas(vDrive);
inc(vDrive,Length(vDrive)+1);
end;
finally
result:=s;
end;
end;
{$ELSE}
begin
{$message warn : Not implemented on you OS}
result:='Not implemented';
end;
{$ENDIF}
//------------------------------------------------------------------------------
// Retourne le nom d'un disque
function GetVolumeLabel (DriveChar: char): string;
{$IFDEF WINDOWS}
var
NotUsed: DWORD;
VolumeFlags: DWORD;
VolumeSerialNumber: DWORD;
Buf: array [0..MAX_PATH] of char;
begin
Result:='';
NotUsed:=0;
VolumeFlags:=0;
GetVolumeInformation(pChar(DriveChar + ':\'),
Buf,
SizeOf(Buf),
@VolumeSerialNumber,
NotUsed,
VolumeFlags,
nil,
0);
SetString(Result,Buf,StrLen(Buf));
end;
{$ELSE}
begin
{$message warn : Not implemented on you OS}
result:='Not implemented';
end;
{$ENDIF}
//------------------------------------------------------------------------------
// Retourne des informations sur la mémoire, sous forme de chaine de caratères
function GetMemoryStatusAsString:String;
{$IFDEF WINDOWS}
var mSS: TMemoryStatusEx;
dkFree,
dkTotal: int64;
// i: integer;
// aPitem: PParseItem;
s:string;
begin
dkFree := DiskFree(0); // drive 0 = current;
dkTotal := DiskSize(0);
try
mSS.dwLength := SizeOf(mSS);
GlobalMemoryStatusEx(mSS);
s:='';
With mSS Do
begin
s:=s+Format(' Mémoire Total : %2.0n MB',[mSS.ullTotalPhys/OneMegabyte])+#13#10;
s:=s+Format(' Mémoire Libre : %2.0n MB => utilisée : %d%%',[mSS.ullAvailPhys/OneMegabyte,mSS.dwMemoryLoad])+#13#10;
s:=s+#13#10;
s:=s+Format(' Fichier Swap Total : %2.0n MB',[ullTotalPageFile/OneMegabyte])+#13#10;
s:=s+Format(' Fichier Swap Libre : %2.0n MB',[ullAvailPageFile/OneMegabyte])+#13#10;
s:=s+#13#10;
s:=s+Format(' Mémoire Virtuelle Total : %2.0n MB',[ullTotalVirtual/OneMegabyte])+#13#10;
s:=s+Format(' Mémoire Virtuelle Libre : %2.0n MB',[ullAvailVirtual/OneMegabyte])+#13#10;
s:=s+#13#10;
s:=s+Format(' Disque Total : %2.0n MB',[dkTotal/OneMegabyte])+#13#10;
s:=s+Format(' Disque Libre : %2.0n MB (%f%%)',[dkFree/OneMegabyte,(dkFree/dkTotal)*100]);
end;
finally
result:=s;
end;
end;
{$ELSE}
begin
{$message warn : Not implemented on you OS}
result:='Not implemented';
end;
{$ENDIF}
//------------------------------------------------------------------------------
function GetCPUBrandString : string;
{$IFDEF CPU32}
var s:array[0..48] of ansichar;
begin
fillchar(s,sizeof(s),0);
//if CPUIDAvail then begin
asm
//check if necessary extended CPUID calls are
//supported, if not return null string
mov eax,080000000h
CPUID
cmp eax,080000004h
jb @@endbrandstr
//get first name part
mov eax,080000002h
CPUID
mov longword(s[0]),eax
mov longword(s[4]),ebx
mov longword(s[8]),ecx
mov longword(s[12]),edx
//get second name part
mov eax,080000003h
CPUID
mov longword(s[16]),eax
mov longword(s[20]),ebx
mov longword(s[24]),ecx
mov longword(s[28]),edx
//get third name part
mov eax,080000004h
CPUID
mov longword(s[32]),eax
mov longword(s[36]),ebx
mov longword(s[40]),ecx
mov longword(s[44]),edx
@@endbrandstr:
end;
result:=string(s);
{$ELSE}
begin
result := 'Processeur AMD/INTEL 64 bit';
{$ENDIF}
end;
//------------------------------------------------------------------------------
// Retourne le nom du CPU, sous forme de chaine de caratères
function GetCPUName : string;
var s : string;
begin
s := GetCPUBrandString;
{$IFDEF CPU32}
if s <> '' then begin
s := stringreplace(s,' ','',[rfReplaceAll]);
s := stringreplace(s,'CPU','',[rfIgnoreCase]);
s := stringreplace(s,'Processor','',[rfIgnoreCase]);
s := stringreplace(s,'(R)','',[rfReplaceAll,rfIgnoreCase]);
s := stringreplace(s,'(TM)','',[rfReplaceAll,rfIgnoreCase]);
s := stringreplace(s,' ',' ',[rfReplaceAll]);
if pos('@',s) <> 0 then delete(s,pos('@',s),10);
result := trimright(s);
end
else result := s;
{$ELSE}
result := s;
{$ENDIF}
end;
//------------------------------------------------------------------------------
// Retourne le nombres de thread maximum (nombre de coeur du processeur)
function GetMaxThreads : Byte;
begin
result := strtointdef(sysutils.GetEnvironmentVariable('NUMBER_OF_PROCESSORS'), 1);
end;
//------------------------------------------------------------------------------
// Retourne le répertoire temporaire de l'OS
function GetTempFolderPath : string;
{$IFDEF WINDOWS}
var lng: DWORD; thePath: string;
begin
SetLength(thePath, MAX_PATH);
lng := GetTempPath(MAX_PATH, PChar(thePath));
SetLength(thePath, lng);
result := thePath;
end;
{$ELSE}
begin
{$message warn : Not implemented on you OS}
result:='Not implemented';
end;
{$ENDIF}
{$IFDEF LINUX}
function ExtractOSVersionFromSW_Vers(Index: Byte): String;
// Index 0: ProductName, 1: ProductVersion, 2: BuildVersion
var
ProfProcess: TProcess;
path: string;
ProfStringList: TStringList;
begin
Result := '';
path := 'sh -c "/usr/bin/sw_vers"';
try
ProfProcess := Tprocess.Create(nil);
ProfProcess.CommandLine := path;
ProfProcess.Options := ProfProcess.Options + [poUsePipes, poWaitOnExit];
ProfProcess.Execute;
except
exit;
end;
try
ProfStringList := TStringList.Create;
ProfStringList.LoadFromStream(ProfProcess.Output);
except
exit;
end;
ProfProcess.Terminate(0);
ProfStringList.NameValueSeparator:=':';
Result:=Trim(ProfstringList.ValueFromIndex[Index]);
FreeAndNil(ProfProcess);FreeAndNil(ProfStringList);
end;
{$ENDIF}
//==============================================================================
initialization
// preparation for high resolution timer
{$IFDEF UNIX}
Init_vProgStartSecond;
{$ENDIF}
QueryPerformanceFrequency(PerformanceFrequency);
//NowPreciseLock := TCriticalSection.Create;
finalization
//NowPreciseLock.Free;
end. |
Partager