I am trying to import an external DLL based on the users OSVersion. Problem started by using the method GetDpiForWindow() which requires Windows 10, version 1607. As this method is missing in older OS Versions like windows 7 an exception is thrown as soon as the class instance is initiated. Is it possible to implement a OSVersion check before the dll gets imported? In this special case also catch of the import exception would do the trick as I could return 96.
public static class WindowUtil
{
[DllImport("user32.dll")]
static extern int GetDpiForWindow(IntPtr hWnd);
public static double GetWindowDPIFactor(IntPtr winhandle)
{
if (winhandle == IntPtr.Zero)
{
return 1;
}
return (double)GetDpiForWindow(winhandle) / 96;
}
}