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

Using different functions for Windows 7 and Windows 10 in dll

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

I created dynamic library(dll) which is using function GetScaleFactorForMonitor from windows API. However this function was introduced in Windows 8.1 and those dll won't load at Windows 7 obviously. I am thinking about the solution to have two versions of the same method in one dll and use it according to Windows version.

Does anyone have any suggestions. I would be up to keep GetScaleFactorForMonitor in my code.

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

You basically need something like this:

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

You probably want to rearrange this for your needs, but you should get the idea.