반응형
안녕하세요.
이번 포스트는 캡쳐했을 경우 프로그램 화면을 캡쳐하지 못하도록 방지하는 기능입니다.
보안이 엄격하거나 데이터가 중요한 회사의 경우 프로그램에 나타난 정보들을 캡쳐하여 유출되지 못하도록 막아야하는 경우가 있습니다.
이럴경우 어떻게 막을까요?
윈도우 이벤트 후킹을 통하여 이벤트를 감지하여 막는 방법으로 해야합니다.
프로그램에서 막고싶어도 윈도우 이벤트 이후에 동작하게되어 한발짝 늦습니다 ㅎㅎ
유니티의 지원되는 버전마다 다르며 버전에서 지원되는 .net 버전 확인후 사용하시길 바랍니다.
[Unity .net 4.X 이상 호환 버전 코드]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using CaptureProtectHandle;
using System.Diagnostics;
public class CaptureProtect : MonoBehaviour
{
//Start is called before the first frame update
void Start()
{
OnCaptureProtect();
}
private const uint WDA_NONE = 0;
private const uint WDA_MONITOR = 1;
[DllImport("user32.dll")]
private static extern uint SetWindowDisplayAffinity(IntPtr windowHandle, uint affinity);
public static void OnCaptureProtect()
{
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
var _a = SetWindowDisplayAffinity(handle, WDA_MONITOR);
//0이면 실패, 1이면 성공
UnityEngine.Debug.Log(_a);
}
}
[Unity .net 4.X 아래 버전 코드]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
public class CaptureProtection : MonoBehaviour
{
private const uint WDA_NONE = 0;
private const uint WDA_MONITOR = 1;
[DllImport("user32.dll")]
private static extern uint SetWindowDisplayAffinity(IntPtr windowHandle, uint affinity);
void Start()
{
IntPtr _handle = User32API.GetCurrentWindowHandle();
var _a = SetWindowDisplayAffinity(_handle, WDA_MONITOR);
//결과가 0이면 실패, 1이면 성공
UnityEngine.Debug.Log(_a);
}
}
public class User32API
{
private static Hashtable processWnd = null;
public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
static User32API()
{
if (processWnd == null)
{
processWnd = new Hashtable();
}
}
[DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
[DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
[DllImport("user32.dll", EntryPoint = "IsWindow")]
public static extern bool IsWindow(IntPtr hWnd);
[DllImport("kernel32.dll", EntryPoint = "SetLastError")]
public static extern void SetLastError(uint dwErrCode);
public static IntPtr GetCurrentWindowHandle()
{
IntPtr ptrWnd = IntPtr.Zero;
uint uiPid = (uint)Process.GetCurrentProcess().Id; // ID
object objWnd = processWnd[uiPid];
if (objWnd != null)
{
ptrWnd = (IntPtr)objWnd;
if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd)) //
{
return ptrWnd;
}
else
{
ptrWnd = IntPtr.Zero;
}
}
bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
// false
if (!bResult && Marshal.GetLastWin32Error() == 0)
{
objWnd = processWnd[uiPid];
if (objWnd != null)
{
ptrWnd = (IntPtr)objWnd;
}
}
return ptrWnd;
}
private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
{
uint uiPid = 0;
if (GetParent(hwnd) == IntPtr.Zero)
{
GetWindowThreadProcessId(hwnd, ref uiPid);
if (uiPid == lParam) //
{
processWnd[uiPid] = hwnd; //
SetLastError(0); //
return false; // false
}
}
return true;
}
}
감사합니다.

반응형
'Programming > Unity' 카테고리의 다른 글
| [Unity3D] 유니티 프로젝트 이름 변경 및 복사 (2) | 2024.01.09 |
|---|---|
| [Unity] Text -TextMeshPro 한글이 깨진다? 초간단 한글 나오게 하기 ! (0) | 2023.04.11 |
| [Unity/C#] 엑셀 파일 불러오기 (.xlsx) (3) | 2022.12.05 |
| [Unity] 유니티 컴파일 에러 - Assembly 'unityplastic' , PlasticSCM 관련 (2) | 2022.10.05 |
| [Unity] 유니티에서 Mysql, MariaDB 연동하고 조회하기 (1) | 2022.09.23 |
댓글