Re: IsOS(...) function for Vista?
"Giovanni Dicanio" <giovanniDOTdicanio@REMOVEMEgmail.com> ha scritto nel
messaggio news:u23mGwsJJHA.1160@TK2MSFTNGP04.phx.gbl...
Another option could be to use C++/CLI extensions to build a simple
managed class, that has a static method (or a read-only property) that
returns the OS version.
This C++/CLI managed class simply uses the native C++ GetOSDisplayString
code inside, but exposes the functionality of that code to the .NET
managed platform.
I implemented that.
You can get full VS2008 solution (containing both C++/CLI class and C# test
WinForm app) here:
http://www.geocities.com/giovanni.dicanio/vc/os_version/ShowOSVersionInfo.zipA screenshot is available here: http://www.geocities.com/giovanni.dicanio/vc/os_version/screenshot.jpgI adjusted the original MSDN sample code, using CString instead of LPTSTR(which was also unsafe, because the output string buffer size was not passedas function parameter), and removing printf() (which was unsuited to GUIapplications like MFC apps).The new GetOSDisplayString function, with the "wrapping" C++/CLI class, arelisted here, too:(Note that the GetOSDisplayString function can be used also in nativeMFC/ATL projects, too.)<code filename="GetOSDisplayString.h">//////////////////////////////////////////////////////////////////////////// FILE: GetOSDisplayString.h// DESC: Prototype of GetOSDisplayString function.//////////////////////////////////////////////////////////////////////////#pragma once//------------------------------------------------------------------------// FUNCTION: GetOSDisplayString//// DESCRIPTION: Returns the version information of currently running// operating system.//// RETURN VALUE:// On success, returns the OS string.// On error, returns an empty string.//------------------------------------------------------------------------CString GetOSDisplayString();</code><code filename="GetOSDisplayString.cpp">//////////////////////////////////////////////////////////////////////////// FILE: GetOSDisplayString.cpp// DESC: Implementation of GetOSDisplayString function.//////////////////////////////////////////////////////////////////////////#include "Stdafx.h" // Pre-compiled header#include "GetOSDisplayString.h" // Module header//========================================================================//// Original code://// Getting the System Version// <http://msdn.microsoft.com/en-us/library/ms724429.aspx>//// The modifications I made to that code are://// - Returning CString instead of using raw TCHAR buffers.//// - Using CString instead of TCHAR raw buffers and functions from// StrSafe library.//// - Removing printf() calls//// Giovanni Dicanio <giovanniDOTdicanioATgmail.c0m>// 2008, October 5th////========================================================================typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);CString GetOSDisplayString(){ CString strOSVersion; // This will store the entire descriptive string OSVERSIONINFOEX osvi; SYSTEM_INFO si; PGNSI pGNSI; PGPI pGPI; DWORD dwType; ZeroMemory(&si, sizeof(SYSTEM_INFO)); ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if( ! GetVersionEx ((OSVERSIONINFO *) &osvi) ) return TEXT(""); // failed // // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise. // pGNSI = (PGNSI) GetProcAddress( GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo"); if(NULL != pGNSI) pGNSI(&si); else GetSystemInfo(&si); if ( VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4 ) { strOSVersion = TEXT("Microsoft "); // // Test for the specific product. // if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0 ) { if( osvi.wProductType == VER_NT_WORKSTATION ) strOSVersion += TEXT("Windows Vista "); else strOSVersion += TEXT("Windows Server 2008 " ); pGPI = (PGPI) GetProcAddress( GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo"); pGPI( 6, 0, 0, 0, &dwType); switch( dwType ) { case PRODUCT_ULTIMATE: strOSVersion += TEXT("Ultimate Edition" ); break; case PRODUCT_HOME_PREMIUM: strOSVersion += TEXT("Home Premium Edition" ); break; case PRODUCT_HOME_BASIC: strOSVersion += TEXT("Home Basic Edition" ); break; case PRODUCT_ENTERPRISE: strOSVersion += TEXT("Enterprise Edition" ); break; case PRODUCT_BUSINESS: strOSVersion += TEXT("Business Edition" ); break; case PRODUCT_STARTER: strOSVersion += TEXT("Starter Edition" ); break; case PRODUCT_CLUSTER_SERVER: strOSVersion += TEXT("Cluster Server Edition" ); break; case PRODUCT_DATACENTER_SERVER: strOSVersion += TEXT("Datacenter Edition" ); break; case PRODUCT_DATACENTER_SERVER_CORE: strOSVersion += TEXT("Datacenter Edition (coreinstallation)" ); break; case PRODUCT_ENTERPRISE_SERVER: strOSVersion += TEXT("Enterprise Edition" ); break; case PRODUCT_ENTERPRISE_SERVER_CORE: strOSVersion += TEXT("Enterprise Edition (coreinstallation)" ); break; case PRODUCT_ENTERPRISE_SERVER_IA64: strOSVersion += TEXT("Enterprise Edition for Itanium-basedSystems" ); break; case PRODUCT_SMALLBUSINESS_SERVER: strOSVersion += TEXT("Small Business Server" ); break; case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM: strOSVersion += TEXT("Small Business Server PremiumEdition" ); break; case PRODUCT_STANDARD_SERVER: strOSVersion += TEXT("Standard Edition" ); break; case PRODUCT_STANDARD_SERVER_CORE: strOSVersion += TEXT("Standard Edition (coreinstallation)" ); break; case PRODUCT_WEB_SERVER: strOSVersion += TEXT("Web Server Edition" ); break; } if ( si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ) strOSVersion += TEXT( ", 64-bit" ); else if (si.wProcessorArchitecture ==PROCESSOR_ARCHITECTURE_INTEL ) strOSVersion += TEXT(", 32-bit"); } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) { if( GetSystemMetrics(SM_SERVERR2) ) strOSVersion += TEXT( "Windows Server 2003 R2, "); else if ( osvi.wSuiteMask==VER_SUITE_STORAGE_SERVER ) strOSVersion += TEXT( "Windows Storage Server 2003"); else if( osvi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) { strOSVersion += TEXT( "Windows XP Professional x64Edition"); } else strOSVersion += TEXT("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 ) strOSVersion += TEXT( "Datacenter Edition forItanium-based Systems" ); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) strOSVersion += TEXT( "Enterprise Edition forItanium-based Systems" ); } else if ( si.wProcessorArchitecture ==PROCESSOR_ARCHITECTURE_AMD64 ) { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) strOSVersion += TEXT( "Datacenter x64 Edition" ); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) strOSVersion += TEXT( "Enterprise x64 Edition" ); else strOSVersion += TEXT( "Standard x64 Edition" ); } else { if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER ) strOSVersion += TEXT( "Compute Cluster Edition" ); else if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) strOSVersion += TEXT( "Datacenter Edition" ); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) strOSVersion += TEXT( "Enterprise Edition" ); else if ( osvi.wSuiteMask & VER_SUITE_BLADE ) strOSVersion += TEXT( "Web Edition" ); else strOSVersion += TEXT( "Standard Edition" ); } } } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) { strOSVersion += TEXT("Windows XP "); if( osvi.wSuiteMask & VER_SUITE_PERSONAL ) strOSVersion += TEXT( "Home Edition" ); else strOSVersion += TEXT( "Professional" ); } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) { strOSVersion += TEXT("Windows 2000 "); if ( osvi.wProductType == VER_NT_WORKSTATION ) { strOSVersion += TEXT( "Professional" ); } else { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) strOSVersion += TEXT( "Datacenter Server" ); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) strOSVersion += TEXT( "Advanced Server" ); else strOSVersion += TEXT( "Server" ); } } // // Include service pack (if any) and build number. // if( _tcslen(osvi.szCSDVersion) > 0 ) { strOSVersion += TEXT(" "); strOSVersion += osvi.szCSDVersion; } CString strBuild; strBuild.Format(TEXT(" (build %d)"), osvi.dwBuildNumber ); strOSVersion += strBuild; // All right return strOSVersion; } else { // printf( "This sample does not support this version ofWindows.\n"); return TEXT(""); // Error code (empty string) }}</code><code filename="OSVersion.h">//////////////////////////////////////////////////////////////////////////// FILE: OSVersion.h// DESC: Define OSVersionInfo class to get information of currently// running OS.//////////////////////////////////////////////////////////////////////////#pragma onceusing namespace System;#include "GetOSDisplayString.h" // Function to get OS versionnamespace OSVersion { //======================================================================== // This class exposes a public read-only property, containing the // description of currently running OS. // // To use this class, create an instance of it, and retrieve the // OperatingSystemVersion property value. //======================================================================== public ref class OSVersionInfo { public: // Constructor - initializes the class. OSVersionInfo() { // Clear string _versionInfo = L""; // Query OS version info. CString osVersionInfo = ::GetOSDisplayString(); // Check for errors. // On error, throw an exception in release builds. ATLASSERT( ! osVersionInfo.IsEmpty() ); if ( osVersionInfo.IsEmpty() ) { throw gcnew InvalidOperationException(L"Can't get operatingsystem information."); } // Store OS info in local data member _versionInfo = gcnew String( CT2W(osVersionInfo.GetString() ) ); } // Returns a string storing the OS version. property String^ OperatingSystemVersion { String^ get() { return _versionInfo; } } private: // Stores OS version info String ^ _versionInfo; };} // namespace OSVersion</code>HTH,Giovanni