Not able to disable screen capture in WPF using SetWindowDisplayAffinity

Vardan Balyan 10 Reputation points
2023-05-29T04:17:28.3833333+00:00

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?

Thanks

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();
}
Developer technologies | Windows Presentation Foundation
Windows development | Windows API - Win32
Developer technologies | C#
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.