How to dynamically call/or not call Windows.Graphics.Capture's API based on Windows 10 version

xin luo 1 Reputation point
2021-09-30T15:35:27.64+00:00

Hi Team,
My Windows C++ desktop application is required to run on all versions of Windows 10 operating systems, and I do want to use Windows.Graphics.Capture APIs to capture other application's window. I Know that Windows.Graphics.Capture is only supported on Windows 10, version 1803 or above. So how to dynamically call/or not call Windows.Graphics.Capture APIs based on Windows 10 version. For example, at the run time, if my application detects that OS is Windows 10 version below 1803, my app will not load the Windows.Graphics.Capture API dll and will not call Windows.Graphics.Capture APIs; if version is 1803 or above, my app will load Windows.Graphics.Capture API dll and call Windows.Graphics.Capture APIs.

Thanks for your help for resolving this issue,

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,426 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Roman R 331 Reputation points
    2021-09-30T17:39:50.25+00:00

    The best place to ask questions on Windows.Graphics.Capture is here: https://github.com/robmikh/Win32CaptureSample/issues/33#issuecomment-774807345

    ApiInformation::IsApiContractPresent is the one you need.

    1 person found this answer helpful.

  2. xin luo 1 Reputation point
    2021-10-03T16:27:34.857+00:00

    Just added two screenshots here, one is win10 version on my machine, the other is runtime error message when running on Windows 10, version 1607. Thanks for your help again.

    137100-win10-version.png137202-error-to-launch.png

    0 comments No comments

  3. Castorix31 81,741 Reputation points
    2021-10-06T13:53:11.11+00:00

    Maybe you can do a test like this (I cannot test if it fails as I have Windows 10 1909) :

    BOOL bOK = FALSE;
    HMODULE hInst = LoadLibrary(L"CoreMessaging.dll");
    if (hInst)
    {
     FARPROC pFunction = (FARPROC&)pFunction = GetProcAddress(hInst, "CreateDispatcherQueueController");
     if (pFunction)
     {
     static IGraphicsCaptureSessionStatics* pStatics;
     HRESULT hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Graphics_Capture_GraphicsCaptureSession).Get(), IID_PPV_ARGS(&pStatics));
     if (SUCCEEDED(hr))
     bOK = TRUE;
     }
    }
    

    At beginning :

    #include <winrt/Windows.Foundation.h>
    #include <winrt/base.h>
    #include <wrl\wrappers\corewrappers.h>  // HString::MakeReference
    using namespace Microsoft::WRL::Wrappers;
    using namespace winrt;
    #include <roapi.h>
    #include <windows.graphics.capture.h>
    using namespace ABI::Windows::Graphics::Capture;
    #pragma comment (lib, "windowsapp")