win10驱动下获取cpu信息

entry.c

#include "Driver.h"
NTSTATUS DriverEntry(
    IN PDRIVER_OBJECT pDriverObject,
    IN PUNICODE_STRING pRegistryPath)
{
    NTSTATUS status;
    //判断CPU类型
    CPUType();
    KdPrint(("Enter DriverEntry\n"));
    //DbgBreakPoint();
    //设置卸载函数
    pDriverObject->DriverUnload =DDKUnload;
    //设置派遣函数
    pDriverObject->MajorFunction[IRP_MJ_CREATE] = DDKDispatchRoutin;
    pDriverObject->MajorFunction[IRP_MJ_CLOSE] = DDKDispatchRoutin;
    pDriverObject->MajorFunction[IRP_MJ_WRITE] = DDKDispatchRoutin;
    pDriverObject->MajorFunction[IRP_MJ_READ] = DDKDispatchRoutin;
    pDriverObject->MajorFunction[IRP_MJ_CLEANUP] = DDKDispatchRoutin;
    pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DDKDispatchRoutin;
    pDriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] = DDKDispatchRoutin;
    pDriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = DDKDispatchRoutin;
    pDriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL] = DDKDispatchRoutin;

    //创建驱动设备对象
    status = CreateDevice(pDriverObject);

    KdPrint(("Leave DriverEntry\n"));
    return status;
}

NTSTATUS CreateDevice(
    IN PDRIVER_OBJECT   pDriverObject)
{
    NTSTATUS status;
    PDEVICE_OBJECT pDevObj;
    PDEVICE_EXTENSION pDevExt;

    //创建设备名称
    UNICODE_STRING devName;
    RtlInitUnicodeString(&devName, L"\\Device\\MyDDKDevice");

    //创建设备
    status = IoCreateDevice(pDriverObject,
        sizeof(DEVICE_EXTENSION),
        &devName,
        FILE_DEVICE_UNKNOWN,
        0, TRUE,
        &pDevObj);
    if (!NT_SUCCESS(status))
        return status;

    pDevObj->Flags |= DO_BUFFERED_IO;
    pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;
    pDevExt->pDevice = pDevObj;
    pDevExt->ustrDeviceName = devName;
    //创建符号链接
    UNICODE_STRING symLinkName;
    RtlInitUnicodeString(&symLinkName, L"\\??\\TESTDDK");
    pDevExt->ustrSymLinkName = symLinkName;
    status = IoCreateSymbolicLink(&symLinkName, &devName);
    if (!NT_SUCCESS(status))
    {
        IoDeleteDevice(pDevObj);
        return status;
    }
    return STATUS_SUCCESS;
}

#pragma PAGEDCODE
VOID DDKUnload(IN PDRIVER_OBJECT pDriverObject)
{
    PDEVICE_OBJECT  pNextObj;
    KdPrint(("Enter DriverUnload\n"));

    pNextObj = pDriverObject->DeviceObject;
    while (pNextObj != NULL)
    {
        PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
            pNextObj->DeviceExtension;

        //删除符号链接
        UNICODE_STRING pLinkName = pDevExt->ustrSymLinkName;
        IoDeleteSymbolicLink(&pLinkName);
        pNextObj = pNextObj->NextDevice;
        IoDeleteDevice(pDevExt->pDevice);
    }
}

#pragma PAGEDCODE
NTSTATUS DDKDispatchRoutin(IN PDEVICE_OBJECT pDevObj,
    IN PIRP pIrp)
{
    KdPrint(("Enter HelloDDKDispatchRoutin\n"));

    PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(pIrp);
    //建立一个字符串数组与IRP类型对应起来
    static char* irpname[] =
    {
        "IRP_MJ_CREATE",
        "IRP_MJ_CREATE_NAMED_PIPE",
        "IRP_MJ_CLOSE",
        "IRP_MJ_READ",
        "IRP_MJ_WRITE",
        "IRP_MJ_QUERY_INFORMATION",
        "IRP_MJ_SET_INFORMATION",
        "IRP_MJ_QUERY_EA",
        "IRP_MJ_SET_EA",
        "IRP_MJ_FLUSH_BUFFERS",
        "IRP_MJ_QUERY_VOLUME_INFORMATION",
        "IRP_MJ_SET_VOLUME_INFORMATION",
        "IRP_MJ_DIRECTORY_CONTROL",
        "IRP_MJ_FILE_SYSTEM_CONTROL",
        "IRP_MJ_DEVICE_CONTROL",
        "IRP_MJ_INTERNAL_DEVICE_CONTROL",
        "IRP_MJ_SHUTDOWN",
        "IRP_MJ_LOCK_CONTROL",
        "IRP_MJ_CLEANUP",
        "IRP_MJ_CREATE_MAILSLOT",
        "IRP_MJ_QUERY_SECURITY",
        "IRP_MJ_SET_SECURITY",
        "IRP_MJ_POWER",
        "IRP_MJ_SYSTEM_CONTROL",
        "IRP_MJ_DEVICE_CHANGE",
        "IRP_MJ_QUERY_QUOTA",
        "IRP_MJ_SET_QUOTA",
        "IRP_MJ_PNP",
    };

    UCHAR type = stack->MajorFunction;
    if (type >= arraysize(irpname))
        KdPrint((" - Unknown IRP, major type %X\n", type));
    else
        KdPrint(("\t%s\n", irpname[type]));

    //对一般IRP的简单操作,后面会介绍对IRP更复杂的操作
    NTSTATUS status = STATUS_SUCCESS;
    // 完成IRP
    pIrp->IoStatus.Status = status;
    pIrp->IoStatus.Information = 0; // bytes xfered
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);

    KdPrint(("Leave DDKDispatchRoutin\n"));

    return status;
}

stRet CPUType()
{
    stRet st_Ret = {FALSE,FALSE};
    ULONG CPUInfo[4] = { -1 };
    CHAR cpu_string[48];
    __cpuid(CPUInfo, 0);
    INT num_ids = CPUInfo[0];
    //GenuineIntel是Intel的CPU
    //0x47, 0x65, 0x6E, 0x75,   0x69, 0x6E, 0x65, 0x49,   0x6E, 0x74, 0x65, 0x6C,   0x00, 0x00, 0x00, 0x00 GenuineIntel
    if ((CPUInfo[1] == 0x756e6547) && (CPUInfo[2] == 0x6c65746e) && (CPUInfo[3] == 0x49656e69))
    {
        st_Ret.bIntelCPU=TRUE;
        KdPrint(("GenuineIntel\n"));
    }
    //AuthenticAMD 是AMD的CPU
    //0x41, 0x75, 0x74, 0x68,   0x65, 0x6E, 0x74, 0x69,   0x63, 0x41, 0x4D, 0x44,   0x00, 0x00, 0x00, 0x00  AuthenticAMD
    if ((CPUInfo[1] == 0x68747541) && (CPUInfo[2] == 0x444d4163) && (CPUInfo[3] == 0x69746e65))
    {
        st_Ret.bIntelCPU = TRUE;
        KdPrint(("AuthenticAMD\n"));
    }
    INT cpu_info[4] = { -1 };
    __cpuid(cpu_info, 0);
    // Interpret CPU feature information.
    if (num_ids > 0) {
        int cpu_info7[4] = { 0 };
        __cpuid(cpu_info, 1);
        if (num_ids >= 7) {
            __cpuid(cpu_info7, 7);
        }
        signature_ = cpu_info[0];
        stepping_ = cpu_info[0] & 0xf;
        model_ = ((cpu_info[0] >> 4) & 0xf) + ((cpu_info[0] >> 12) & 0xf0);
        family_ = (cpu_info[0] >> 8) & 0xf;
        type_ = (cpu_info[0] >> 12) & 0x3;
        ext_model_ = (cpu_info[0] >> 16) & 0xf;
        ext_family_ = (cpu_info[0] >> 20) & 0xff;
        has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
        has_sse_ = (cpu_info[3] & 0x02000000) != 0;
        has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
        has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
        has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
        has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
        has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
        has_aesni_ = (cpu_info[2] & 0x02000000) != 0;
        has_avx2_ = has_avx_ && (cpu_info7[1] & 0x00000020) != 0;
    }

    // Get the brand string of the cpu.
    __cpuid(cpu_info, 0x80000000);
    const int parameter_end = 0x80000004;
    int max_parameter = cpu_info[0];

    //CHAR temp[40] = { -1 };
    if (cpu_info[0] >= parameter_end) {
        PCHAR cpu_string_ptr = cpu_string;
        for (int parameter = 0x80000002; parameter <= parameter_end &&
            cpu_string_ptr < &cpu_string[lstrlen(cpu_string)]; parameter++) {
            __cpuid(cpu_info, parameter);
            lstrcpy(cpu_string_ptr, cpu_info, lstrlen(cpu_info));
            cpu_string_ptr += lstrlen(cpu_info);
        }
        //lstrcpy(g_cpu_string, cpu_string_ptr-34, 32);
        KdPrint(("cpu_string:%s cpu_string:%x\n", cpu_string, cpu_string));
        DbgBreakPoint();
    }

    const int parameter_containing_non_stop_time_stamp_counter = 0x80000007;
    if (max_parameter >= parameter_containing_non_stop_time_stamp_counter) {
        __cpuid(cpu_info, parameter_containing_non_stop_time_stamp_counter);
        has_non_stop_time_stamp_counter_ = (cpu_info[3] & (1 << 8)) != 0;
    }
    return st_Ret;
}
//  字符串长度
DWORD lstrlen(PCHAR pSrc)
{
    DWORD   dwRet = 0;

    while (*pSrc != 0)
    {
        dwRet++;
        pSrc++;
    }
    return dwRet;
}
//  字符拷贝
VOID lstrcpy(PCHAR pDst, PCHAR pSrc, DWORD len)
{
    //while (1)
    //{
    //  //  复制字符,直到零结尾
    //  *pDst = *pSrc;
    //  if (*pSrc == 0)
    //      break;

    //  //  下一个字符
    //  pSrc++;
    //  pDst++;
    //}
    for (DWORD i = 0; i < len; i++) {
            //  复制字符
            *pDst = *pSrc;
            if (*pSrc == 0)
                break;
            //  下一个字符
            pSrc++;
            pDst++;
    }
}

Driver.h

//#include <NTDDK.h>
#include <ntifs.h>
#include <string.h>
#include <ntstrsafe.h>
#define PAGEDCODE code_seg("PAGE")
#define LOCKEDCODE code_seg()
#define INITCODE code_seg("INIT")

#define PAGEDDATA data_seg("PAGE")
#define LOCKEDDATA data_seg()
#define INITDATA data_seg("INIT")

#define arraysize(p) (sizeof(p)/sizeof((p)[0]))

typedef struct _DEVICE_EXTENSION {
    PDEVICE_OBJECT pDevice;
    UNICODE_STRING ustrDeviceName;  //设备名称
    UNICODE_STRING ustrSymLinkName; //符号链接名
} DEVICE_EXTENSION, *PDEVICE_EXTENSION;
typedef struct MyStruct
{
    //Intel
    BOOLEAN bIntelCPU;
    //AMD
    BOOLEAN bAMDCPU;
}stRet;
INT signature_;  // raw form of type, family, model, and stepping
INT type_;  // process type
INT family_;  // family of the processor
INT model_;  // model of processor
INT stepping_;  // processor revision number
INT ext_model_;
INT ext_family_;
BOOLEAN has_mmx_;
BOOLEAN has_sse_;
BOOLEAN has_sse2_;
BOOLEAN has_sse3_;
BOOLEAN has_ssse3_;
BOOLEAN has_sse41_;
BOOLEAN has_sse42_;
BOOLEAN has_avx_;
BOOLEAN has_avx2_;
BOOLEAN has_aesni_;
BOOLEAN has_non_stop_time_stamp_counter_;
ULONG CPUInfo[4] = { -1 };
//CHAR g_cpu_string[0x36];
PCHAR cpu_string_ptr;
// 函数声明
stRet CPUType();
NTSTATUS CreateDevice(IN PDRIVER_OBJECT pDriverObject);
VOID DDKUnload(IN PDRIVER_OBJECT pDriverObject);
NTSTATUS DDKDispatchRoutin(IN PDEVICE_OBJECT pDevObj,
    IN PIRP pIrp);
//  字符串长度
DWORD lstrlen(PCHAR pSrc);
//  字符拷贝
VOID lstrcpy(PCHAR pDst, PCHAR pSrc, DWORD len);

原文地址:http://blog.51cto.com/haidragon/2317152

时间: 2024-10-09 00:32:53

win10驱动下获取cpu信息的相关文章

c++获取cpu信息

原文地址:http://blog.csdn.net/jamesliulyc/article/details/2028958 1.什么是cpuid指令 CPUID指令是intel IA32架构下获得CPU信息的汇编指令,可以得到CPU类型,型号,制造商信息,商标信息,序列号,缓存等一系列CPU相关的东西. 2.cpuid指令的使用 cpuid使用eax作为输入参数,eax,ebx,ecx,edx作为输出参数,举个例子: __asm {  mov eax, 1  cpuid  ... } 以上代码以

使用dmidecode在Linux下获取硬件信息

dmidecode命令可以让你在Linux系统下获取有关硬件方面的信息.dmidecode的作用是将DMI数据库中的信息解码,以可读的文本方式显示.由于DMI信息可以人为修改,因此里面的信息不一定是系统准确的信息.dmidecode遵循SMBIOS/DMI标准,其输出的信息包括BIOS.系统.主板.处理器.内存.缓存等等. DMI(Desktop Management Interface,DMI)就是帮助收集电脑系统信息的管理系统,DMI信息的收集必须在严格遵照SMBIOS规范的前提下进行.SM

linux下获取主机信息

程序源于linux程序设计这本书,个人利用这本书学习,本文作为一个知识点来学习. 这里首先介绍几个结构体: 1 2 3 4 5 6 7 struct hostent{ char* h_name;//主机名称 char* h_aliases;//主机别名 short h_addrtype;//主机地址类型 short h_length;//主机地址长度 char **h_addr_list;//主机地址列表 }   其中,主机地址类型h_addrtype为AF_INET;地址列表为字符串,长度不定

Linux下查看CPU信息

linux下查看CPU的信息主要通过查看/proc/cpuinfo这个文件. 一.针对/proc/cpuinfo文件,我们需要查看processor,physical id,siblings,core id,cpu cores这几个字段. 1.拥有相同physical id的所有逻辑处理器共享同一个物理插座.每个physical id 代表一个唯一的物理封装,即一颗CPU. 2.Siblings表示位于一个物理封装的CPU上逻辑CPU的个数. 3.每个core id 均代表一个唯一的处理器内核,

Linux下获取网络接口信息

Linux下的网络接口信息在shell下可以很方便地使用ifconfig查看.同样,使用C/C++也可以很方便地获取接口信息. netdevice是一个低级别的访问Linux网络设备的方法.此方法通过ioctl来获取网络接口的相关信息. 这里需要借助<net/if.h>头文件中定义的ifreq结构体.此结构体包含了网络接口的名称.IP地址.广播地址.网络地址. 掩码等相关信息.在获取上述相关信息的时候需要指明网络接口的名称. 1 struct ifreq { 2 char ifr_name[I

获取CPU信息

1 查看手机CPU信息 cmd——adb shell——cd /proc------cat cpuinfo 2 获取cpu的是arm指令集,armv7指令集.还是neon指令集 /** * * [获取cpu类型和架构] * * @return * 三个参数类型的数组,第一个参数标识是不是ARM架构,第二个参数标识是V6还是V7架构,第三个参数标识是不是neon指令集 */ public static Object[] getCpuArchitecture() { if ((Integer) mA

Qt 获取CPU信息

在Qt中调用Windows API GetSystemInfo可以获取CPU的相关信息,使用Qt的界面控件显示在界面上.在实现的过程中遇到了一个问题,就是显示地址信息在界面控件上. 试了好几种方法,都不能显示,最后想到了格式化函数sprintf,将地址转为char类型的数组或者指针,再转为字符串就可以正常显示了. 如果有人有更好的方法来显示地址,欢迎交流. 完整代码: #pragma execution_character_set("utf-8") #ifndef QCPUINFO_H

ubuntu下查看cpu信息

查看CPU信息cat /proc/cpuinfo 几个cpu more /proc/cpuinfo |grep "physical id"|uniq|wc -l 每个cpu是几核(假设cpu配置相同) more /proc/cpuinfo |grep "physical id"|grep "0"|wc -l cat /proc/cpuinfo | grep processor 1. 查看物理CPU的个数#cat /proc/cpuinfo |gr

Linux下查看CPU信息[/proc/cpuinfo]

最近在研究linux系统负载的时候,接触到一些关于CPU信息查看的知识,和大家分享一下.通过对/proc/cpuinfo文件中的参数的分析,也学到了不少东西. 在linux操作系统中,CPU的信息在启动的过程中被装载到虚拟目录/proc下的cpuinfo文件中,我们可以通过 cat /proc/cpuinfo 查看一下: 上图是本人电脑的CPU信息,下面我们来分析其中几个比较重要的指标: processor     逻辑处理器的id. physical id    物理封装的处理器的id. co