Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Fix missing shcore.dll error on Windows 7 #30699

Merged
merged 1 commit into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion third_party/accessibility/ax/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ source_set("ax") {
]
libs = [
"oleacc.lib",
"shcore.lib",
"uiautomationcore.lib",
]
}
Expand Down
70 changes: 67 additions & 3 deletions third_party/accessibility/base/win/display.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,75 @@
namespace base {
namespace win {

namespace {

template <typename T>
bool AssignProcAddress(HMODULE comBaseModule, const char* name, T*& outProc) {
outProc = reinterpret_cast<T*>(GetProcAddress(comBaseModule, name));
return *outProc != nullptr;
}

// Helper class for supporting display scale factor lookup across Windows
// versions, with fallbacks where these lookups are unavailable.
class ScaleHelperWin32 {
public:
ScaleHelperWin32();
~ScaleHelperWin32();

/// Returns the scale factor for the specified monitor. Sets |scale| to
/// SCALE_100_PERCENT if the API is not available.
HRESULT GetScaleFactorForMonitor(HMONITOR hmonitor,
DEVICE_SCALE_FACTOR* scale) const;

private:
using GetScaleFactorForMonitor_ =
HRESULT __stdcall(HMONITOR hmonitor, DEVICE_SCALE_FACTOR* scale);

GetScaleFactorForMonitor_* get_scale_factor_for_monitor_ = nullptr;

HMODULE shlib_module_ = nullptr;
bool scale_factor_for_monitor_supported_ = false;
};

ScaleHelperWin32::ScaleHelperWin32() {
if ((shlib_module_ = LoadLibraryA("Shcore.dll")) != nullptr) {
scale_factor_for_monitor_supported_ =
AssignProcAddress(shlib_module_, "GetScaleFactorForMonitor",
get_scale_factor_for_monitor_);
}
}

ScaleHelperWin32::~ScaleHelperWin32() {
if (shlib_module_ != nullptr) {
FreeLibrary(shlib_module_);
}
}

HRESULT ScaleHelperWin32::GetScaleFactorForMonitor(
HMONITOR hmonitor,
DEVICE_SCALE_FACTOR* scale) const {
if (hmonitor == nullptr || scale == nullptr) {
return E_INVALIDARG;
}
if (!scale_factor_for_monitor_supported_) {
*scale = SCALE_100_PERCENT;
return S_OK;
}
return get_scale_factor_for_monitor_(hmonitor, scale);
}

ScaleHelperWin32* GetHelper() {
static ScaleHelperWin32* helper = new ScaleHelperWin32();
return helper;
}

} // namespace

float GetScaleFactorForHWND(HWND hwnd) {
HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
DEVICE_SCALE_FACTOR scale_factor = DEVICE_SCALE_FACTOR_INVALID;
if (SUCCEEDED(GetScaleFactorForMonitor(monitor, &scale_factor))) {
return ScaleFactorToFloat(scale_factor);
DEVICE_SCALE_FACTOR scale = DEVICE_SCALE_FACTOR_INVALID;
if (SUCCEEDED(GetHelper()->GetScaleFactorForMonitor(monitor, &scale))) {
return ScaleFactorToFloat(scale);
}
return 1.0f;
}
Expand Down