출처 : MSDN
http://j2doll.tistory.com/113
현재 윈도우즈 버전 얻는 코드입니다.
주 용도는 프로그램이 지원 가능한 환경에서만 실행하게 한다거나,
커스텀한 프로그램 설치 시에 플랫폼 버전별로 다른 설치를 하게 한다거나
하는 용도로 활용될수 있습니다.
그리고, 아래 코드는 기본 예제에서 약간만 수정 하였습니다.
위 프로그램은 실행 파일입니다.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#define BUFSIZE 80
#define SM_SERVERR2 89
typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
int _tmain(int argc, _TCHAR* argv[])
{
OSVERSIONINFOEX osvi;
SYSTEM_INFO si;
PGNSI pGNSI;
BOOL bOsVersionInfoEx;
ZeroMemory(&si, sizeof(SYSTEM_INFO));
// 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 == 6 && osvi.dwMinorVersion == 0 )
{
if( osvi.wProductType == VER_NT_WORKSTATION )
_tprintf (_T("Microsoft Windows Vista "));
else
_tprintf (_T("Windows Server \"Longhorn\" "));
}
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
{
// Use GetProcAddress to avoid load issues on Windows 2000
pGNSI = (PGNSI) GetProcAddress(
GetModuleHandle(_T("kernel32.dll")),
"GetNativeSystemInfo");
if(NULL != pGNSI)
pGNSI(&si);
if( GetSystemMetrics(SM_SERVERR2) )
_tprintf( _T("Microsoft Windows Server 2003 \"R2\" "));
else if( osvi.wProductType == VER_NT_WORKSTATION &&
si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
{
_tprintf( _T("Microsoft Windows XP Professional x64 Edition "));
}
else
_tprintf (_T("Microsoft Windows Server 2003, "));
}
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
{
_tprintf (_T("Microsoft Windows XP "));
}
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
{
_tprintf (_T("Microsoft Windows 2000 "));
}
if ( osvi.dwMajorVersion <= 4 )
{
_tprintf (_T("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 &&
si.wProcessorArchitecture!=PROCESSOR_ARCHITECTURE_AMD64)
{
if( osvi.dwMajorVersion == 4 )
_tprintf ( _T("Workstation 4.0 ") );
else if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
_tprintf ( _T("Home Edition ") );
else _tprintf ( _T("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 ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
_tprintf ( _T("Datacenter Edition for Itanium-based Systems") );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
_tprintf ( _T("Enterprise Edition for Itanium-based Systems") );
}
else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
_tprintf ( _T("Datacenter x64 Edition ") );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
_tprintf ( _T("Enterprise x64 Edition ") );
else _tprintf( _T("Standard x64 Edition ") );
}
else
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
_tprintf ( _T("Datacenter Edition ") );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
_tprintf ( _T("Enterprise Edition ") );
else if ( osvi.wSuiteMask == VER_SUITE_BLADE )
_tprintf ( _T("Web Edition ") );
else _tprintf ( _T("Standard Edition ") );
}
}
else if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==0)
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
_tprintf ( _T("Datacenter Server ") );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
_tprintf ( _T("Advanced Server ") );
else _tprintf ( _T("Server ") );
}
else // Windows NT 4.0
{
if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
_tprintf (_T("Server 4.0, Enterprise Edition ") );
else _tprintf ( _T("Server 4.0 ") );
}
}
}
// Test for specific product on Windows NT 4.0 SP5 and earlier
else
{
HKEY hKey;
TCHAR szProductType[BUFSIZE];
DWORD dwBufLen = BUFSIZE * sizeof(_TCHAR);
LONG lRet;
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
_T("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"),
0, KEY_QUERY_VALUE, &hKey );
if( lRet != ERROR_SUCCESS )
return FALSE;
lRet = RegQueryValueEx( hKey, _T("ProductType"), NULL, NULL,
(LPBYTE) szProductType, &dwBufLen);
RegCloseKey( hKey );
if( (lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE) )
return FALSE;
if ( lstrcmpi( _T("WINNT"), szProductType) == 0 )
_tprintf( _T("Workstation ") );
if ( lstrcmpi( _T("LANMANNT"), szProductType) == 0 )
_tprintf( _T("Server ") );
if ( lstrcmpi( _T("SERVERNT"), szProductType) == 0 )
_tprintf( _T("Advanced Server ") );
_tprintf( _T("%d.%d "), osvi.dwMajorVersion, osvi.dwMinorVersion );
}
// Display service pack (if any) and build number.
if( osvi.dwMajorVersion == 4 &&
lstrcmpi( osvi.szCSDVersion, _T("Service Pack 6") ) == 0 )
{
HKEY hKey;
LONG lRet;
// Test for SP6 versus SP6a.
lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
_T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009"),
0, KEY_QUERY_VALUE, &hKey );
if( lRet == ERROR_SUCCESS )
_tprintf( _T("Service Pack 6a (Build %d)\n"),
osvi.dwBuildNumber & 0xFFFF );
else // Windows NT 4.0 prior to SP6a
{
_tprintf( _T("%s (Build %d)\n"),
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
}
RegCloseKey( hKey );
}
else // not Windows NT 4.0
{
_tprintf( _T("%s (Build %d)\n"),
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
}
if ( osvi.wServicePackMajor != 0 )
{
// If no Service Pack has been installed, wServicePackMajor is zero.
_tprintf( _T(" Service Pack Version %d.%d"),
osvi.wServicePackMajor,
osvi.wServicePackMinor );
}
break;
// Test for the Windows Me/98/95.
case VER_PLATFORM_WIN32_WINDOWS:
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
{
_tprintf (_T("Microsoft Windows 95 "));
if (osvi.szCSDVersion[1]=='C' || osvi.szCSDVersion[1]=='B')
_tprintf(_T("OSR2 ") );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
{
_tprintf (_T("Microsoft Windows 98 "));
if ( osvi.szCSDVersion[1]=='A' || osvi.szCSDVersion[1]=='B')
_tprintf(_T("SE ") );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
{
_tprintf (_T("Microsoft Windows Millennium Edition\n"));
}
break;
case VER_PLATFORM_WIN32s:
_tprintf (_T("Microsoft Win32s\n"));
break;
}
system( "pause" );
return 0;
}
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.