5 Replies - 3052 Views - Last Post: 22 April 2016 - 02:46 PM

#1 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,808
  • Joined: 01-February 13

Protecting your application`s sensitive data from ScreenShots

Posted 17 April 2016 - 12:33 PM

Have you ever wanted to protect sensitive data on your application from being captured by a ScreenShot. This will do it on Win7 and i believe it will work on Win8 and Win10 also. I don`t have 8 or 10 to test it on but, i believe it should work on them too.

This can be defeated in Win7 if the person knows what you have done and has the knowledge to turn it off but, most common people will not know how to do that. This could also be defeated by simply snapping an image of the screen with a camera. So, i don`t claim that this is fool proof but, it is handy for several situations.

First, here is a small class called Protector that you can add to your project.
Imports System.Runtime.InteropServices

Public Class Protector
    Private Const WDA_NONE As UInteger = &H0
    Private Const WDA_MONITOR As UInteger = &H1

    <DllImport("user32.dll")>
    Private Shared Function SetWindowDisplayAffinity(ByVal hWnd As IntPtr, ByVal dwAffinity As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    Public Shared Function ScreenShotProtect(ByVal winForm As Form, ByVal Protect As Boolean) As Boolean
        Dim result As Boolean = False
        If winForm IsNot Nothing Then
            If Protect Then
                result = SetWindowDisplayAffinity(winForm.Handle, WDA_MONITOR)
            Else
                result = SetWindowDisplayAffinity(winForm.Handle, WDA_NONE)
            End If
        End If
        Return result
    End Function
End Class



Then you can call the ScreenShotProtect function from your Form`s code by passing the instance of the Form and a Boolean value of True or False to turn the protection on or off. Here is a simple example that uses a CheckBox on the Form to turn it on and off.
Public Class Form1

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If Not Protector.ScreenShotProtect(Me, CheckBox1.Checked) Then
            Me.Text = "Protection Failed"
        End If
    End Sub

End Class



The first image is with the protection turned off, and the second image with the protection on.
Attached Image
Attached Image

This post has been edited by IronRazer: 17 April 2016 - 12:35 PM


Is This A Good Question/Topic? 2
  • +

Replies To: Protecting your application`s sensitive data from ScreenShots

#2 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,535
  • Joined: 07-September 13

Re: Protecting your application`s sensitive data from ScreenShots

Posted 22 April 2016 - 04:00 AM

Hello IronRazer,

Is there a way to apply this to say a textbox?

What I am thinking is a screen can sometimes provide useful details in terms of debug reasons, however there may be parts of the screen for example banking details that you would want blanked out, when taking print screens.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6562
  • View blog
  • Posts: 26,614
  • Joined: 12-December 12

Re: Protecting your application`s sensitive data from ScreenShots

Posted 22 April 2016 - 04:52 AM

You could introduce the PasswordChar before printing.
Was This Post Helpful? 0
  • +
  • -

#4 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,535
  • Joined: 07-September 13

Re: Protecting your application`s sensitive data from ScreenShots

Posted 22 April 2016 - 05:45 AM

Sorry when I said print screen I meant screenshot.

As far as I know you can't detect when the users are taking a screenshot.
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,808
  • Joined: 01-February 13

Re: Protecting your application`s sensitive data from ScreenShots

Posted 22 April 2016 - 11:56 AM

@ maceysoftware,

I had thought about that before posting this snippet but, it did not work on a child window. The documents on the SetWindowDisplayAffinity function don`t say specifically that it only works on top level windows (A Form) but, that seems to be the case, that it only works on top level windows.

And yes, you are correct, there is no easy way to detect a screenshot being taken. There may be a way to hook the system messages to monitor any calls to any Api functions that can be used to capture the screen. I never tried it because the hook for it would have to be written in C++. It has been a few years since i messed with unmanaged C++ so, it just seemed like a PITA to mess with at the time.
Was This Post Helpful? 0
  • +
  • -

#6 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,535
  • Joined: 07-September 13

Re: Protecting your application`s sensitive data from ScreenShots

Posted 22 April 2016 - 02:46 PM

As it only took a handle (intptr) i had tried to see if i could pass in a textbox handle and was a little disappointed that it didn't work.

However i have stored this little beaut of a snippet away
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1