幽冥大陆(五十五)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 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
DianSan_ERP2 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅3 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
不是二师兄的八戒3 天前
Linux服务器挂载OSS存储的完整实践指南
linux·运维·服务器
zhangfeng11333 天前
趋动云 如何ssh登录 服务区 项目server
运维·人工智能·ssh
ZeroNews内网穿透3 天前
谷歌封杀OpenClaw背后:本地部署或是出路
运维·服务器·数据库·安全
失重外太空啦3 天前
nginx
运维·nginx
Gofarlic_oms13 天前
避免Kisssoft高级分析模块过度采购的科学评估方法
大数据·linux·运维·人工智能·matlab
田井中律.3 天前
服务器部署问题汇总(ubuntu24.04.3)
运维·服务器
大大水瓶3 天前
HAProxy 从入门到实战:负载均衡与流量管理全解析
运维·负载均衡