get windows version – 1

prototype:  api-function: GetVersionEx( *group), bool, pascal, raw, name('GetVersionExA')
procedure:  GetWinVersion(), long
return:     0-error, 1-win32s, 2-win95, 3-win98, 4-nt4, 5-nt2000

code: 
 execute 1+ GetWinVersion()
   s"= 'error in GetWinVersion'
   s"= 'win32s'
   s"= 'win95'
   s"= 'win98'
   s"= 'nt4'
   s"= 'nt2000'
 end
 message( s")

definition:

GetWinVersion     procedure 
ret               long, auto
tosvi             group, type
dwOSVersionInfoSize ulong
dwMajorVersion      ulong
dwMinorVersion      ulong
dwBuildNumber       ulong
dwPlatformId        ulong
szCSDVersion        cstring( 128)
                  end
osviex            group, auto
osvi                group( tosvi).
wServicePackMajor   long
wServicePackMinor   long
wSuiteMask          long
wProductType        byte
wReserved           byte
                  end
 code
 ret= 0
 clear( osviex, -1)
 osviex.osvi.dwOSVersionInfoSize= size( osviex)
 if not GetVersionEx( osviex)
   osviex.osvi.dwOSVersionInfoSize= size( osviex.osvi)
   if not GetVersionEx( osviex) then return ret.
 end
 execute 1+ osviex.osvi.dwPlatformId
   do win32s
   do win9x
   do winnt
 end
 return( ret)  

winnt routine
 if osviex.osvi.dwMajorVersion <= 4
   ret= 4
 elsif osviex.osvi.dwMajorVersion = 5
   ret= 5
 end

win9x routine
 if osviex.osvi.dwMajorVersion > 4 or |
    ( osviex.osvi.dwMajorVersion = 4 and osviex.osvi.dwMinorVersion > 
0)
   ret= 3
 else
   ret= 2
 end

win32s routine
 ret= 1

 omit( '->')
---------------------------------------------------------------------------------------------
BOOL DisplaySystemVersion()
{
   OSVERSIONINFOEX osvi;
   BOOL bOsVersionInfoEx;

   // Try calling GetVersionEx using the OSVERSIONINFOEX structure,
   // which is supported on Windows 2000.
   //
   // If that fails, try using the OSVERSIONINFO structure.

   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

   if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
   {
      // If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.

      osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
      if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) 
         return FALSE;
   }

   switch (osvi.dwPlatformId)
   {
      case VER_PLATFORM_WIN32_NT:

      // Test for the product.

         if ( osvi.dwMajorVersion <= 4 )
            printf( "Microsoft Windows NT ");

         if ( osvi.dwMajorVersion == 5 )
            printf ("Microsoft Windows 2000 ");

      // Test for workstation versus server.

         if( bOsVersionInfoEx )
         {
            if ( osvi.wProductType == VER_NT_WORKSTATION )
               printf ( "Professional " );

            if ( osvi.wProductType == VER_NT_SERVER )
               printf ( "Server " );
         }
         else
         {
            HKEY hKey;
            char szProductType[80];
            DWORD dwBufLen;

            RegOpenKeyEx( HKEY_LOCAL_MACHINE,
               "SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
               0, KEY_QUERY_VALUE, &hKey );
            RegQueryValueEx( hKey, "ProductType", NULL, NULL,
               (LPBYTE) szProductType, &dwBufLen);
            RegCloseKey( hKey );
            if ( lstrcmpi( "WINNT", szProductType) == 0 )
               printf( "Workstation " );
            if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
               printf( "Server " );
         }

      // Display version, service pack (if any), and build number.

         printf ("version %d.%d %s (Build %d)\n",
            osvi.dwMajorVersion,
            osvi.dwMinorVersion,
            osvi.szCSDVersion,
            osvi.dwBuildNumber & 0xFFFF);

         break;

      case VER_PLATFORM_WIN32_WINDOWS:

         if ((osvi.dwMajorVersion > 4) || 
            ((osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion > 0)))
         {
             printf ("Microsoft Windows 98 ");
         } 
         else printf ("Microsoft Windows 95 ");

         break;

      case VER_PLATFORM_WIN32s:

         printf ("Microsoft Win32s ");
         break;
   }
   return TRUE; 
}

Schreibe einen Kommentar