Search code examples
winapiwindows-consolewin32-process

SetWindowDisplayAffinity does not work for console app


I get accessdenied(5) error in following console app codes, is there anyway to protect console window against screenshot?

int wmain(void)
{
    HWND hWnd = GetConsoleWindow();
    BOOL b = SetWindowDisplayAffinity(hWnd, WDA_MONITOR);
    DWORD e = GetLastError();   // e==5
    return 0;
}

Solution

  • According to the documentation:

    Parameters

    hWnd

    Type: HWND

    A handle to the top-level window. The window must belong to the current process.

    The console does not belong to the current process, so the HWND cannot be set.