原生库模块为性能关键操作提供 C++ 实现,特别是 DirectX Graphics Infrastructure (DXGI) 屏幕捕获功能。
| English Version | 中文版本 |
原生库模块包含:
原生库提供对 DirectX 功能的低级访问:
DXGI 复制 API 的原生 C++ 实现。
由 AutoHotkey 脚本加载的预编译原生库。
低级 DirectX Graphics Infrastructure API 包装器。
// 初始化和管理
int DX_Init(int output, int fps, const char* dllPath);
int DX_Shutdown();
bool DX_IsReady();
// 像素检索
int DX_GetPixel(int x, int y);
// 输出管理
int DX_EnumOutputs();
const char* DX_GetOutputName(int idx);
int DX_SelectOutput(int idx);
// 配置
void DX_SetFPS(int fps);
// 错误处理
DX_ErrorInfo DX_LastError();
全面的错误报告和恢复机制。
struct DX_ErrorInfo {
int Code; // 错误代码
const char* Text; // 错误描述
const char* Function; // 函数名
int Line; // 源代码行
DWORD SystemError; // 系统错误代码
};
实时性能指标和统计信息。
struct DX_PerformanceStats {
DWORD FrameCount; // 处理的总帧数
DWORD PixelRequests; // 像素检索请求
DWORD Errors; // 错误计数
DWORD RecoveryAttempts; // 恢复尝试
DWORD LastFrameTime; // 最后帧处理时间
DWORD AverageFrameTime; // 平均帧时间
};
初始化 DXGI 复制库。
参数:
output(整数):显示输出索引(从 0 开始)fps(整数):目标帧率dllPath(const char*):DLL 文件路径返回:整数(0 = 成功,负数 = 错误)
错误代码:
-1:DLL 加载失败-2:DXGI 初始化失败-3:输出选择失败-4:复制设置失败关闭 DXGI 复制库并释放资源。
参数:无
返回:整数(0 = 成功,负数 = 错误)
检查 DXGI 库是否准备好进行操作。
参数:无
返回:布尔值(true = 就绪,false = 未就绪)
检索指定坐标处的像素颜色。
参数:
x(整数):X 坐标y(整数):Y 坐标返回:整数(RGB 颜色值,-1 = 错误)
枚举可用的显示输出。
参数:无
返回:整数(可用输出数量)
获取指定输出的名称。
参数:
idx(整数):输出索引(从 0 开始)返回:const char*(输出名称,nullptr = 错误)
选择指定输出进行复制。
参数:
idx(整数):输出索引(从 0 开始)返回:整数(0 = 成功,负数 = 错误)
设置复制的帧率。
参数:
fps(整数):目标帧率返回:void
获取最后的错误信息。
参数:无
返回:DX_ErrorInfo(错误信息结构)
// 初始化 DXGI 库
int result = DX_Init(0, 60, "dxgi_dup.dll");
if (result == 0) {
// 库初始化成功
printf("DXGI 库初始化成功\n");
} else {
// 处理错误
DX_ErrorInfo error = DX_LastError();
printf("初始化失败: %s\n", error.Text);
}
// 检查库状态
if (DX_IsReady()) {
// 检索像素颜色
int color = DX_GetPixel(100, 200);
if (color != -1) {
printf("像素颜色: 0x%06X\n", color);
} else {
DX_ErrorInfo error = DX_LastError();
printf("像素检索失败: %s\n", error.Text);
}
}
// 枚举可用输出
int outputCount = DX_EnumOutputs();
printf("可用输出数量: %d\n", outputCount);
// 列出所有输出名称
for (int i = 0; i < outputCount; i++) {
const char* name = DX_GetOutputName(i);
if (name) {
printf("输出 %d: %s\n", i, name);
}
}
// 尝试操作并处理错误
int result = DX_SelectOutput(1);
if (result != 0) {
DX_ErrorInfo error = DX_LastError();
printf("错误代码: %d\n", error.Code);
printf("错误描述: %s\n", error.Text);
printf("函数: %s\n", error.Function);
printf("行号: %d\n", error.Line);
}
DLL 路径可以通过配置文件指定:
// 从配置加载 DLL 路径
const char* dllPath = Config_GetString("DXGI", "DllPath", "dxgi_dup.dll");
帧率和其他性能设置可以通过配置调整:
// 从配置加载帧率设置
int targetFPS = Config_GetInt("DXGI", "TargetFPS", 60);
DX_SetFPS(targetFPS);
原生库包含全面的错误处理:
库提供实时性能监控:
// 获取性能统计
DX_PerformanceStats stats = DX_GetPerformanceStats();
printf("帧数: %lu\n", stats.FrameCount);
printf("平均帧时间: %lu ms\n", stats.AverageFrameTime);
详细的错误信息用于调试:
// 启用详细错误报告
DX_EnableDebug(true);
// 获取调试信息
DX_DebugInfo debugInfo = DX_GetDebugInfo();