1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#include <windows.h>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
#define PRODUCT_PROFESSIONAL 0x00000030
#define VER_SUITE_WH_SERVER 0x00008000
bool windowsVersionName(wchar_t* str, int bufferSize)
{
OSVERSIONINFOEX osvi;
SYSTEM_INFO si;
BOOL bOsVersionInfoEx;
DWORD dwType; ZeroMemory(&si, sizeof(SYSTEM_INFO));
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*) &osvi);
if(bOsVersionInfoEx == 0)
return false; // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
PGNSI pGNSI = (PGNSI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
if(NULL != pGNSI)
pGNSI(&si);
else GetSystemInfo(&si); // Check for unsupported OS
if (VER_PLATFORM_WIN32_NT != osvi.dwPlatformId || osvi.dwMajorVersion <= 4 )
return false;
//std::wstringstream os;
std::stringstream os;
os << L"Microsoft "; // Test for the specific product. if ( osvi.dwMajorVersion == 6 )
{
if( osvi.dwMinorVersion == 0 )
{
if( osvi.wProductType == VER_NT_WORKSTATION )
os << "Windows Vista ";
else os << "Windows Server 2008 ";
}
if ( osvi.dwMinorVersion == 1 )
{
if( osvi.wProductType == VER_NT_WORKSTATION )
os << "Windows 7 ";
else os << "Windows Server 2008 R2 ";
}
PGPI pGPI = (PGPI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
cout << "dwType = " << dwType << endl;
/*switch( dwType )
{
case PRODUCT_ULTIMATE:
//the rest of the switch statement would have went here.
*/
}
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
{
if( GetSystemMetrics(SM_SERVERR2) )
os << "Windows Server 2003 R2, ";
else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER )
os << "Windows Storage Server 2003";
else if ( osvi.wSuiteMask & VER_SUITE_WH_SERVER )
os << "Windows Home Server";
else if( osvi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
{
os << "Windows XP Professional x64 Edition";
}
else os << "Windows Server 2003, "; // Test for the server type.
if ( osvi.wProductType != VER_NT_WORKSTATION )
{
if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
os << "Datacenter Edition for Itanium-based Systems";
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
os << "Enterprise Edition for Itanium-based Systems";
}
else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
os << "Datacenter x64 Edition";
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
os << "Enterprise x64 Edition";
else os << "Standard x64 Edition";
}
else
{
if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
os << "Compute Cluster Edition";
else 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";
}
}
}
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
{
os << "Windows XP ";
if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
os << "Home Edition";
else os << "Professional";
}
if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
{
os << "Windows 2000 ";
if ( osvi.wProductType == VER_NT_WORKSTATION )
{
os << "Professional";
}
else
{
if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
os << "Datacenter Server";
else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
os << "Advanced Server";
else os << "Server";
}
} // Include service pack (if any) and build number.
// if(wcslen(osvi.szCSDVersion) > 0) debugging important
{
os << " " << osvi.szCSDVersion;
}
os << L" (build " << osvi.dwBuildNumber << L")";
if ( osvi.dwMajorVersion >= 6 )
{
if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
os << ", 64-bit";
else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL )
os << ", 32-bit";
}
// wcscpy_s(str, bufferSize, os.str().c_str());
cout << os.str().c_str() << endl;
return true;
}
int main()
{
wchar_t* winstr; int buffer = 0;
//cout << windowsVersionName(wchar_t* str, int bufferSize)
cout << windowsVersionName(winstr, buffer);
return 0;
}
|