OS 타입 및 버젼 알아 오기
C++ 2008. 1. 17. 09:33 |어플리케이션이 실행될 OS의 타입과 빌드 번호 등을 알아 오는 데모
OSVersionChk 소스 코드 :
// OSVersionChk.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#pragma comment (lib, "wininet")
#pragma comment (lib, "comctl32")
#pragma comment (lib, "FDI")
#pragma comment (lib, "Version")
#pragma comment (lib, "dxguid")
#pragma warning (disable:4244)
#pragma warning (disable:4311)
#pragma warning (disable:4312)
#include <windows.h>
#include <windowsx.h>
#include <wininet.h>
#include <commctrl.h>
#include <process.h>
#include <shlobj.h>
#include <tlhelp32.h>
void DebugShowConsoleAndOutputDebugString(const char* buf);
int _tmain(int argc, _TCHAR* argv[])
{
bool g_bIsNT = false;
bool g_bIsVista = false;
#define BUFSIZE 256
char buf[256];
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)) )
{
// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
return FALSE;
}
switch (osvi.dwPlatformId)
{
// Tests for Windows NT product family.
case VER_PLATFORM_WIN32_NT:
g_bIsNT = true;
// Test for the product.
if ( osvi.dwMajorVersion <= 4 )
DebugShowConsoleAndOutputDebugString ("Microsoft Windows NT ");
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
DebugShowConsoleAndOutputDebugString ("Microsoft Windows 2000 ");
if (osvi.dwMajorVersion == 6) // windows vista
{
DebugShowConsoleAndOutputDebugString ("Microsoft Windows Vista ");
g_bIsVista = true;
}
if( bOsVersionInfoEx ) // Use information from GetVersionEx.
{
// Test for the workstation type.
if ( osvi.wProductType == VER_NT_WORKSTATION )
{
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
DebugShowConsoleAndOutputDebugString ("Microsoft Windows XP ");
if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
DebugShowConsoleAndOutputDebugString ( "Home Edition " );
else
DebugShowConsoleAndOutputDebugString ( "Professional " );
}
// Test for the server type.
else if ( osvi.wProductType == VER_NT_SERVER )
{
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
DebugShowConsoleAndOutputDebugString ("Microsoft Windows .NET ");
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
DebugShowConsoleAndOutputDebugString ( "DataCenter Server " );
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
if( osvi.dwMajorVersion == 4 )
DebugShowConsoleAndOutputDebugString ("Advanced Server " );
else
DebugShowConsoleAndOutputDebugString ( "Enterprise Server " );
else if ( osvi.wSuiteMask == VER_SUITE_BLADE )
DebugShowConsoleAndOutputDebugString ( "Web Server " );
else
DebugShowConsoleAndOutputDebugString ( "Server " );
}
}
else // Use the registry on early versions of Windows NT.
{
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 )
DebugShowConsoleAndOutputDebugString ( "Professional " );
if ( lstrcmpi( "LANMANNT", szProductType) == 0 )
DebugShowConsoleAndOutputDebugString ( "Server " );
if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
DebugShowConsoleAndOutputDebugString ( "Advanced Server " );
}
// Display version, service pack (if any), and build number.
if ( osvi.dwMajorVersion <= 4 )
{
wsprintf (buf, "version %d.%d %s (Build %d)\n",
osvi.dwMajorVersion,
osvi.dwMinorVersion,
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
DebugShowConsoleAndOutputDebugString (buf);
}
else
{
wsprintf (buf, "%s (Build %d)\n",
osvi.szCSDVersion,
osvi.dwBuildNumber & 0xFFFF);
DebugShowConsoleAndOutputDebugString (buf);
}
DebugShowConsoleAndOutputDebugString ("\n");
break;
// Test for the Windows 95 product family.
case VER_PLATFORM_WIN32_WINDOWS:
g_bIsNT = false;
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
{
DebugShowConsoleAndOutputDebugString ("Microsoft Windows 95 ");
if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' )
DebugShowConsoleAndOutputDebugString("OSR2 " );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
{
DebugShowConsoleAndOutputDebugString ("Microsoft Windows 98 ");
if ( osvi.szCSDVersion[1] == 'A' )
DebugShowConsoleAndOutputDebugString ("SE " );
}
if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
{
DebugShowConsoleAndOutputDebugString ("Microsoft Windows Millennium Edition ");
}
DebugShowConsoleAndOutputDebugString ("\n");
break;
}
getchar();
return 0;
}
// 각종 정보를 OutputDebugString()만 호출하는게 아니라 화면에도 출력 하기 위해서...
void DebugShowConsoleAndOutputDebugString(const char* buf)
{
//printf(buf);
cout << buf << endl;
OutputDebugString(buf);
}