Not able to disable screen capture in WPF using SetWindowDisplayAffinity

Vardan Balyan 10 Reputation points

Hi i have a WPF application based on .NET 6 and i want to disable the screen capture for my app and found SetWindowDisplayAffinity but this code is not working in my project. If i debug my code by getting the last error from Marshal.GetLastWin32Error() then it returns value 87 (ERROR_INVALID_PARAMETER).

What am i doing wrong here? Or am i missing something important?


Here is my code for reference :

[DllImport("User32.dll", SetLastError = true)]
private static extern bool SetWindowDisplayAffinity(IntPtr hWnd, uint dwAffinity);

public const int WDA_NONE = 0x00000000;
public const int WDA_MONITOR = 0x00000001;
public const int WDA_EXCLUDEFROMCAPTURE = 0x00000011;

private void Window_Loaded(object sender, RoutedEventArgs e)
     SetWindowDisplayAffinity(new WindowInteropHelper(this).Handle, WDA_MONITOR);

     var res = Marshal.GetLastWin32Error();
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,708 questions
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,507 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,601 questions
{count} vote