主题
保护窗口 - ProtectWindow
函数简介
为指定窗口设置 防截屏保护。被系统或第三方截屏工具捕获时,受保护窗口区域可显示为 黑屏 或 透明,从而避免窗口内容泄露。
支持两种实现方式:
- 驱动模式(
flag= 1、2):经内核驱动拦截截屏,可对 任意进程 的目标窗口生效。 - R3 模式(
flag= 3、4):调用 WindowsSetWindowDisplayAffinity,无需驱动,但 仅对本进程创建的窗口 有效。
传入 flag = 0 可 还原 窗口,取消保护。
接口名称
ProtectWindowDLL调用
c
int32_t ProtectWindow(int64_t instance, int64_t hwnd, int32_t flag);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| hwnd | 长整数型 | 目标窗口句柄。窗口须已创建并完成显示;未就绪时返回 -2(见 返回值)。 |
| flag | 整数型 | 保护标志,详见下方说明。 |
flag 保护标志
| 值 | 模式 | 说明 |
|---|---|---|
| 0 | — | 还原,取消窗口保护。 |
| 1 | 驱动 | 黑屏:截屏时该窗口区域显示为黑色。 |
| 2 | 驱动 | 透明:截屏时该窗口区域显示为透明。 |
| 3 | R3 | 黑屏(R3 模式,基于 SetWindowDisplayAffinity)。 |
| 4 | R3 | 透明(R3 模式,基于 SetWindowDisplayAffinity)。 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// flag=1 驱动模式黑屏;取消保护传 0
int ret = ola.ProtectWindow(hwnd, 1);
if (ret == 1) {
// 保护已生效
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
int ret = ola.ProtectWindow(hwnd, 0);python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
ret = ola.ProtectWindow(hwnd, 0)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
int ret = ola.ProtectWindow(hwnd, 0);go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
// flag=1 驱动模式黑屏;取消保护传 0
ret := ola.ProtectWindow(hwnd, 1)
if ret == 1 {
// 保护已生效
}rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
// flag=1 驱动模式黑屏;取消保护传 0
let ret = ola.protect_window(&hwnd, 1);
if ret == 1 {
// 保护已生效
}cpp
var ola = com("OlaPlug.OlaSoft")
var ret = ola.ProtectWindow(hwnd, 0)vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
ret = ola.ProtectWindow(hwnd, 0)text
.局部变量 ola, OLAPlug
ola.创建 ()
ret = ola.ProtectWindow(hwnd, 0)aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var ret = ola.ProtectWindow(hwnd, 0);text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
整数 ret = ola.ProtectWindow(hwnd, 0)cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
int32_t ret = ola.ProtectWindow(hwnd, 0);原生 DLL 调用
cpp
ProtectWindow(instance, hwnd, 0);csharp
using System.Runtime.InteropServices;
using System.Text;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int ProtectWindow(long ola, int hwnd, int flag);
ProtectWindow(instance, hwnd, 0);python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
ola.ProtectWindow(instance, hwnd, 0)返回值
成功返回 1;失败时接口返回值即为错误码(有符号 int32,日志里常见负数)。
错误码去哪里读
| 方式 | 说明 |
|---|---|
本接口返回值 ret | 失败时 ret 本身就是错误码,优先用它对照。 |
GetLastError / GetLastError() | 读取最后一次错误码(通常与失败时的 ret 一致)。 |
| GetLastErrorString | 读取中文说明;用完后须 FreeStringPtr 释放。 |
| 驱动错误码说明 - DriverErrorCodes | 十进制 / 十六进制完整对照表;对 ret 做 Ctrl+F 即可定位。 |
| 返回值 | 说明 |
|---|---|
1 | 成功。 |
0 | 权限不足,或未拿到驱动 Status(如通讯未建立、参数无效)。 |
| 其他 | 驱动错误码,见上方「错误码去哪里读」。 |
TIP
失败示例:int ret = ...; if (ret != 1) { /* 到驱动错误码说明页搜索 ret */ }
注意事项
| 项目 | 说明 |
|---|---|
| 驱动模式(flag 1、2) | 须以 管理员权限 运行;内置欧拉驱动会自动加载,无需手动 LoadDriver。可对 其他进程 的窗口设置保护;自签名驱动 需先 ImportDriverFromFile 或 ImportDriver。 |
| R3 模式(flag 3、4) | 使用 Windows SetWindowDisplayAffinity,不需要驱动;只能保护本进程创建的窗口,无法保护或关闭其他进程窗口。 |
| R3 特征 | R3 模式可能在部分系统或检测场景下留下 特殊窗口特征,隐蔽性通常不如驱动模式。 |
| 窗口就绪 | 目标 hwnd 须为有效且已显示的窗口;过早调用会返回 -2。 |
| 取消保护 | 需要恢复窗口正常显示时,对同一 hwnd 传入 flag = 0。 |
| 与截图模块 | 本接口影响 图像处理 中 Capture、GetScreenData 等对受保护区域的捕获效果;驱动模式与 R3 模式在各类截屏后端上的表现可能略有差异。 |
