在 dll 中对 Windows 7 和 Windows 10 使用不同的函数

Using different functions for Windows 7 and Windows 10 in dll

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

有没有人有什么建议。我愿意在我的代码中保留 GetScaleFactorForMonitor

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

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
  ...
}

您可能想根据需要重新安排此内容,但您应该明白了。