Registriert seit: 1. Feb 2018
3.691 Beiträge
Delphi 11 Alexandria
|
AW: Windows Version ermitteln
27. Mai 2024, 19:49
So mache ich es, es liest Major, Minor, Build und Revisionsnummber von Windows aus.
Kompatibel mit Turbo Delphi 2006, FreePascal, RADStudio etc...
Es erscheint nicht "Das ist Windows 11" sondern was auch immer Windows selbst für sich hinterlegt hat als numerischer Wert.
Delphi-Quellcode:
program winverex;
{$APPTYPE CONSOLE}
uses
Windows, SysUtils;
type
TWindowsVersion = packed record
Major,
Minor,
Build,
Revision: DWORD;
end;
function GetWindowsVersion: TWindowsVersion;
type
TVSFixedFileInfo = record
dwSignature,
dwStrucVersion,
dwFileVersionMS,
dwFileVersionLS,
dwProductVersionMS,
dwProductVersionLS,
dwFileFlagsMask,
dwFileFlags,
dwFileOS,
dwFileType,
dwFileSubtype,
dwFileDateMS,
dwFileDateLS: DWORD;
end;
PVSFixedFileInfo = ^TVSFixedFileInfo;
TRtlGetNtVersionNumbers = procedure(out MajorVersion, MinorVersion, BuildNumber: DWORD); stdcall;
var
RtlGetNtVersionNumbers: TRtlGetNtVersionNumbers;
hLib: HINST;
dwHandle: DWORD;
pBuffer: Pointer;
pVersionInfo: PVSFIXEDFILEINFO;
dwLen: DWORD;
WindowsDir: array[0..Pred(MAX_PATH)] of AnsiChar;
WideWindowsDir: WideString;
GotRtlVer: Boolean;
begin
dwHandle := 0;
pVersionInfo := nil;
GotRtlVer := False;
GetWindowsDirectory(WindowsDir, MAX_PATH);
StrCat(WindowsDir, '\System32\Ntdll.dll');
SetString(WideWindowsDir, PAnsiChar(@WindowsDir[0]), Length(WindowsDir));
hLib := LoadLibraryW(PWideChar(WideWindowsDir));
if hLib <> 0 then
begin
RtlGetNtVersionNumbers := TRtlGetNtVersionNumbers(GetProcAddress(hLib, 'RtlGetNtVersionNumbers'));
if Assigned(RtlGetNtVersionNumbers) then
begin
RtlGetNtVersionNumbers(Result.Major, Result.Minor, Result.Build);
GotRtlVer := True;
end;
FreeLibrary(hLib);
end;
Result.Revision := 0;
dwLen := GetFileVersionInfoSizeW(PWideChar(WideWindowsDir), dwHandle);
if dwLen > 0 then
begin
pBuffer := AllocMem(dwLen);
try
if GetFileVersionInfoW(PWideChar(WideWindowsDir), dwHandle, dwLen, pBuffer) then
begin
if VerQueryValueW(pBuffer, '\', Pointer(pVersionInfo), dwLen) then
begin
if not GotRtlVer then
begin
Result.Major := HiWord(pVersionInfo^.dwFileVersionMS);
Result.Minor := LoWord(pVersionInfo^.dwFileVersionMS);
end;
Result.Build := HiWord(pVersionInfo^.dwFileVersionLS);
Result.Revision := LoWord(pVersionInfo^.dwFileVersionLS);
end;
end;
finally
FreeMem(pBuffer);
end;
end;
end;
var
WindowsVersion: TWindowsVersion;
begin
WindowsVersion := GetWindowsVersion;
WriteLn(Format('Windows version: %d.%d.%d.%d', [WindowsVersion.Major, WindowsVersion.Minor, WindowsVersion.Build, WindowsVersion.Revision]));
WriteLn('Done');
ReadLn;
end.
|