Getting the Windows version?

后端 未结 4 1514
一个人的身影
一个人的身影 2020-12-30 03:01

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

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

相关标签:
4条回答
  • 2020-12-30 03:25

    The unit (GetWinVersionInfo) shown here detects 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.

    0 讨论(0)
  • 2020-12-30 03:34

    On delphidabbler.com there is a complete article about 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.

    0 讨论(0)
  • 2020-12-30 03:41

    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.

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

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

      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.

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

    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:

     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.

    Here's a sample function that will help you detect Vista, and do something special when on 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.

    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.

    0 讨论(0)
  • 2020-12-30 03:44

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

    • Read TOSVersion.Architecture to check for 32 or 64 bit OS.
    • Read TOSVersion.Platform to check for Windows or Mac.
    • Read TOSVersion.Major and TOSVersion.Minor for version numbers.
    • Read TOSVersion.Name to obtain the basic product name, e.g. Windows 7.
    • Read TOSVersion.ToString to obtain the full product name with version, e.g. Windows 7 Service Pack 1 (Version 6.1, Build 7601, 64-bit Edition).

    For older versions of Delphi I recommend the following:

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

    • 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

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

    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 CheckWin32Version to 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.

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

    0 讨论(0)
提交回复
热议问题