yeasir007

Tuesday, October 30, 2018

How to prevent/disable/protect screen capture of windows application (Winform,WPF,Win32,MFC etc.)

Microsoft has been developed an API named  SetWindowDisplayAffinity to support the window content protection. This feature enables applications to protect application content from being captured or copied through a specific set of public operating system features and APIs. Unlike other security features or an implementation of DRM there is no guarantee that this API can strictly protect window content, for example where someone takes a photograph of screen using mobile camera. But this API is feasible and easy to use.



BOOL SetWindowDisplayAffinity(HWND hWnd,DWORD dwAffinity);

There are two types of DWORD values WDA_MONITOR and WDA_NONE. We have to set WDA_MONITOR to protect the application contents.


It's basically C++ API but we can use this for other windows C# application like Winform or WPF by extern the C++ API's. (Sample code C#)


using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsAppPrtScrProtector
{
    public partial class Form1 : Form
    {
        const uint WDA_NONE = 0;
        const uint WDA_MONITOR = 1;

        [DllImport("user32.dll")]
        public static extern uint SetWindowDisplayAffinity(IntPtr hWnd, uint dwAffinity);
        public Form1()
        {
           InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
             SetWindowDisplayAffinity(this.Handle, WDA_MONITOR);
        } 
    }
 }


Note:
For WPF you have to create Handler by your self for SetWindowDisplayAffinity's first
parameter like bellow:

private void ContentControl_Loaded(object sender, RoutedEventArgs e){
        IntPtr hwnd = new WindowInteropHelper(this).Handle;
        SetWindowDisplayAffinity(hwnd, WDA_MONITOR);
 }


We can use this API with C++ application easily. Here bellow is the code: (Sample code C++)


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   SetWindowDisplayAffinity(hWnd, WDA_MONITOR);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}


You have to include "Winuser.h" header to use this API. It will not work with visual studio 2008 or lower version. You can use this with visual studio 2010 and upper version only.


Happy coding. :)


18 comments:

  1. Replies
    1. Its Not C or C++. Its c# (csharp).

      Delete
    2. How To Prevent/Disable/Protect Screen Capture Of Windows Application (Winform,Wpf,Win32,Mfc Etc.) - Yeasir007 >>>>> Download Now

      >>>>> Download Full

      How To Prevent/Disable/Protect Screen Capture Of Windows Application (Winform,Wpf,Win32,Mfc Etc.) - Yeasir007 >>>>> Download LINK

      >>>>> Download Now

      How To Prevent/Disable/Protect Screen Capture Of Windows Application (Winform,Wpf,Win32,Mfc Etc.) - Yeasir007 >>>>> Download Full

      >>>>> Download LINK gm

      Delete
  2. I am getting the access denied error in SetWindowDisplayAffinity

    ReplyDelete
  3. I am getting the access denied error in SetWindowDisplayAffinity

    ReplyDelete
    Replies
    1. Download the sample code or post your code here to observe the error details.

      Delete
  4. Hi thank you for your post. I can prevent user to capture my Content from WPF media element tools. But the sound Still capture.
    How can I Detect the SetWindowDisplayAffinity? So when this function is execute my player stop working to and show the message to user close that program if they want use my app?

    ReplyDelete
    Replies
    1. SetWindowDisplayAffinity(https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowdisplayaffinity) will make sure to capture the screen of the application. As almost every screen recorder capture the image, then makes it dynamic. So it will make sure to protect the screen not the audio portion. If you want to protect your audio also then make it complex by hardware rendering and HDCP(https://en.wikipedia.org/wiki/High-bandwidth_Digital_Content_Protection) for your output.

      You can also check Microsoft's latest API "PlayReady DRM"(https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/playready-client-sdk) which is available for UWP but as we know there is a way to use WINRT API in WPF. So you can try this out.

      Delete
    2. thank you I test it and write here the result. But I have question are this API Only Support win 10 or Support the another version of windows too?

      Delete
    3. Minimum supported client: Windows 7 [desktop apps only]
      Minimum supported server :Windows Server 2008 R2 [desktop apps only]

      Delete
  5. I have test with window 7 but it's not works.

    ReplyDelete
  6. Can you please guide me why screen capture not prevent in window 7.

    ReplyDelete
    Replies
    1. Please download the sample project and run in your PC and let me know the result.

      Delete
  7. can u tell me how to overcome this as a user ?

    ReplyDelete
  8. In windows 7 it nevers work but with the windows 10 it is working with the same code can you please tell me any solution of this?

    ReplyDelete
  9. thank you alot, works just fine with windows 10.

    ReplyDelete
  10. How do i make it transparent Like it just doesnt show at all in recording

    ReplyDelete
  11. How To Prevent/Disable/Protect Screen Capture Of Windows Application (Winform,Wpf,Win32,Mfc Etc.) - Yeasir007 >>>>> Download Now

    >>>>> Download Full

    How To Prevent/Disable/Protect Screen Capture Of Windows Application (Winform,Wpf,Win32,Mfc Etc.) - Yeasir007 >>>>> Download LINK

    >>>>> Download Now

    How To Prevent/Disable/Protect Screen Capture Of Windows Application (Winform,Wpf,Win32,Mfc Etc.) - Yeasir007 >>>>> Download Full

    >>>>> Download LINK x8

    ReplyDelete

Thanks for your comments