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.
This commit is contained in:
Tobias Taschner 2015-08-06 11:32:38 +02:00
parent d8e291c27a
commit 2b3633b3c0
6 changed files with 58 additions and 7 deletions

View File

@ -190,13 +190,7 @@ public:
{ return m_osVersionMinor; } { return m_osVersionMinor; }
// return true if the OS version >= major.minor // return true if the OS version >= major.minor
bool CheckOSVersion(int major, int minor) const bool CheckOSVersion(int major, int minor) const;
{
return DoCheckVersion(GetOSMajorVersion(),
GetOSMinorVersion(),
major,
minor);
}
int GetToolkitMajorVersion() const int GetToolkitMajorVersion() const
{ return m_tkVersionMajor; } { return m_tkVersionMajor; }
@ -300,6 +294,8 @@ protected:
return majorCur > major || (majorCur == major && minorCur >= minor); return majorCur > major || (majorCur == major && minorCur >= minor);
} }
bool m_initializedForCurrentPlatform;
void InitForCurrentPlatform(); void InitForCurrentPlatform();

View File

@ -143,6 +143,9 @@ WXDLLIMPEXP_BASE wxString wxGetOsDescription();
WXDLLIMPEXP_BASE wxOperatingSystemId wxGetOsVersion(int *majorVsn = NULL, WXDLLIMPEXP_BASE wxOperatingSystemId wxGetOsVersion(int *majorVsn = NULL,
int *minorVsn = 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 // Get platform endianness
WXDLLIMPEXP_BASE bool wxIsPlatformLittleEndian(); WXDLLIMPEXP_BASE bool wxIsPlatformLittleEndian();

View File

@ -879,6 +879,18 @@ wxString wxGetOsDescription();
*/ */
wxOperatingSystemId wxGetOsVersion(int* major = NULL, int* minor = NULL); 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 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 bit. The check is performed at run-time and may differ from the value

View File

@ -135,6 +135,8 @@ wxPlatformInfo::wxPlatformInfo(wxPortId pid, int tkMajor, int tkMinor,
wxEndianness endian, wxEndianness endian,
bool usingUniversal) bool usingUniversal)
{ {
m_initializedForCurrentPlatform = false;
m_tkVersionMajor = tkMajor; m_tkVersionMajor = tkMajor;
m_tkVersionMinor = tkMinor; m_tkVersionMinor = tkMinor;
m_port = pid; m_port = pid;
@ -166,6 +168,8 @@ bool wxPlatformInfo::operator==(const wxPlatformInfo &t) const
void wxPlatformInfo::InitForCurrentPlatform() void wxPlatformInfo::InitForCurrentPlatform()
{ {
m_initializedForCurrentPlatform = true;
// autodetect all informations // autodetect all informations
const wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL; const wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
if ( !traits ) if ( !traits )
@ -294,6 +298,19 @@ wxString wxPlatformInfo::GetEndiannessName(wxEndianness end)
return wxEndiannessNames[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 // wxPlatformInfo - string -> enum conversions

View File

@ -1327,6 +1327,21 @@ wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
return s_version.os; 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() wxWinVersion wxGetWinVersion()
{ {
int verMaj, int verMaj,

View File

@ -1150,6 +1150,14 @@ wxString wxGetOsDescription()
#endif // !__DARWIN__ #endif // !__DARWIN__
bool wxCheckOsVersion(int majorVsn, int minorVsn)
{
int majorCur, minorCur;
wxGetOsVersion(&majorCur, &minorCur);
return majorCur > majorVsn || (majorCur == majorVsn && minorCur >= minorVsn);
}
unsigned long wxGetProcessId() unsigned long wxGetProcessId()
{ {
return (unsigned long)getpid(); return (unsigned long)getpid();