Get rid of deprecated warnings for ::GetVersionEx().

This does not solve the actual problem of this function not returning the
correct value for Windows 8+ any more, but at least allows to compile the
library without warnings with MSVC 12 and later.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76409 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2014-04-27 22:39:37 +00:00
parent 2964de72a5
commit acf6264309

View File

@ -1162,33 +1162,56 @@ wxLoadUserResource(const wxString& resourceName,
// OS version // OS version
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
namespace
{
// Helper function wrapping Windows GetVersionEx() which is deprecated since
// Windows 8. For now, all we do in this wrapper is to avoid the deprecation
// warnings but this is not enough as the function now actually doesn't return
// the correct value any more and we need to use VerifyVersionInfo() to perform
// binary search to find the real Windows version.
OSVERSIONINFOEX wxGetWindowsVersionInfo()
{
OSVERSIONINFOEX info;
wxZeroMemory(info);
#ifdef __VISUALC__
#pragma warning(push)
#pragma warning(disable:4996) // 'xxx': was declared deprecated
#endif
info.dwOSVersionInfoSize = sizeof(info);
if ( !::GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&info)) )
{
// This really shouldn't ever happen.
wxFAIL_MSG( "GetVersionEx() unexpectedly failed" );
}
#ifdef __VISUALC__
#pragma warning(pop)
#endif
return info;
}
// check if we're running under a server or workstation Windows system: it // check if we're running under a server or workstation Windows system: it
// returns true or false with obvious meaning as well as -1 if the system type // returns true or false with obvious meaning as well as -1 if the system type
// couldn't be determined // couldn't be determined
// //
// this function is currently private but we may want to expose it later if // this function is currently private but we may want to expose it later if
// it's really useful // it's really useful
namespace
{
int wxIsWindowsServer() int wxIsWindowsServer()
{ {
#ifdef VER_NT_WORKSTATION #ifdef VER_NT_WORKSTATION
OSVERSIONINFOEX info; switch ( wxGetWindowsVersionInfo().wProductType )
wxZeroMemory(info);
info.dwOSVersionInfoSize = sizeof(info);
if ( ::GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&info)) )
{ {
switch ( info.wProductType ) case VER_NT_WORKSTATION:
{ return false;
case VER_NT_WORKSTATION:
return false;
case VER_NT_SERVER: case VER_NT_SERVER:
case VER_NT_DOMAIN_CONTROLLER: case VER_NT_DOMAIN_CONTROLLER:
return true; return true;
}
} }
#endif // VER_NT_WORKSTATION #endif // VER_NT_WORKSTATION
@ -1201,145 +1224,135 @@ wxString wxGetOsDescription()
{ {
wxString str; wxString str;
OSVERSIONINFO info; const OSVERSIONINFOEX info = wxGetWindowsVersionInfo();
wxZeroMemory(info); switch ( info.dwPlatformId )
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if ( ::GetVersionEx(&info) )
{ {
switch ( info.dwPlatformId )
{
#ifdef VER_PLATFORM_WIN32_CE #ifdef VER_PLATFORM_WIN32_CE
case VER_PLATFORM_WIN32_CE: case VER_PLATFORM_WIN32_CE:
str.Printf(_("Windows CE (%d.%d)"), str.Printf(_("Windows CE (%d.%d)"),
info.dwMajorVersion, info.dwMajorVersion,
info.dwMinorVersion); info.dwMinorVersion);
break; break;
#endif #endif
case VER_PLATFORM_WIN32s: case VER_PLATFORM_WIN32s:
str = _("Win32s on Windows 3.1"); str = _("Win32s on Windows 3.1");
break; break;
case VER_PLATFORM_WIN32_WINDOWS: case VER_PLATFORM_WIN32_WINDOWS:
switch (info.dwMinorVersion) switch (info.dwMinorVersion)
{ {
case 0: case 0:
if ( info.szCSDVersion[1] == 'B' || if ( info.szCSDVersion[1] == 'B' ||
info.szCSDVersion[1] == 'C' ) info.szCSDVersion[1] == 'C' )
{ {
str = _("Windows 95 OSR2"); str = _("Windows 95 OSR2");
} }
else else
{ {
str = _("Windows 95"); str = _("Windows 95");
} }
break; break;
case 10: case 10:
if ( info.szCSDVersion[1] == 'B' || if ( info.szCSDVersion[1] == 'B' ||
info.szCSDVersion[1] == 'C' ) info.szCSDVersion[1] == 'C' )
{ {
str = _("Windows 98 SE"); str = _("Windows 98 SE");
} }
else else
{ {
str = _("Windows 98"); str = _("Windows 98");
} }
break; break;
case 90: case 90:
str = _("Windows ME"); str = _("Windows ME");
break; break;
default: default:
str.Printf(_("Windows 9x (%d.%d)"), str.Printf(_("Windows 9x (%d.%d)"),
info.dwMajorVersion,
info.dwMinorVersion);
break;
}
if ( !wxIsEmpty(info.szCSDVersion) )
{
str << wxT(" (") << info.szCSDVersion << wxT(')');
}
break;
case VER_PLATFORM_WIN32_NT:
switch ( info.dwMajorVersion )
{
case 5:
switch ( info.dwMinorVersion )
{
case 0:
str = _("Windows 2000");
break;
case 2:
// we can't distinguish between XP 64 and 2003
// as they both are 5.2, so examine the product
// type to resolve this ambiguity
if ( wxIsWindowsServer() == 1 )
{
str = _("Windows Server 2003");
break;
}
//else: must be XP, fall through
case 1:
str = _("Windows XP");
break;
}
break;
case 6:
switch ( info.dwMinorVersion )
{
case 0:
str = wxIsWindowsServer() == 1
? _("Windows Server 2008")
: _("Windows Vista");
break;
case 1:
str = wxIsWindowsServer() == 1
? _("Windows Server 2008 R2")
: _("Windows 7");
break;
case 2:
str = wxIsWindowsServer() == 1
? _("Windows Server 2012")
: _("Windows 8");
break;
case 3:
str = wxIsWindowsServer() == 1
? _("Windows Server 2012 R2")
: _("Windows 8.1");
break;
}
break;
}
if ( str.empty() )
{
str.Printf(_("Windows NT %lu.%lu"),
info.dwMajorVersion, info.dwMajorVersion,
info.dwMinorVersion); info.dwMinorVersion);
} break;
}
if ( !wxIsEmpty(info.szCSDVersion) )
{
str << wxT(" (") << info.szCSDVersion << wxT(')');
}
break;
str << wxT(" (") case VER_PLATFORM_WIN32_NT:
<< wxString::Format(_("build %lu"), info.dwBuildNumber); switch ( info.dwMajorVersion )
if ( !wxIsEmpty(info.szCSDVersion) ) {
{ case 5:
str << wxT(", ") << info.szCSDVersion; switch ( info.dwMinorVersion )
} {
str << wxT(')'); case 0:
str = _("Windows 2000");
break;
if ( wxIsPlatform64Bit() ) case 2:
str << _(", 64-bit edition"); // we can't distinguish between XP 64 and 2003
break; // as they both are 5.2, so examine the product
} // type to resolve this ambiguity
} if ( wxIsWindowsServer() == 1 )
else {
{ str = _("Windows Server 2003");
wxFAIL_MSG( wxT("GetVersionEx() failed") ); // should never happen break;
}
//else: must be XP, fall through
case 1:
str = _("Windows XP");
break;
}
break;
case 6:
switch ( info.dwMinorVersion )
{
case 0:
str = wxIsWindowsServer() == 1
? _("Windows Server 2008")
: _("Windows Vista");
break;
case 1:
str = wxIsWindowsServer() == 1
? _("Windows Server 2008 R2")
: _("Windows 7");
break;
case 2:
str = wxIsWindowsServer() == 1
? _("Windows Server 2012")
: _("Windows 8");
break;
case 3:
str = wxIsWindowsServer() == 1
? _("Windows Server 2012 R2")
: _("Windows 8.1");
break;
}
break;
}
if ( str.empty() )
{
str.Printf(_("Windows NT %lu.%lu"),
info.dwMajorVersion,
info.dwMinorVersion);
}
str << wxT(" (")
<< wxString::Format(_("build %lu"), info.dwBuildNumber);
if ( !wxIsEmpty(info.szCSDVersion) )
{
str << wxT(", ") << info.szCSDVersion;
}
str << wxT(')');
if ( wxIsPlatform64Bit() )
str << _(", 64-bit edition");
break;
} }
return str; return str;
@ -1374,8 +1387,7 @@ wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
{ {
static struct static struct
{ {
// this may be false, true or -1 if we tried to initialize but failed bool initialized;
int initialized;
wxOperatingSystemId os; wxOperatingSystemId os;
@ -1386,49 +1398,36 @@ wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
// query the OS info only once as it's not supposed to change // query the OS info only once as it's not supposed to change
if ( !s_version.initialized ) if ( !s_version.initialized )
{ {
OSVERSIONINFO info; const OSVERSIONINFOEX info = wxGetWindowsVersionInfo();
wxZeroMemory(info);
info.dwOSVersionInfoSize = sizeof(info); s_version.initialized = true;
if ( ::GetVersionEx(&info) )
{
s_version.initialized = true;
#if defined(__WXWINCE__) #if defined(__WXWINCE__)
s_version.os = wxOS_WINDOWS_CE; s_version.os = wxOS_WINDOWS_CE;
#elif defined(__WXMICROWIN__) #elif defined(__WXMICROWIN__)
s_version.os = wxOS_WINDOWS_MICRO; s_version.os = wxOS_WINDOWS_MICRO;
#else // "normal" desktop Windows system, use run-time detection #else // "normal" desktop Windows system, use run-time detection
switch ( info.dwPlatformId ) switch ( info.dwPlatformId )
{ {
case VER_PLATFORM_WIN32_NT: case VER_PLATFORM_WIN32_NT:
s_version.os = wxOS_WINDOWS_NT; s_version.os = wxOS_WINDOWS_NT;
break; break;
case VER_PLATFORM_WIN32_WINDOWS: case VER_PLATFORM_WIN32_WINDOWS:
s_version.os = wxOS_WINDOWS_9X; s_version.os = wxOS_WINDOWS_9X;
break; break;
} }
#endif // Windows versions #endif // Windows versions
s_version.verMaj = info.dwMajorVersion; s_version.verMaj = info.dwMajorVersion;
s_version.verMin = info.dwMinorVersion; s_version.verMin = info.dwMinorVersion;
}
else // GetVersionEx() failed
{
s_version.initialized = -1;
}
} }
if ( s_version.initialized == 1 ) if ( verMaj )
{ *verMaj = s_version.verMaj;
if ( verMaj ) if ( verMin )
*verMaj = s_version.verMaj; *verMin = s_version.verMin;
if ( verMin )
*verMin = s_version.verMin;
}
// this works even if we were not initialized successfully as the initial
// values of this field is 0 which is wxOS_UNKNOWN and exactly what we need
return s_version.os; return s_version.os;
} }