获取 Windows 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8144599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:56:23  来源:igfitidea点击:

Getting the Windows version?

windowsdelphiwinapi

提问by David Heffernan

Can anyone help me detect which version of Windows the user may be using?

谁能帮我检测用户可能使用的是哪个版本的 Windows?

I have seen some examples to do this, but they are not updated for Vista/7 Operating Systems.

我已经看到了一些这样做的例子,但它们没有针对 Vista/7 操作系统进行更新。

Additionally it would be useful to detect if the OS is running on x32 or x64 architecture.

此外,检测操作系统是在 x32 还是 x64 架构上运行会很有用。

Thanks in advance.

提前致谢。

回答by David Heffernan

On XE2 a new class was introduced to deal with this: TOSVersion.

在 XE2 上引入了一个新类来处理这个问题:TOSVersion.

  • Read TOSVersion.Architectureto check for 32 or 64 bit OS.
  • Read TOSVersion.Platformto check for Windows or Mac.
  • Read TOSVersion.Majorand TOSVersion.Minorfor version numbers.
  • Read TOSVersion.Nameto obtain the basic product name, e.g. Windows 7.
  • Read TOSVersion.ToStringto obtain the full product name with version, e.g. Windows 7 Service Pack 1 (Version 6.1, Build 7601, 64-bit Edition).
  • 阅读TOSVersion.Architecture以检查 32 位或 64 位操作系统。
  • 阅读TOSVersion.Platform以检查 Windows 或 Mac。
  • 阅读TOSVersion.MajorTOSVersion.Minor获取版本号。
  • 阅读TOSVersion.Name以获取基本产品名称,例如 Windows 7。
  • 阅读TOSVersion.ToString以获取带有版本的完整产品名称,例如 Windows 7 Service Pack 1(版本 6.1,Build 7601,64 位版本)。


For older versions of Delphi I recommend the following:

对于旧版本的 Delphi,我推荐以下内容:

In order to check for 2000, XP, Vista, 7 I suggest you read Win32MajorVersionand Win32MinorVersion.

为了检查 2000、XP、Vista、7,我建议您阅读Win32MajorVersionWin32MinorVersion.

  • major.minor = 5.0 => Windows 2000
  • major.minor = 5.1 => Windows XP
  • major.minor = 5.2 => Windows 2003 server or XP64
  • major.minor = 6.0 => Windows Vista/2008 server
  • major.minor = 6.1 => Windows 7/2008 server R2
  • Major.minor = 5.0 => Windows 2000
  • Major.minor = 5.1 => Windows XP
  • Major.minor = 5.2 => Windows 2003 服务器或 XP64
  • Major.minor = 6.0 => Windows Vista/2008 服务器
  • Major.minor = 6.1 => Windows 7/2008 服务器 R2

The same information is available on MSDN, but the above came from my head!

MSDN上提供了相同的信息,但以上内容来自我的头脑!

If you are wanting very detailed product information then that takes a bit more work. Warren's answer gives one good route to obtaining that information. If you are wanting to test capability then version numbers are fine.

如果您想要非常详细的产品信息,则需要做更多的工作。沃伦的回答提供了一条获得该信息的好途径。如果您想测试功能,那么版本号就可以了。

Use CheckWin32Versionto check if the prevailing OS exceeds a certain version level. Although you should check that the function works correctly in your Delphi since the implementation of that function in Delphi 6 and earlier was incorrect.

使用CheckWin32Version检查,如果当时的OS超过某一版本级别。尽管您应该检查该函数在您的 Delphi 中是否正常工作,因为该函数在 Delphi 6 及更早版本中的实现是不正确的。

To find out what the native OS architecture is (32 or 64 bit), use the GetNativeSystemInfofunction. This function is not available on older operating systems so you should load it explicitly with GetProcAddress. Test for wProcessorArchitecture=PROCESSOR_ARCHITECTURE_AMD64to check for 64 bit OS.

要了解本机操作系统架构是什么(32 位或 64 位),请使用该GetNativeSystemInfo函数。此功能在较旧的操作系统上不可用,因此您应该使用GetProcAddress. 测试wProcessorArchitecture=PROCESSOR_ARCHITECTURE_AMD64以检查 64 位操作系统。

回答by Warren P

The JEDI JCL already does this, even on versions older than XE2. See David's answer for the built-in solution in XE2 and later.

JEDI JCL 已经这样做了,即使在比 XE2 更旧的版本上也是如此。请参阅 David 对 XE2 及更高版本中的内置解决方案的回答。

Using the Jedi JCL, you can add unit JclSysInfo, and call function GetWindowsVersion. It returns an enumerated type TWindowsVersion.

使用 Jedi JCL,您可以添加单元 JclSysInfo,并调用函数GetWindowsVersion. 它返回一个枚举类型 TWindowsVersion。

Currently JCL contains all shipped windows versions, and gets changed each time Microsoft ships a new version of Windows in a box:

目前,JCL 包含所有已发布的 Windows 版本,并且每次 Microsoft 在一个盒子中发布新版本的 Windows 时都会更改:

  TWindowsVersion =
   (wvUnknown, wvWin95, wvWin95OSR2, wvWin98, wvWin98SE, wvWinME,
    wvWinNT31, wvWinNT35, wvWinNT351, wvWinNT4, wvWin2000, wvWinXP,
    wvWin2003, wvWinXP64, wvWin2003R2, wvWinVista, wvWinServer2008,
    wvWin7, wvWinServer2008R2);

If you want to know if you're running 64-bit windows 7 instead of 32-bit, then call JclSysInfo.IsWindows64.

如果您想知道您运行的是 64 位 Windows 7 而不是 32 位,请调用JclSysInfo.IsWindows64.

Note that JCL allso handles Editions, like Pro, Ultimate, etc. For that call GetWindowsEdition, and it returns one of these:

请注意,JCL 也处理版本,如 Pro、Ultimate 等。对于该调用 GetWindowsEdition,它返回以下之一:

TWindowsEdition =
   (weUnknown, weWinXPHome, weWinXPPro, weWinXPHomeN, weWinXPProN, weWinXPHomeK,
    weWinXPProK, weWinXPHomeKN, weWinXPProKN, weWinXPStarter, weWinXPMediaCenter,
    weWinXPTablet, weWinVistaStarter, weWinVistaHomeBasic, weWinVistaHomeBasicN,
    weWinVistaHomePremium, weWinVistaBusiness, weWinVistaBusinessN,
    weWinVistaEnterprise, weWinVistaUltimate, weWin7Starter, weWin7HomeBasic,
    weWin7HomePremium, weWin7Professional, weWin7Enterprise, weWin7Ultimate);

For historical interest, you can check the NT-level edition too with the NtProductType function, it returns:

出于历史兴趣,您也可以使用 NtProductType 函数检查 NT 级别版本,它返回:

 TNtProductType =? ? ? ?(ptUnknown, ptWorkStation, ptServer, ptAdvancedServer,? ? ? ? 
        ptPersonal, ptProfessional, ptDatacenterServer, 
        ptEnterprise, ptWebEdition);

Note that "N editions" are detected above. That's an EU (Europe) version of Windows, created due to EU anti-trust regulations. That's a pretty fine gradation of detection inside the JCL.

请注意,上面检测到“N 个版本”。这是欧盟(欧洲)版本的 Windows,根据欧盟反垄断法规创建。这是 JCL 内部非常精细的检测等级。

Here's a sample function that will help you detect Vista, and do something special when on Vista.

这是一个示例函数,可帮助您检测 Vista,并在 Vista 上执行一些特殊操作。

function IsSupported:Boolean;
begin
  case GetWindowsVersion of
     wvVista:  result := false; 
    else
      result := true;
  end;
end;

Note that if you want to do "greater than" checking, then you should just use other techniques. Also note that version checking can often be a source of future breakage. I have usually chosen to warn users and continue, so that my binary code doesn't become the actual source of breakage in the future.

请注意,如果您想做“大于”检查,那么您应该只使用其他技术。另请注意,版本检查通常可能是未来损坏的来源。我通常选择警告用户并继续,这样我的二进制代码就不会成为将来破坏的实际来源。

Recently I tried to install an app, and the installer checked my drive free space, and would not install, because I had more than 2 gigabytes of free space. The 32 bit integer signed value in the installer became negative, breaking the installer. I had to install it into a VM to get it to work. Adding "smart code" often makes your app "stupider". Be wary.

最近我尝试安装一个应用程序,安装程序检查了我的驱动器可用空间,但不会安装,因为我有超过 2 GB 的可用空间。安装程序中的 32 位整数有符号值变为负数,从而破坏了安装程序。我必须将它安装到 VM 中才能使其工作。添加“智能代码”通常会使您的应用程序变得“愚蠢”。警惕。

Incidentally, I found that from the command line, you can run WMIC.exe, and type path Win32_OperatingSystem(The "Select * from Win32_OperatingSystem" didn't work for me). In future perhaps JCL could be extended to use the WMI information.

顺便说一句,我发现从命令行,您可以运行 WMIC.exe,然后键入path Win32_OperatingSystem(“从 Win32_OperatingSystem 中选择 *”对我不起作用)。将来也许可以扩展 JCL 以使用 WMI 信息。

回答by Vic Adam

The unit (GetWinVersionInfo) shown heredetects up to Vista. I can't imagine updating it for Windows 7 would be that difficult. I don't think it tells you x86.x64 though.

此处显示的单元 (GetWinVersionInfo)最多可检测到 Vista。我无法想象为 Windows 7 更新它会那么困难。不过,我认为它不会告诉您 x86.x64。

回答by Name

On delphidabbler.com there is a complete articleabout getting operating system version including 64 bits versions. The article hasn't been updated to include Vista/7 though. But the same functions might still work.

在 delphidabbler.com 上有一篇关于获取操作系统版本(包括 64 位版本)的完整文章。不过,该文章尚未更新以包含 Vista/7。但相同的功能可能仍然有效。