问:如何列出系统中安装的中央处理器(CPU)
答:
    以下方法是通过注册表来得到的。在processor这个kernel service的注册表项下有相关内容。代码如下:
/*
 *
 *  enum-proc.c - 列出系统中的中央处理器(CPU)
 *
 *  作者: xstring@csdn
 *  日期: 2004.04.16
 *
 *  说明: 
 *      如果你要得到每个CPU的更多硬件信息, 请自己查看程序中所涉及到
 *  的注册表位置。
 *
 */#define _WIN32_WINNT 0x400
#define WIN32_LEAN_AND_MEAN
#define STRICT#include <stdio.h>
#include <tchar.h>
#include <locale.h>
#include <windows.h>#pragma comment (lib, "advapi32.lib")
#pragma comment (lib, "user32.lib")void print_msg (LPCTSTR api, DWORD error)
    {
        LPTSTR str = NULL;
        FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 
            NULL, error, 0, (LPTSTR)&str, 0, 0);
        _tprintf (TEXT ("%s 调用失败,错误代码: %u\n %s\n"), 
            api, error, str);
        LocalFree (str);
    };
#define CHECK_ERROR(apiname)    \
    if (error != ERROR_SUCCESS) \
    {                           \
        api = TEXT (apiname);   \
        goto failed;            \
    }
int main (void)
    {
        HKEY    keyServ = NULL;
        HKEY    keyEnum = NULL;
        HKEY    key     = NULL;
        DWORD   error   = 0;
        LPCTSTR api     = NULL;
        DWORD   type, count, size, i;        setlocale (LC_ALL, ".ACP");        error = RegOpenKeyEx (HKEY_LOCAL_MACHINE, 
            TEXT ("SYSTEM\\CurrentControlSet\\Services\\Processor\\Enum"), 
            0, KEY_READ, &keyServ);        CHECK_ERROR ("RegOpenKeyEx");        error = RegOpenKeyEx (HKEY_LOCAL_MACHINE, 
            TEXT ("SYSTEM\\CurrentControlSet\\Enum"), 
            0, KEY_READ, &keyEnum);        CHECK_ERROR ("RegOpenKeyEx");        size = sizeof (count);
        error = RegQueryValueEx (keyServ, TEXT ("Count"), 
            0, &type, (LPBYTE)&count, &size);        CHECK_ERROR ("RegQueryValueEx");        _tprintf (TEXT ("您的系统上共有 %u 个中央处理器: \n\n"), count);        for (i=0;i<count;++i)
        {
            TCHAR sz [0x200];
            TCHAR name [0x40];
            wsprintf (name, TEXT ("%d"), i);
            size = sizeof (sz);
            error = RegQueryValueEx (keyServ, name, 
                0, &type, (LPBYTE)sz, &size);            CHECK_ERROR ("RegQueryValueEx");            error = RegOpenKeyEx (keyEnum, sz, 
                0, KEY_READ, &key);            CHECK_ERROR ("RegOpenKeyEx");            size = sizeof (sz);
            error = RegQueryValueEx (key, TEXT ("FriendlyName"), 
                0, &type, (LPBYTE)sz, &size);            CHECK_ERROR ("RegQueryValueEx");            _tprintf ("  CPU %d - %s\n", 
                i, sz);
            RegCloseKey (key);
            key = NULL;
        };        RegCloseKey (keyEnum);
        RegCloseKey (keyServ);
        return 0;failed:
        if (keyServ)
            RegCloseKey (keyServ);
        if (keyEnum)
            RegCloseKey (keyEnum);
        if (key)
            RegCloseKey (key);
        print_msg (api, error);
        return error;
    };在我的系统上运行结果如下:您的系统上共有 1 个中央处理器:  CPU 0 - Intel(R) Pentium(R) 4 CPU 1.80GHz

解决方案 »

  1.   

    还是这个准确!
    #include<iostream>
    using namespace std;unsigned int SteppingID;
    unsigned int Model;
    unsigned int Family;
    unsigned int ProcessorType;
    unsigned char Brand;
    unsigned long VersionInfo;
    unsigned long FeatureInfo;template<typename T>
    void Print(char* a,T ret){
      cout<<a<<":\t"<<ret<<endl;
    }void PrintBrand(char* str){
      cout<<"This processor is:"<<str<<endl;
    }void GetFeature(long i,char *str)
    {   
    _asm xor ebx,ebx
    _asm mov ebx,i
    _asm bt FeatureInfo,ebx //"bt" is opcode to test a particular bit(on/off) in a string
    //The bit number to test is provided as the second operand
    //The result is stored in the carry flag
    _asm jnc Feature_not_installed
    cout<<"CPU Feature: "<<str<<"\tO"<<endl;
    return;
    Feature_not_installed:
    cout<<"CPU Feature: "<<str<<"\tX"<<endl;
    }
    int main()
    {
    char *strFeatures[32]={
                                "FPU    ",// x87 FPU on Chip
        "VME    ",// Virtual 8086 Enhancement
        "DE     ",// Debugging Extensions
        "PSE    ",// Page Size Extensions
    "TSC    ",// Time Stamp Counter
    "MSR    ",// RDMSR and WRMSR Support
    "PAE    ",// Physical Address Extensions
    "MCE    ",// Machine Check Exception
    "CX8    ",// CMPXCHG8B instruction
        "APIC   ",// Advanced Programmable Interrupt Controller on Chip
    " ", //Reserved     10
    "SEP    ",// SYSENTER and SYSEXIT
    "MTRR   ",// Memory Type Range Register
    "PGE    ",// PTE Global Bit
        "MCA    ",// Machine Check Architecture
    "CMOV   ",// Condtional Move/Compare Instruction
    "PAT    ",// Page Attribute Table
    "PSE-32 ",// Page Size Extension
    "PSN    ",// Processor Serial Number
    "CLFSH  ",// CFLUSH Instruction
    " ", //Reserved   20
    "DS     ",// Debug Store
        "ACPI   ",// Thermal Monitor and Clock Control
    "MMX    ",// MMX Technology
    "FXSR   ",// FXSAVE/FXRSTOR
    "SSE    ",// SSE Extensions
    "SSE2   ",// SSE2 Extensions
    "SS     ",// Self Snoop
    "HTT    ",// Hyper Threading Tech
    "TM     ",//Thermal Monitor
    " ", //Reserved       30
    "PBE    ",// Pend. Brk. En
    };
        char* strProcessorType[]={
    "ProcessorType", "Original OEM Processor",
            "ProcessorType", "Intel OverDrive Processor",
            "ProcessorType", "Dual Processor",
    "ProcessorType", "Intel reserved"
    };
    char* strComputerBrand[]={
    "This Processor does not support the brand identification feature",
    "Intel Celeron",
    "Intel Pentium III",
    "Intel Pentium III Xeon or Intel Celeron",
    "Intel Pentium III",
        "Mobile Intel Pentium III-M",
    "Other Processor",
    "Mobile Intel Celeron",
    "Intel Pentium 4",
    "Intel Pentium 4",
    "Intel Celeron",
    "Intel Xeon or Xeon MP",
    "Intel Xeon MP",
    "Mobile Intel Pentium 4  or Intel Xeon",
    "Other Processor",
    "Other Processor",
    "Other Processor",
    "Other Processor",
    "Other Processor",
    "Other Processor",
    "Mobile Intel Celeron",
    "Mobile Intel Celeron",
    "Intel Pentium M"
    }; char VendorID[13];
    _asm
    {
    mov eax,0
    cpuid //CPUID Instruction
    mov dword ptr [VendorID],ebx
    mov dword ptr [VendorID+4],edx
    mov dword ptr [VendorID+8],ecx
    mov byte ptr [VendorID+12],0
    }
    //Show the Vendor ID. Should be "GenuineIntel" if the processor is Intel"/////////////
       cout<<"Processor Vendor: "<<VendorID<<endl;
    //Store all the required information in memory////////////////////////////////////////
    _asm
    {
    mov eax,1
    cpuid //CPUID Instruction
    mov VersionInfo,eax //Store Version Information
    mov FeatureInfo,edx //Store Feature Information
    mov Brand,bl //Store Brand Information
    }
    //Extract the SteppingID, model and family from the bit encoded VersionInfo and display
    SteppingID = VersionInfo & 0x0F; //Stepping ID
    Model = (VersionInfo & 0xF0)>>4; //Model
    Family = (VersionInfo & 0x0F00)>>8; //Family
    ProcessorType = (VersionInfo & 3000)>>12; //Processor Type    Print("SteppingID", SteppingID);
        Print("Model", Model);
    Print("Family", Family);
    //Show the Processor Type/////////////////////////////////////////////////////////////
    Print("The Processor Type is",strProcessorType[ProcessorType]);//Show the Computer Brand/////////////////////////////////////////////////////////////
    if(Brand<=22)
     Print("This processor is",strComputerBrand[Brand]);
    else
     Print("This processor is","Other Processor");
    /////////////////////////////////////////////////////////////////////////////////////
    //Get Processor features
    for(int i=0;i<32;i++){
           GetFeature(i,strFeatures[i]);
    }
    //////////////////////////////////////////////////////
    //ending
    return 0;
    }
      

  2.   

    当有系统中有多个不同型号的CPU时,无法用你这种方法判断
      

  3.   

    呵呵,请教楼主,能否得到K6的主频?其他的CPU好像都没有问题,就是K6的主频搞不定。