線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3552
推到 Plurk!
推到 Facebook!

請問如何詳細辨識 OS 版本

答題得分者是:hagar
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-08-30 11:45:22 IP:210.69.xxx.xxx 未訂閱
這是微軟 MSDN 網站上資料    
引言: Getting the System Version The following example uses the GetVersionEx function to display the version of the currently running operating system. Relying on version information is not the best way to test for a feature. Instead, refer to the documentation for the feature of interest. For more information on common techniques for feature detection, see Operating System Version. If you must require a particular operating system, be sure to use it as a minimum supported version, rather than design the test for the one operating system. This way, your detection code will continue to work on future versions of Windows. #include #include #define BUFSIZE 80 int main() { OSVERSIONINFOEX osvi; BOOL bOsVersionInfoEx; // Try calling GetVersionEx using the OSVERSIONINFOEX structure. // If that fails, try using the OSVERSIONINFO structure. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) ) { osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) return FALSE; } switch (osvi.dwPlatformId) { // Test for the Windows NT product family. case VER_PLATFORM_WIN32_NT: // Test for the specific product. if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) printf ("Microsoft Windows Server 2003, "); if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) printf ("Microsoft Windows XP "); if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) printf ("Microsoft Windows 2000 "); if ( osvi.dwMajorVersion <= 4 ) printf("Microsoft Windows NT "); // Test for specific product on Windows NT 4.0 SP6 and later. if( bOsVersionInfoEx ) { // Test for the workstation type. if ( osvi.wProductType == VER_NT_WORKSTATION ) { if( osvi.dwMajorVersion == 4 ) printf ( "Workstation 4.0 " ); else if( osvi.wSuiteMask & VER_SUITE_PERSONAL ) printf ( "Home Edition " ); else printf ( "Professional " ); } // Test for the server type. else if ( osvi.wProductType == VER_NT_SERVER || osvi.wProductType == VER_NT_DOMAIN_CONTROLLER ) { if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) printf ( "Datacenter Edition " ); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) printf ( "Enterprise Edition " ); else if ( osvi.wSuiteMask == VER_SUITE_BLADE ) printf ( "Web Edition " ); else printf ( "Standard Edition " ); } else if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) printf ( "Datacenter Server " ); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) printf ( "Advanced Server " ); else printf ( "Server " ); } else // Windows NT 4.0 { if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) printf ("Server 4.0, Enterprise Edition " ); else printf ( "Server 4.0 " ); } } } else // Test for specific product on Windows NT 4.0 SP5 and earlier { HKEY hKey; char szProductType[BUFSIZE]; DWORD dwBufLen=BUFSIZE; LONG lRet; lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey ); if( lRet != ERROR_SUCCESS ) return FALSE; lRet = RegQueryValueEx( hKey, "ProductType", NULL, NULL, (LPBYTE) szProductType, &dwBufLen); if( (lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE) ) return FALSE; RegCloseKey( hKey ); if ( lstrcmpi( "WINNT", szProductType) == 0 ) printf( "Workstation " ); if ( lstrcmpi( "LANMANNT", szProductType) == 0 ) printf( "Server " ); if ( lstrcmpi( "SERVERNT", szProductType) == 0 ) printf( "Advanced Server " ); printf( "%d.%d ", osvi.dwMajorVersion, osvi.dwMinorVersion ); } // Display service pack (if any) and build number. if( osvi.dwMajorVersion == 4 && lstrcmpi( osvi.szCSDVersion, "Service Pack 6" ) == 0 ) { HKEY hKey; LONG lRet; // Test for SP6 versus SP6a. lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009", 0, KEY_QUERY_VALUE, &hKey ); if( lRet == ERROR_SUCCESS ) printf( "Service Pack 6a (Build %d)\n", osvi.dwBuildNumber & 0xFFFF ); else // Windows NT 4.0 prior to SP6a { printf( "%s (Build %d)\n", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); } RegCloseKey( hKey ); } else // not Windows NT 4.0 { printf( "%s (Build %d)\n", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); } break; // Test for the Windows Me/98/95. case VER_PLATFORM_WIN32_WINDOWS: if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) { printf ("Microsoft Windows 95 "); if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' ) printf("OSR2 " ); } if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) { printf ("Microsoft Windows 98 "); if ( osvi.szCSDVersion[1] == 'A' ) printf("SE " ); } if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) { printf ("Microsoft Windows Millennium Edition\n"); } break; case VER_PLATFORM_WIN32s: printf ("Microsoft Win32s\n"); break; } return TRUE; }
這是 Delphi Win32 Help
引言: The OSVERSIONINFO data structure contains operating system version information. The information includes major and minor version numbers, a build number, a platform identifier, and descriptive text about the operating system. This structure is used with the GetVersionEx function. typedef struct _OSVERSIONINFO{ DWORD dwOSVersionInfoSize; DWORD dwMajorVersion; DWORD dwMinorVersion; DWORD dwBuildNumber; DWORD dwPlatformId; TCHAR szCSDVersion[ 128 ]; } OSVERSIONINFO; Members dwOSVersionInfoSize Specifies the size, in bytes, of this data structure. Set this member to sizeof(OSVERSIONINFO) before calling the GetVersionEx function. dwMajorVersion Identifies the major version number of the operating system. For example, for Windows NT version 3.51, the major version number is 3; and for Windows NT version 4.0, the major version number is 4. dwMinorVersion Identifies the minor version number of the operating system. For example, for Windows NT version 3.51, the minor version number is 51; and for Windows NT version 4.0, the minor version number is 0. dwBuildNumber Windows NT: Identifies the build number of the operating system. Windows 95: Identifies the build number of the operating system in the low-order word. The high-order word contains the major and minor version numbers. dwPlatformId Identifies the operating system platform. This member can be one of the following values: Value Platform VER_PLATFORM_WIN32s Win32s on Windows 3.1. VER_PLATFORM_WIN32_WINDOWS Win32 on Windows 95. VER_PLATFORM_WIN32_NT Win32 on Windows NT. szCSDVersion Windows NT: Contains a null-terminated string, such as "Service Pack 3", that indicates the latest Service Pack installed on the system. If no Service Pack has been installed, the string is empty. Windows 95: Contains a null-terminated string that provides arbitrary additional information about the operating system. See Also GetVersionEx
小弟參考寫出
引言: var OS :TOSVersionInfo; begin // ZeroMemory(@OS,SizeOf(OS)); OS.dwOSVersionInfoSize:=SizeOf(OS); GetVersionEx(OS); Memo1.Clear; Memo1.Lines.Add(IntToStr(OS.dwOSVersionInfoSize)); Memo1.Lines.Add(IntToStr(OS.dwMajorVersion)); Memo1.Lines.Add(IntToStr(OS.dwMinorVersion)); Memo1.Lines.Add(IntToStr(OS.dwBuildNumber)); Memo1.Lines.Add(IntToStr(OS.dwPlatformId)); Memo1.Lines.Add(OS.szCSDVersion); // Memo1.Lines.Add(bOsVersionInfoEx); // Memo1.Lines.Add(OS.wProductType); end;
請問 wProductType 是否不支援, 如果要判斷是否為 WorkStation , Professional 和其他資訊, 在 Delphi 中要如何寫? 謝謝 發表人 - pcboy2 於 2004/08/30 12:02:06
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-08-30 18:54:55 IP:202.39.xxx.xxx 未訂閱
參考這篇試試: http://www.swissdelphicenter.ch/torry/showcode.php?id=316
{************************************************} 
{ Function to detect OS Version by Nico Bendlin  } 
{************************************************}     {$IFDEF CONDITIONALEXPRESSIONS} 
  {$IF Defined(TOSVersionInfoEx)} 
    {$DEFINE TOSVERSIONINFOEX_DEFINED} 
  {$IFEND} 
{$ENDIF} 
{$IFNDEF TOSVERSIONINFOEX_DEFINED}     type 
  POSVersionInfoEx = ^TOSVersionInfoEx; 
  TOSVersionInfoEx = packed record 
    dwOSVersionInfoSize: DWORD; 
    dwMajorVersion     : DWORD; 
    dwMinorVersion     : DWORD; 
    dwBuildNumber      : DWORD; 
    dwPlatformId       : DWORD; 
    szCSDVersion       : array [0..127] of AnsiChar; 
    wServicePackMajor  : Word; 
    wServicePackMinor  : Word; 
    wSuiteMask         : Word; 
    wProductType       : Byte; 
    wReserved          : Byte; 
  end;     const 
  VER_SERVER_NT                       = $80000000; 
  {$EXTERNALSYM VER_SERVER_NT} 
  VER_WORKSTATION_NT                  = $40000000; 
  {$EXTERNALSYM VER_WORKSTATION_NT} 
  VER_SUITE_SMALLBUSINESS             = $00000001; 
  {$EXTERNALSYM VER_SUITE_SMALLBUSINESS} 
  VER_SUITE_ENTERPRISE                = $00000002; 
  {$EXTERNALSYM VER_SUITE_ENTERPRISE} 
  VER_SUITE_BACKOFFICE                = $00000004; 
  {$EXTERNALSYM VER_SUITE_BACKOFFICE} 
  VER_SUITE_COMMUNICATIONS            = $00000008; 
  {$EXTERNALSYM VER_SUITE_COMMUNICATIONS} 
  VER_SUITE_TERMINAL                  = $00000010; 
  {$EXTERNALSYM VER_SUITE_TERMINAL} 
  VER_SUITE_SMALLBUSINESS_RESTRICTED  = $00000020; 
  {$EXTERNALSYM VER_SUITE_SMALLBUSINESS_RESTRICTED} 
  VER_SUITE_EMBEDDEDNT                = $00000040; 
  {$EXTERNALSYM VER_SUITE_EMBEDDEDNT} 
  VER_SUITE_DATACENTER                = $00000080; 
  {$EXTERNALSYM VER_SUITE_DATACENTER} 
  VER_SUITE_SINGLEUSERTS              = $00000100; 
  {$EXTERNALSYM VER_SUITE_SINGLEUSERTS} 
  VER_SUITE_PERSONAL                  = $00000200; 
  {$EXTERNALSYM VER_SUITE_PERSONAL} 
  VER_SUITE_BLADE                     = $00000400; 
  {$EXTERNALSYM VER_SUITE_BLADE} 
  VER_SUITE_EMBEDDED_RESTRICTED       = $00000800; 
  {$EXTERNALSYM VER_SUITE_EMBEDDED_RESTRICTED} 
  VER_SUITE_SECURITY_APPLIANCE        = $00001000; 
  {$EXTERNALSYM VER_SUITE_SECURITY_APPLIANCE}     const 
  VER_NT_WORKSTATION              = $0000001; 
  {$EXTERNALSYM VER_NT_WORKSTATION} 
  VER_NT_DOMAIN_CONTROLLER        = $0000002; 
  {$EXTERNALSYM VER_NT_DOMAIN_CONTROLLER} 
  VER_NT_SERVER                   = $0000003; 
  {$EXTERNALSYM VER_NT_SERVER}     {$ENDIF}  // TOSVERSIONINFOEX_DEFINED     function GetOSVersionInfo(var Info: TOSVersionInfoEx): Boolean; 
begin 
  FillChar(Info, SizeOf(TOSVersionInfoEx), 0); 
  Info.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx); 
  Result := GetVersionEx(TOSVersionInfo(Addr(Info)^)); 
  if (not Result) then 
  begin 
    FillChar(Info, SizeOf(TOSVersionInfoEx), 0); 
    Info.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx); 
    Result := GetVersionEx(TOSVersionInfo(Addr(Info)^)); 
    if (not Result) then 
      Info.dwOSVersionInfoSize := 0; 
  end; 
end;     function GetOSVersionText: string; 
var 
  Info: TOSVersionInfoEx; 
  Key: HKEY; 
begin 
  Result := ''; 
  if (not GetOSVersionInfo(Info)) then 
    Exit; 
  case Info.dwPlatformId of 
    { Win32s } 
    VER_PLATFORM_WIN32s: 
      Result := 'Microsoft Win32s'; 
    { Windows 9x } 
    VER_PLATFORM_WIN32_WINDOWS: 
      if (Info.dwMajorVersion = 4) and (Info.dwMinorVersion = 0) then 
      begin 
        Result := 'Microsoft Windows 95'; 
        if (Info.szCSDVersion[1] in ['B', 'C']) then 
          Result := Result  ' OSR2'; 
      end 
      else if (Info.dwMajorVersion = 4) and (Info.dwMinorVersion = 10) then 
      begin 
        Result := 'Microsoft Windows 98'; 
        if (Info.szCSDVersion[1] = 'A') then 
          Result := Result   ' SE'; 
      end 
      else if (Info.dwMajorVersion = 4) and (Info.dwMinorVersion = 90) then 
        Result := 'Microsoft Windows Millennium Edition'; 
    { Windows NT } 
    VER_PLATFORM_WIN32_NT: 
      begin 
        { Version } 
        if (Info.dwMajorVersion = 5) and (Info.dwMinorVersion = 2) then 
          Result := 'Microsoft Windows Server 2003' 
        else if (Info.dwMajorVersion = 5) and (Info.dwMinorVersion = 1) then 
          Result := 'Microsoft Windows XP' 
        else if (Info.dwMajorVersion = 5) and (Info.dwMinorVersion = 0) then 
          Result := 'Microsoft Windows 2000' 
        else 
          Result := 'Microsoft Windows NT'; 
        { Extended } 
        if (Info.dwOSVersionInfoSize >= SizeOf(TOSVersionInfoEx)) then 
        begin 
          { ProductType } 
          if (Info.wProductType = VER_NT_WORKSTATION) then 
          begin 
            if (Info.dwMajorVersion = 4) then 
              Result := Result   #10'Workstation 4.0' 
            else if(Info.wSuiteMask and VER_SUITE_PERSONAL <> 0) then 
              Result := Result   #10'Home Edition' 
            else 
              Result := Result   #10'Professional'; 
          end 
          else if (Info.wProductType = VER_NT_SERVER) then 
          begin 
             if (Info.dwMajorVersion = 5) and (Info.dwMinorVersion = 2) then 
             begin 
               if (Info.wSuiteMask and VER_SUITE_DATACENTER <> 0) then 
                 Result := Result   #10'Datacenter Edition' 
               else if (Info.wSuiteMask and VER_SUITE_ENTERPRISE <> 0) then 
                 Result := Result   #10'Enterprise Edition' 
               else if (Info.wSuiteMask = VER_SUITE_BLADE) then 
                 Result := Result   #10'Web Edition' 
               else 
                 Result := Result   #10'Standard Edition'; 
             end 
             else if (Info.dwMajorVersion = 5) and (Info.dwMinorVersion = 0) then 
             begin 
               if (Info.wSuiteMask and VER_SUITE_DATACENTER <> 0) then 
                  Result := Result   #10'Datacenter Server' 
               else if (Info.wSuiteMask and VER_SUITE_ENTERPRISE <> 0) then 
                  Result := Result   #10'Advanced Server' 
               else 
                  Result := Result   #10'Server'; 
             end 
             else 
             begin 
               Result := Result   #10'Server '   
                 IntToStr(Info.dwMajorVersion)   '.'   
                 IntToStr(Info.dwMinorVersion); 
               if (Info.wSuiteMask and VER_SUITE_ENTERPRISE <> 0) then 
                 Result := Result   ', Enterprise Edition'; 
             end; 
          end; 
        end; 
        { CSDVersion } 
        if (Info.dwMajorVersion = 4) and 
          (StrIComp(Info.szCSDVersion, 'Service Pack 6') = 0) and 
          (RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
            'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix\Q246009', 0, 
            KEY_QUERY_VALUE, Key) = ERROR_SUCCESS) then 
        begin 
          Result := Result   #10'Service Pack 6a'; 
          RegCloseKey(Key); 
        end 
        else 
          Result := Result   #10   StrPas(Info.szCSDVersion); 
        Result := Result   #10'(Build '   
          IntToStr(Info.dwBuildNumber and $FFFF)   ')'; 
      end; 
  end; 
end;     ////////////////////////////////////////////////////////////////////////////////     procedure TForm1.Button1Click(Sender: TObject); 
begin 
  ShowMessage(GetOSVersionText); 
end;     
-- 歡迎光臨 KTop 研究院!
系統時間:2024-04-28 22:10:36
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!