幽冥大陆(五十五)ASR SetThreadInformation C语言识别到自动化软件

一、函数支持的系统版本

SetThreadInformation是 Windows 系统的一个内核相关 API,它的支持情况如下:

  1. 最低支持的客户端版本 :Windows 10 版本 1607(即周年更新版,内部版本 14393)
  2. 最低支持的服务器版本:Windows Server 2016
  3. 不支持的系统版本:Windows 7、Windows 8/8.1、Windows 10 1607 之前的版本、Windows Server 2008/2012/2012 R2 等早期服务器系统

二、补充说明

  1. 头文件与库依赖 使用这个函数时,需要包含 Windows 的头文件processthreadsapi.h,并且链接Kernel32.lib库(这是 Windows 系统的标准库,通常编译器会自动链接)。

  2. 函数使用的注意事项 该函数需要特定的权限,且第二个参数ThreadInformationClass(线程信息类)目前仅支持ThreadMemoryPriority(值为 0)这一个选项,用于设置线程的内存优先级。示例代码如下:

    cpp

    运行

    复制代码
    #include <windows.h>
    #include <processthreadsapi.h>
    #include <iostream>
    
    int main() {
        // 获取当前线程的句柄
        HANDLE hThread = GetCurrentThread();
        if (hThread == NULL) {
            std::cerr << "获取线程句柄失败,错误码:" << GetLastError() << std::endl;
            return 1;
        }
    
        // 设置线程的内存优先级为MEMORY_PRIORITY_NORMAL(值为5)
        MEMORY_PRIORITY_INFORMATION memPriority = { MEMORY_PRIORITY_NORMAL };
        BOOL result = SetThreadInformation(
            hThread,
            ThreadMemoryPriority,  // 仅支持该信息类
            &memPriority,
            sizeof(memPriority)
        );
    
        if (result) {
            std::cout << "线程内存优先级设置成功" << std::endl;
        } else {
            std::cerr << "设置失败,错误码:" << GetLastError() << std::endl;
        }
    
        return 0;
    }
  3. 兼容性判断 如果你的程序需要兼容旧版 Windows 系统,可以在运行时通过GetVersionExVerifyVersionInfo函数判断系统版本,或者通过GetProcAddress检查Kernel32.dll中是否存在SetThreadInformation函数,避免程序在不支持的系统上崩溃。

阿雪技术观

让我们积极投身于技术共享的浪潮中,不仅仅是作为受益者,更要成为贡献者。无论是分享自己的代码、撰写技术博客,还是参与开源项目的维护和改进,每一个小小的举动都可能成为推动技术进步的巨大力量

Embrace open source and sharing, witness the miracle of technological progress, and enjoy the happy times of humanity! Let's actively join the wave of technology sharing. Not only as beneficiaries, but also as contributors. Whether sharing our own code, writing technical blogs, or participating in the maintenance and improvement of open source projects, every small action may become a huge force driving technological progrss.

相关推荐
开开心心就好2 小时前
免费卸载工具,可清理残留批量管理启动项
linux·运维·服务器·windows·随机森林·pdf·1024程序员节
Lbwnb丶2 小时前
检测服务器是否是虚拟化,如KVM,VM等
linux·运维·服务器
老猿讲编程2 小时前
【车载信息安全系列4】基于Linux中UIO的HSE应用实现
linux·运维·服务器
鸡吃丸子2 小时前
初识Docker
运维·前端·docker·容器
wanhengidc3 小时前
巨椰 云手机 云游戏稳定运行
运维·服务器·arm开发·游戏·云计算
林义满3 小时前
大促零宕机背后的运维升级:长三角中小跨境电商的架构优化实践
大数据·运维·架构
linweidong4 小时前
顺丰运维面试题及参考答案
运维·nginx·容器·ansible·运维开发·防火墙·python面试
qq_455760854 小时前
docker run
运维·docker·容器
叽里咕噜怪4 小时前
Docker-基础
运维·docker·容器