Warm tip: This article is reproduced from serverfault.com, please click

c++-在DLL中为Windows 7和Windows 10使用不同的功能

(c++ - Using different functions for Windows 7 and Windows 10 in dll)

发布于 2020-12-01 12:46:50

我创建了动态库(dll),该库正在使用GetScaleFactorForMonitorWindows API中的函数。但是,此功能是Windows 8.1中引入的,那些dll在Windows 7中显然不会加载。我正在考虑该解决方案,即在一个dll中具有相同方法的两个版本,并根据Windows版本使用它。

有没有人有什么建议。我将保留GetScaleFactorForMonitor我的代码。

Questioner
PPM
Viewed
11
446k 2020-12-02 01:56:17

你基本上需要这样的东西:

typedef HRESULT CALLBACK GETSCALEFACTORFORMONITOR(HMONITOR hMon, DEVICE_SCALE_FACTOR* pScale);

...

HMODULE hm = LoadLibrary("Shcore.dll");     // GetScaleFactorForMonitor is here

GETSCALEFACTORFORMONITOR* pGETSCALEFACTORFORMONITOR = NULL;

if (hm)
{
  pGETSCALEFACTORFORMONITOR = (GETSCALEFACTORFORMONITOR*)GetProcAddress(hm, "GetScaleFactorForMonitor");
}

if (pGETSCALEFACTORFORMONITOR)
{ 
  // GetScaleFactorForMonitor exists, call it like this:
  HRESULT hr = (*pGETSCALEFACTORFORMONITOR)(whatever parameters);   // call GetScaleFactorForMonitor
  // instead of like this:
  // HRESULT hr = GetScaleFactorForMonitor(whatever parameters);
}
else
{
  // GetScaleFactorForMonitor not available
  ...
}

你可能想根据自己的需要进行重新安排,但是你应该明白这一点。