function osver: string;
begin
result := 'Unknown (Windows ' + IntToStr(Win32MajorVersion) + '.' + IntToStr(Win32MinorVersion) + ')';
case Win32MajorVersion of
4:
case Win32MinorVersion of
0: result := 'Windows 95';
10: result := 'Windows 98';
90: result := 'Windows ME';
end;
5:
case Win32MinorVersion of
0: result := 'Windows 2000';
1: result := 'Windows XP';
end;
6:
case Win32MinorVersion of
0: result := 'Windows Vista';
1: result := 'Windows 7';
2: result := 'Windows 8';
3: result := 'Windows 8.1';
end;
10:
case Win32MinorVersion of
0: result := 'Windows 10';
end;
end;
end;
function GetWin32_OSName:string;
var
objWMIService : OLEVariant;
colItems : OLEVariant;
colItem : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
function GetWMIObject(const objectName: String): IDispatch;
var
chEaten: Integer;
BindCtx: IBindCtx;
Moniker: IMoniker;
begin
OleCheck(CreateBindCtx(0, bindCtx));
OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
end;
begin
objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
colItems := objWMIService.ExecQuery('SELECT Caption FROM Win32_OperatingSystem','WQL',0);
oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;
if oEnum.Next(1, colItem, iValue) = 0 then
Result:=colItem.Caption; //The caption property return an string wich includes the operating system version. For example, "Microsoft Windows XP Professional Version = 5.1.2500".
end;
{$J+}
TYPE
TOperatingSystemClass = (osUntested,osUnknown,osWin95,osWin98,osWinME,osWinNT,osWin2000,osWinXP,osWinXPSP1,osWinXPSP2,osWinXPSP3,osWin2003,osWinVista,osWinVistaSP1,osWinVistaSP2,osWinVistaSP3,osWin7,osWin7SP1,osWin7SP2,osWin7SP3,osHigher);
FUNCTION OperatingSystemClass : TOperatingSystemClass;
CONST
OSClass : TOperatingSystemClass = osUntested;
VAR
Info : OSVERSIONINFOEX;
OldInfo : OSVERSIONINFO ABSOLUTE Info;
BEGIN
IF OSClass=osUntested THEN BEGIN
FillChar(Info,SizeOf(Info),0);
Info.dwOSVersionInfoSize:=SizeOf(Info); OSClass:=osUnknown;
IF NOT GetVersionEx(OldInfo) THEN BEGIN
FillChar(OldInfo,SizeOf(OldInfo),0);
OldInfo.dwOSVersionInfoSize:=SizeOf(OldInfo);
GetVersionEx(OldInfo)
END;
CASE Info.dwPlatformId OF
VER_PLATFORM_WIN32_WINDOWS : CASE Info.dwMajorVersion OF
3 : OSClass:=osWin95;
4 : CASE Info.dwMinorVersion OF
0 : OSClass:=osWin95;
10 : OSClass:=osWin98
ELSE // OTHERWISE //
OSClass:=osWinME
END
END;
VER_PLATFORM_WIN32_NT : CASE Info.dwMajorVersion OF
5 : CASE Info.dwMinorVersion OF
0 : OSClass:=osWin2000;
1 : OSClass:=osWinXP;
2 : OSClass:=osWin2003
END;
6 : IF Info.dwMinorVersion=0 THEN
OSClass:=osWinVista
ELSE IF Info.dwMinorVersion=1 THEN
OSClass:=osWin7
ELSE
OSClass:=osHigher
END
END;
IF (OSClass IN [osWinXP,osWinVista,osWin7]) AND (Info.wServicePackMajor>0) THEN
INC(OSClass,MAX(Info.wServicePackMajor,3))
END;
Result:=OSClass
END;
function OS_IsWindowsXP: Boolean;
begin
Result:= (Win32MajorVersion= 5) AND (Win32MinorVersion= 1);
end;
function OS_IsWindowsXPUp: Boolean;
begin
Result:= ((Win32MajorVersion = 5) AND (Win32MinorVersion>= 1))
OR (Win32MajorVersion>= 6);
end;
function OS_IsWindowsVista: Boolean;
begin
Result:= (Win32MajorVersion= 6) AND (Win32MinorVersion= 0);
end;
function OS_IsWindowsVistaUp: Boolean;
begin
Result:= (Win32MajorVersion>= 6);
end;
function OS_IsWindows7: Boolean;
begin
Result:= (Win32MajorVersion= 6) AND (Win32MinorVersion= 1);
end;
function OS_IsWindows7Up: Boolean;
begin
Result:= ((Win32MajorVersion = 6) AND (Win32MinorVersion>= 1)) { Any version 6 equal and above Win7 }
OR (Win32MajorVersion>= 7); { Any version above 6 }
end;
function OS_IsWindows8: Boolean;
begin
Result:= (Win32MajorVersion = 6) AND (Win32MinorVersion= 2);
end;
function OS_IsWindows8Up: Boolean;
begin
Result:= ((Win32MajorVersion = 6) AND (Win32MinorVersion>= 2))
OR (Win32MajorVersion>= 7);
end;
7条答案
按热度按时间jtjikinw1#
大概是这样的:
实际上没有必要调用
GetVersionEx
,因为SysUtils.pas
在它的initialization
子句中有InitPlatformID
,因此全局常量Win32MajorVersion
和Win32MinorVersion
(以及友元)已经被填充了。ne5o7dgx2#
作为Win32 API的替代方法,您可以使用WMI
Win32_OperatingSystem
类。您可以编写一个类似于下面的简单函数来检索操作Windows版本名称。
此外,您还可以使用Win32_OperatingSystem类的其他属性检索有关Windows版本的更多信息。
检查此代码
nxagd54h3#
我在我的程序中使用了以下代码:
其中
gzszwxb44#
如果你想获得更多关于操作系统的信息,你可以使用WMI(RRUZ的代码,用它来命名)。更多的信息,你可以使用GLibWMI的组件COperatingSystemInfo(来自Sourceforge或Author WebPage(mine);-D)的规定。
所有的库都是免费的,源代码可用。对于操作系统,您可以获得以下属性:
alt text http://a.imageshack.us/img594/1660/coperatinsysteminfo.png
此致
gkn4icbw5#
由于Delphi XE2,您可以使用
TOSVersion
记录。请注意,它是一个全局记录,并且是you cannot create TOSVersion objects。例如:
您可以使用的记录属性很少:
Minor
、Major
、Build
、Architecture
与Platform
一起提供,您可以使用它们来确保操作系统是Windows。要将系统的当前版本与定义的版本进行比较,请使用
Check
版本。zd287kbt6#
JCL包含以下枚举类型:
它包含此函数和其他函数:
Documentation here
更好的是,当Windows的新版本发布时,有人会更新JCL,而您不必自己扩展此代码。此外,在这个问题的所有答案中,除了通常的Windows最终用户版本之外,只有JCL处理Windows Server操作系统检测。
v8wbuo2f7#