幽冥大陆(五十五)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.

相关推荐
聆风吟º6 小时前
CANN开源项目深度实践:基于amct-toolkit实现自动化模型量化与精度保障策略
运维·开源·自动化·cann
较劲男子汉10 小时前
CANN Runtime零拷贝传输技术源码实战 彻底打通Host与Device的数据传输壁垒
运维·服务器·数据库·cann
风流倜傥唐伯虎10 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
Doro再努力10 小时前
【Linux操作系统10】Makefile深度解析:从依赖推导到有效编译
android·linux·运维·服务器·编辑器·vim
senijusene10 小时前
Linux软件编程:IO编程,标准IO(1)
linux·运维·服务器
忧郁的橙子.10 小时前
02-本地部署Ollama、Python
linux·运维·服务器
醇氧10 小时前
【linux】查看发行版信息
linux·运维·服务器
No8g攻城狮11 小时前
【Linux】Windows11 安装 WSL2 并运行 Ubuntu 22.04 详细操作步骤
linux·运维·ubuntu
做人不要太理性11 小时前
CANN Runtime 运行时与维测组件:异构任务调度、显存池管理与全链路异常诊断机制解析
人工智能·自动化
酷酷的崽79811 小时前
CANN 生态可维护性与可观测性:构建生产级边缘 AI 系统的运维体系
运维·人工智能