Programming - cpueblo.com

GetVersionEx() - OS 버전을 가져와 문자열로 이쁘게 얻기


글쓴이 : 유광희 날짜 : 2004-08-20 (금) 14:06 조회 : 15478

#define BUFSIZE 80
String GetOsVer()
{
	String OS;
	String TempStr;

   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);


   bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi);
   if (!bOsVersionInfoEx)
   {
	  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 family.
		 if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
			OS += "Microsoft Windows Server 2003 family, ";

		 if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
			OS += "Microsoft Windows XP ";

		 if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
			OS += "Microsoft Windows 2000 ";

		 if ( osvi.dwMajorVersion <= <FONT COLOR=#FF00FF>4 )
			OS += "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 )
				  OS += "Workstation 4.0 " ;
			   else if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
				  OS += "Home Edition " ;
			   else
				  OS += "Professional " ;
			}

			// Test for the server type.
			else if ( osvi.wProductType == VER_NT_SERVER )
			{
			   if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
			   {
				  if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
					 OS += "Datacenter Edition " ;
				  else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
					 OS += "Enterprise Edition " ;
				  else if ( osvi.wSuiteMask == VER_SUITE_BLADE )
					 OS += "Web Edition ";
				  else
					 OS += "Standard Edition ";
			   }

			   else if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
			   {
				  if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
					 OS += "Datacenter Server ";
				  else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
					 OS += "Advanced Server ";
				  else
					 OS += "Server ";
			   }

			   else  // Windows NT 4.0
			   {
				  if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
					 OS += "Server 4.0, Enterprise Edition ";
				  else
					 OS += "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 )
			   OS += "Workstation ";
			if ( lstrcmpi( "LANMANNT", szProductType) == 0 )
			   OS += "Server ";
			if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
			   OS += "Advanced Server ";


			TempStr.printf("%d.%d", osvi.dwMajorVersion, osvi.dwMinorVersion );
			OS += TempStr;
		 }

	  // 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 )
			{
				TempStr.printf("Service Pack 6a (Build %d)\\n", osvi.dwBuildNumber & 0xFFFF );
				OS += TempStr;
			}
			else // Windows NT 4.0 prior to SP6a
			{
			   TempStr.printf("%s (Build %d)\\n",
				  osvi.szCSDVersion,
				  osvi.dwBuildNumber & 0xFFFF);
			   OS += TempStr;
			}

			RegCloseKey( hKey );
		 }
		 else // Windows NT 3.51 and earlier or Windows 2000 and later
		 {

			TempStr.sprintf("%s (Build %d)\\n",
			   osvi.szCSDVersion,
			   osvi.dwBuildNumber & 0xFFFF);

			OS += TempStr;
		 }
		 break;

	  // Test for the Windows 95 product family.
	  case VER_PLATFORM_WIN32_WINDOWS:

         if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
         {
			 OS += "Microsoft Windows 95 ";
			 if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' )
				OS += "OSR2 " ;
		 }

		 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
		 {
			 OS += "Microsoft Windows 98 ";
			 if ( osvi.szCSDVersion[1] == 'A' )
				OS += "SE ";
         }

         if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
         {
			 OS += "Microsoft Windows Millennium Edition\\n";
		 }
		 break;

	  case VER_PLATFORM_WIN32s:

		 OS += "Microsoft Win32s\\n";
         break;
   }

   return OS;
}