From 2b3633b3c040b277740cd1c8d4fdb998ebf900a0 Mon Sep 17 00:00:00 2001 From: Tobias Taschner Date: Thu, 6 Aug 2015 11:32:38 +0200 Subject: [PATCH] Add wxCheckOsVersion() to implement platform based OS version checks. Windows implements VerifyVersionInfo api since Win2k. Starting with Windows 8.1 GetVersionEx is deprecated and may not return the expected version number if the application does not contain the correct compatibility information in its manifest. VerifyVersionInfo works independent of manifest in the executable (and is the recommend way to check). Existing code may already use wxPlatformInfo::CheckOSVersion() so the method forwards the call to wxCheckOsVersion if initialized for the current system. --- include/wx/platinfo.h | 10 +++------- include/wx/utils.h | 3 +++ interface/wx/utils.h | 12 ++++++++++++ src/common/platinfo.cpp | 17 +++++++++++++++++ src/msw/utils.cpp | 15 +++++++++++++++ src/unix/utilsunx.cpp | 8 ++++++++ 6 files changed, 58 insertions(+), 7 deletions(-) diff --git a/include/wx/platinfo.h b/include/wx/platinfo.h index 33cd2c58ca..3166f82804 100644 --- a/include/wx/platinfo.h +++ b/include/wx/platinfo.h @@ -190,13 +190,7 @@ public: { return m_osVersionMinor; } // return true if the OS version >= major.minor - bool CheckOSVersion(int major, int minor) const - { - return DoCheckVersion(GetOSMajorVersion(), - GetOSMinorVersion(), - major, - minor); - } + bool CheckOSVersion(int major, int minor) const; int GetToolkitMajorVersion() const { return m_tkVersionMajor; } @@ -300,6 +294,8 @@ protected: return majorCur > major || (majorCur == major && minorCur >= minor); } + bool m_initializedForCurrentPlatform; + void InitForCurrentPlatform(); diff --git a/include/wx/utils.h b/include/wx/utils.h index 3461821b46..e29c131697 100644 --- a/include/wx/utils.h +++ b/include/wx/utils.h @@ -143,6 +143,9 @@ WXDLLIMPEXP_BASE wxString wxGetOsDescription(); WXDLLIMPEXP_BASE wxOperatingSystemId wxGetOsVersion(int *majorVsn = NULL, int *minorVsn = NULL); +// Check is OS version is at least the specified major and minor version +WXDLLIMPEXP_BASE bool wxCheckOsVersion(int majorVsn, int minorVsn = 0); + // Get platform endianness WXDLLIMPEXP_BASE bool wxIsPlatformLittleEndian(); diff --git a/interface/wx/utils.h b/interface/wx/utils.h index 46418d2b77..0dd2a30d73 100644 --- a/interface/wx/utils.h +++ b/interface/wx/utils.h @@ -879,6 +879,18 @@ wxString wxGetOsDescription(); */ wxOperatingSystemId wxGetOsVersion(int* major = NULL, int* minor = NULL); +/** + Returns @true the version of the operating system on which the program + is running under is the same or later than the given version. + + @since 3.1.0 + + @see wxGetOsVersion(), wxPlatformInfo + + @header{wx/utils.h} +*/ +bool wxCheckOsVersion(int majorVsn, int minorVsn = 0); + /** Returns @true if the operating system the program is running under is 64 bit. The check is performed at run-time and may differ from the value diff --git a/src/common/platinfo.cpp b/src/common/platinfo.cpp index 268d51273e..41fd0c23db 100644 --- a/src/common/platinfo.cpp +++ b/src/common/platinfo.cpp @@ -135,6 +135,8 @@ wxPlatformInfo::wxPlatformInfo(wxPortId pid, int tkMajor, int tkMinor, wxEndianness endian, bool usingUniversal) { + m_initializedForCurrentPlatform = false; + m_tkVersionMajor = tkMajor; m_tkVersionMinor = tkMinor; m_port = pid; @@ -166,6 +168,8 @@ bool wxPlatformInfo::operator==(const wxPlatformInfo &t) const void wxPlatformInfo::InitForCurrentPlatform() { + m_initializedForCurrentPlatform = true; + // autodetect all informations const wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL; if ( !traits ) @@ -294,6 +298,19 @@ wxString wxPlatformInfo::GetEndiannessName(wxEndianness end) return wxEndiannessNames[end]; } +bool wxPlatformInfo::CheckOSVersion(int major, int minor) const +{ + // If this instance of wxPlatformInfo has been initialized by InitForCurrentPlatform() + // this check gets forwarded to the wxCheckOsVersion which might do more than a simple + // number check if supported by the platform + if (m_initializedForCurrentPlatform) + return wxCheckOsVersion(major, minor); + else + return DoCheckVersion(GetOSMajorVersion(), + GetOSMinorVersion(), + major, + minor); +} // ---------------------------------------------------------------------------- // wxPlatformInfo - string -> enum conversions diff --git a/src/msw/utils.cpp b/src/msw/utils.cpp index 93bc29b864..aedbb78841 100644 --- a/src/msw/utils.cpp +++ b/src/msw/utils.cpp @@ -1327,6 +1327,21 @@ wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin) return s_version.os; } +bool wxCheckOsVersion(int majorVsn, int minorVsn) +{ + OSVERSIONINFOEX osvi = { sizeof(osvi), 0, 0, 0, 0, { 0 }, 0, 0 }; + DWORDLONG const dwlConditionMask = + ::VerSetConditionMask( + ::VerSetConditionMask( + 0, VER_MAJORVERSION, VER_GREATER_EQUAL), + VER_MINORVERSION, VER_GREATER_EQUAL); + + osvi.dwMajorVersion = majorVsn; + osvi.dwMinorVersion = minorVsn; + + return ::VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask) != FALSE; +} + wxWinVersion wxGetWinVersion() { int verMaj, diff --git a/src/unix/utilsunx.cpp b/src/unix/utilsunx.cpp index f7e63d2856..cc53be2b0f 100644 --- a/src/unix/utilsunx.cpp +++ b/src/unix/utilsunx.cpp @@ -1150,6 +1150,14 @@ wxString wxGetOsDescription() #endif // !__DARWIN__ +bool wxCheckOsVersion(int majorVsn, int minorVsn) +{ + int majorCur, minorCur; + wxGetOsVersion(&majorCur, &minorCur); + + return majorCur > majorVsn || (majorCur == majorVsn && minorCur >= minorVsn); +} + unsigned long wxGetProcessId() { return (unsigned long)getpid();