Skip to content

Commit

Permalink
[win32] Use versionhelpers.h instead of GetVersionEx().
Browse files Browse the repository at this point in the history
It might be very slightly slower due to the removal of local caching,
but then again, these values are likely built into every single Windows
DLL, so it should be fast enough to just call the function every time.

[rp-download] IDownloader: Removed m_isWinXP. It's no longer needed
because we're just calling IsWindowsVistaOrGreater() on demand.
  • Loading branch information
GerbilSoft committed Jul 17, 2023
1 parent ebc9358 commit 7f9329c
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 101 deletions.
44 changes: 7 additions & 37 deletions src/librpsecure/win32/integrity_level.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* ROM Properties Page shell extension. (librpsecure/win32) *
* integrity_level.c: Integrity level manipulation for process tokens. *
* *
* Copyright (c) 2020-2022 by David Korth. *
* Copyright (c) 2020-2023 by David Korth. *
* SPDX-License-Identifier: GPL-2.0-or-later *
***************************************************************************/

Expand All @@ -11,50 +11,20 @@

#include "integrity_level.h"

// C includes.
// C includes
#include <assert.h>
#include <errno.h>
#include <malloc.h>
#include <stdlib.h>

// Windows includes.
// Windows includes
#include <sddl.h>
#include <tchar.h>
#include <versionhelpers.h>

// stdboolx
#include "stdboolx.h"

/**
* Check if we're running Windows Vista or later.
* @return True if running Vista; false if not.
*/
static bool isRunningVista(void)
{
// Are we running Windows Vista or later?
// NOTE: Technically not thread-safe, but the worst that will
// happen is two threads set isVista to the same value.
static bool isVista = false;
static bool hasCheckedVista = false;
OSVERSIONINFO osvi;

if (hasCheckedVista) {
return isVista;
}

#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4996)
#endif /* _MSC_VER */
// TODO: Use versionhelpers.h.
osvi.dwOSVersionInfoSize = sizeof(osvi);
isVista = (GetVersionEx(&osvi) && osvi.dwMajorVersion >= 6);
#ifdef _MSC_VER
# pragma warning(pop)
#endif /* _MSC_VER */

return isVista;
}

/**
* Adjust a token's integrity level.
* @param hToken Token.
Expand Down Expand Up @@ -144,7 +114,7 @@ HANDLE CreateIntegrityLevelToken(int level)
DWORD dwRet;

// Are we running Windows Vista or later?
if (!isRunningVista()) {
if (!IsWindowsVistaOrGreater()) {
// Not running Windows Vista or later.
// Can't create a low-integrity token.
return NULL;
Expand Down Expand Up @@ -210,7 +180,7 @@ int GetProcessIntegrityLevel(void)
DWORD dwLengthNeeded;

// Are we running Windows Vista or later?
if (!isRunningVista()) {
if (!IsWindowsVistaOrGreater()) {
// Not running Windows Vista or later.
// Can't get the integrity level.
return ret;
Expand Down Expand Up @@ -289,7 +259,7 @@ DWORD SetProcessIntegrityLevel(int level)
DWORD dwRet;

// Are we running Windows Vista or later?
if (!isRunningVista()) {
if (!IsWindowsVistaOrGreater()) {
// Not running Windows Vista or later.
// Can't set the process integrity level.
// We'll pretend everything "just works" anyway.
Expand Down
33 changes: 6 additions & 27 deletions src/librpsecure/win32/secoptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* ROM Properties Page shell extension. (librpsecure/win32) *
* secoptions.c: Security options for executables. *
* *
* Copyright (c) 2016-2022 by David Korth. *
* Copyright (c) 2016-2023 by David Korth. *
* SPDX-License-Identifier: GPL-2.0-or-later *
***************************************************************************/

Expand All @@ -11,17 +11,18 @@

#include "secoptions.h"

// C includes.
// C includes
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

// Windows includes.
// Windows includes
#include <windows.h>
#include <sdkddkver.h>
#include <winternl.h>
#include <tchar.h>
#include <versionhelpers.h>

#ifndef _WIN64

Expand Down Expand Up @@ -216,7 +217,6 @@ static DWORD HardenProcessIntegrityLevelPolicy(void)
*/
int rp_secure_win32_secoptions_init(int bHighSec)
{
OSVERSIONINFO osvi;
HMODULE hKernel32;
PFNHEAPSETINFORMATION pfnHeapSetInformation;
PFNSETDLLDIRECTORYW pfnSetDllDirectoryW;
Expand All @@ -243,25 +243,6 @@ int rp_secure_win32_secoptions_init(int bHighSec)
return GetLastError();
}

#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4996)
#endif /* _MSC_VER */
// GetVersionEx() should never fail...
osvi.dwOSVersionInfoSize = sizeof(osvi);
bRet = GetVersionEx(&osvi);
assert(bRet != 0);
if (!bRet) {
// GetVersionEx() failed somehow.
// Assume we're running Windows XP.
osvi.dwMajorVersion = 5;
osvi.dwMinorVersion = 1;
osvi.dwBuildNumber = 2600;
}
#ifdef _MSC_VER
# pragma warning(pop)
#endif /* _MSC_VER */

/** BEGIN: Windows XP/2003 **/
// Remove the current directory from the DLL search path.
// TODO: Enable and test this.
Expand Down Expand Up @@ -327,7 +308,7 @@ int rp_secure_win32_secoptions_init(int bHighSec)
}
#endif /* !_WIN64 */

if (osvi.dwMajorVersion < 6) {
if (!IsWindowsVistaOrGreater()) {
// We're done here.
return 0;
}
Expand All @@ -338,9 +319,7 @@ int rp_secure_win32_secoptions_init(int bHighSec)
// Harden the process's integrity level policy.
HardenProcessIntegrityLevelPolicy();

if ((osvi.dwMajorVersion == 6 && osvi.dwMinorVersion < 2) ||
osvi.dwMajorVersion < 7)
{
if (!IsWindows8OrGreater()) {
// We're done here.
return 0;
}
Expand Down
12 changes: 0 additions & 12 deletions src/rp-download/IDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ IDownloader::IDownloader()
, m_if_modified_since(-1)
, m_maxSize(0)
, m_inProgress(false)
#ifdef _WIN32
, m_isWinXP(false)
#endif /* _WIN32 */
{
createUserAgent();
}
Expand All @@ -45,9 +42,6 @@ IDownloader::IDownloader(const TCHAR *url)
, m_if_modified_since(-1)
, m_maxSize(0)
, m_inProgress(false)
#ifdef _WIN32
, m_isWinXP(false)
#endif /* _WIN32 */
{
createUserAgent();
}
Expand All @@ -58,9 +52,6 @@ IDownloader::IDownloader(const tstring &url)
, m_if_modified_since(-1)
, m_maxSize(0)
, m_inProgress(false)
#ifdef _WIN32
, m_isWinXP(false)
#endif /* _WIN32 */
{
createUserAgent();
}
Expand Down Expand Up @@ -263,9 +254,6 @@ tstring IDownloader::getOSRelease(void)
_sntprintf(buf, _countof(buf), _T("%lu.%lu"), osvi.dwMajorVersion, osvi.dwMinorVersion);
s_os_release += buf;

// Check if we're using an older (pre-Vista) version of Windows.
m_isWinXP = (osvi.dwMajorVersion < 6);

# ifdef _WIN64
s_os_release += _T("; Win64");
# else /* !_WIN64 */
Expand Down
3 changes: 0 additions & 3 deletions src/rp-download/IDownloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ class IDownloader
std::tstring m_userAgent; // User-Agent

bool m_inProgress; // Set when downloading
#ifdef _WIN32
bool m_isWinXP; // Set for Windows versions older than Vista
#endif /* _WIN32 */
};

}
9 changes: 5 additions & 4 deletions src/rp-download/WinInetDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* ROM Properties Page shell extension. (rp-download) *
* WinInetDownloader.cpp: WinInet-based file downloader. *
* *
* Copyright (c) 2016-2022 by David Korth. *
* Copyright (c) 2016-2023 by David Korth. *
* SPDX-License-Identifier: GPL-2.0-or-later *
***************************************************************************/

Expand All @@ -14,11 +14,12 @@
#include "libwin32common/w32err.hpp"
#include "libwin32common/w32time.h"

// C++ STL classes.
// C++ STL classes
using std::string;
using std::wstring;

// Windows includes.
// Windows includes
#include <versionhelpers.h>
#include <wininet.h>

namespace RpDownload {
Expand Down Expand Up @@ -69,7 +70,7 @@ int WinInetDownloader::download(void)
INTERNET_FLAG_NO_AUTH |
INTERNET_FLAG_NO_COOKIES |
INTERNET_FLAG_NO_UI;
if (m_isWinXP) {
if (!IsWindowsVistaOrGreater()) {
// WinInet doesn't support SNI prior to Vista.
static const TCHAR rpdb_domain[] = _T("https://rpdb.gerbilsoft.com/");
if (m_url.size() >= _countof(rpdb_domain) &&
Expand Down
15 changes: 7 additions & 8 deletions src/svrplus/svrplus.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
#include "librpsecure/restrict-dll.h"

// Additional Windows headers
#include <windowsx.h>
#include <shellapi.h>
#include <commctrl.h>
#include <shellapi.h>
#include <versionhelpers.h>
#include <windowsx.h>

// Older versions of the Windows SDK might be missing some ARM systems.
#ifndef IMAGE_FILE_MACHINE_ARM
Expand Down Expand Up @@ -669,8 +670,8 @@ static void InitDialog(HWND hDlg)
LPCTSTR s_strtbl;
int ls_ret;

// OS version check.
OSVERSIONINFO osvi;
// OS version check
// MSVC 2022 runtime requires Windows Vista or later.
unsigned int vcyear, vcver;

static_assert(ARRAY_SIZE(g_archs) == ARRAY_SIZE(bHasMsvcForArch), "bHasMsvcForArch[] is out of sync with g_archs[]!");
Expand Down Expand Up @@ -735,14 +736,12 @@ static void InitDialog(HWND hDlg)
SetWindowText(GetDlgItem(hDlg, IDC_STATIC_DESC), (ls_ret > 0) ? s_strtbl : _T("RES ERR"));

// MSVC 2022 runtime requires Windows Vista or later.
osvi.dwOSVersionInfoSize = sizeof(osvi);
if (GetVersionEx(&osvi) != 0 && osvi.dwMajorVersion >= 6) {
if (IsWindowsVistaOrGreater()) {
// Windows Vista or later. Use MSVC 2022.
vcyear = 2022;
vcver = 17;
} else {
// Windows XP/2003 or earlier, or GetVersionEx() failed.
// Use MSVC 2017.
// Windows XP/2003 or earlier. Use MSVC 2017.
vcyear = 2017;
vcver = 15;
}
Expand Down
8 changes: 3 additions & 5 deletions src/win32/AchWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ using namespace LibRpBase;
using LibRpFile::RpFile_windres;
using LibRpTexture::rp_image;

// ROM icon.
// ROM icon
#include "config/PropSheetIcon.hpp"

// C++ STL classes.
// C++ STL classes
using std::string;
using std::tstring;
using std::unordered_map;
Expand Down Expand Up @@ -362,9 +362,7 @@ int AchWin32Private::notifyFunc(Achievements::ID id)
// TODO: DPI awareness.
const UINT dpi = rp_GetDpiForWindow(hNotifyWnd);
int iconSize;
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(osvi);
if (GetVersionEx(&osvi) && osvi.dwMajorVersion >= 6) {
if (IsWindowsVistaOrGreater()) {
// Windows Vista or later. Use the large icon.
nid.dwInfoFlags |= NIIF_LARGE_ICON;
iconSize = 32;
Expand Down
8 changes: 4 additions & 4 deletions src/win32/RP_ExtractIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ RP_ExtractIcon_Private::RP_ExtractIcon_Private()
// Enable icon squaring only on Windows XP.
// On Windows 7 and 11, it causes the icon to look "squished"
// if the original image is taller than it is wide.
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(osvi);
if (GetVersionEx(&osvi) == 0 || osvi.dwMajorVersion < 6) {
// GetVersionEx() failed, or we're running Windows XP.
if (!IsWindowsVistaOrGreater()) {
// Not running Windows Vista or later.
// This must be Windows XP or earlier.
// Enable icon squaring.
thumbnailer.setDoSquaring(true);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/win32/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
#include "libwin32common/sdk/windowsx_ts.h"
#include "libwin32common/sdk/commctrl_ts.h"

// Additional Windows headers.
// Additional Windows headers
#include <olectl.h>
#include <shellapi.h>
#include <shlwapi.h>
#include <commdlg.h>
#include <objidl.h>
#include <versionhelpers.h>

// FIXME: shlobj.h on MinGW-w64 on AppVeyor doesn't properly inline a few
// functions when building in C mode, resulting in multiple definition errors.
Expand Down

0 comments on commit 7f9329c

Please sign in to comment.