请问各位谁有获取系统CPU,内存等现成代码呢?发一个给我吧,我是初学者不知道怎样写.谢谢了.

解决方案 »

  1.   

    http://www.code365.com/programe/vc/xt/200512162102431280.htm
      

  2.   

    #include <windows.h>
    SYSTEM_INFO info;
    GetSystemInfo(&info);
    然后根据info结构里面的数据来判断。
    具体如下:
    SYSTEM_INFOThe SYSTEM_INFO structure contains information about the current computer system. This includes the architecture and type of the processor, the number of processors in the system, the page size, and other such information.
    typedef struct _SYSTEM_INFO {  union {    DWORD dwOemId;    struct {      WORD wProcessorArchitecture;      WORD wReserved;    };  };  DWORD dwPageSize;  LPVOID lpMinimumApplicationAddress;  LPVOID lpMaximumApplicationAddress;  DWORD_PTR dwActiveProcessorMask;  DWORD dwNumberOfProcessors;  DWORD dwProcessorType;  DWORD dwAllocationGranularity;  WORD wProcessorLevel;  WORD wProcessorRevision;
    } SYSTEM_INFO;
    Members
    dwOemId 
    An obsolete member that is retained for compatibility with Windows NT 3.5 and earlier. New applications should use the wProcessorArchitecture branch of the union. Windows Me/98/95:  The system always sets this member to zero, the value defined for PROCESSOR_ARCHITECTURE_INTEL.
    wProcessorArchitecture 
    System's processor architecture. This value can be one of the following values: 
    PROCESSOR_ARCHITECTURE_UNKNOWN
    PROCESSOR_ARCHITECTURE_INTELWindows NT 3.51:  PROCESSOR_ARCHITECTURE_MIPSWindows NT 4.0 and earlier:  PROCESSOR_ARCHITECTURE_ALPHAWindows NT 4.0 and earlier:  PROCESSOR_ARCHITECTURE_PPC64-bit Windows:  PROCESSOR_ARCHITECTURE_IA64, PROCESSOR_ARCHITECTURE_IA32_ON_WIN64, PROCESSOR_ARCHITECTURE_AMD64wReserved 
    Reserved for future use. 
    dwPageSize 
    Page size and the granularity of page protection and commitment. This is the page size used by the VirtualAlloc function. 
    lpMinimumApplicationAddress 
    Pointer to the lowest memory address accessible to applications and dynamic-link libraries (DLLs). 
    lpMaximumApplicationAddress 
    Pointer to the highest memory address accessible to applications and DLLs. 
    dwActiveProcessorMask 
    Mask representing the set of processors configured into the system. Bit 0 is processor 0; bit 31 is processor 31. 
    dwNumberOfProcessors 
    Number of processors in the system. 
    dwProcessorType 
    An obsolete member that is retained for compatibility with Windows NT 3.5 and earlier. Use the wProcessorArchitecture, wProcessorLevel, and wProcessorRevision members to determine the type of processor. Windows Me/98/95:  Specifies the type of processor in the system. This member is one of the following values:
    PROCESSOR_INTEL_386
    PROCESSOR_INTEL_486
    PROCESSOR_INTEL_PENTIUMdwAllocationGranularity 
    Granularity with which virtual memory is allocated. For example, a VirtualAlloc request to allocate 1 byte will reserve an address space of dwAllocationGranularity bytes. This value was hard coded as 64K in the past, but other hardware architectures may require different values. 
    wProcessorLevel 
    System's architecture-dependent processor level. It should be used only for display purposes. To determine the feature set of a processor, use the IsProcessorFeaturePresent function.
    If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_INTEL, wProcessorLevel is defined by the CPU vendor.If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_IA64, wProcessorLevel is set to 1.If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_MIPS, wProcessorLevel is of the form 00xx, where xx is an 8-bit implementation number (bits 8-15 of the PRId register). The member can be the following value.Value Meaning 
    0004 MIPS R4000 
    If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_ALPHA, wProcessorLevel is of the form xxxx, where xxxx is a 16-bit processor version number (the low-order 16 bits of a version number from the firmware). The member can be one of the following values.
    Value Meaning 
    21064 Alpha 21064 
    21066 Alpha 21066 
    21164 Alpha 21164 
    If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_PPC, wProcessorLevel is of the form xxxx, where xxxx is a 16-bit processor version number (the high-order 16 bits of the Processor Version Register). The member can be one of the following values.
    Value Meaning 
    1 PPC 601 
    3 PPC 603 
    4 PPC 604 
    6 PPC 603+ 
    9 PPC 604+ 
    20 PPC 620 wProcessorRevision 
    Architecture-dependent processor revision. The following table shows how the revision value is assembled for each type of processor architecture. Processor Meaning 
    Intel 80386 or 80486 A value of the form xxyz. 
    If xx is equal to 0xFF, y - 0xA is the model number, and z is the stepping identifier. For example, an Intel 80486-D0 system returns 0xFFD0.If xx is not equal to 0xFF, xx + 'A' is the stepping letter and yz is the minor stepping.
     
    Intel Pentium, Cyrix, or NextGen 586 A value of the form xxyy, where xx is the model number and yy is the stepping. Display this value of 0x0201 as follows: 
    Model xx, Stepping yy
     
    MIPS A value of the form 00xx, where xx is the 8-bit revision number of the processor (the low-order 8 bits of the PRId register). 
    ALPHA A value of the form xxyy, where xxyy is the low-order 16 bits of the processor revision number from the firmware. Display this value as follows: 
    Model A+xx, Pass yy
     
    PPC A value of the form xxyy, where xxyy is the low-order 16 bits of the processor version register. Display this value as follows: 
    xx.yy
     
      

  3.   

    这是我的部分代码:
    CString memory::GetMemUsges()
    {
    MEMORYSTATUS MemStat;
    MemStat.dwLength = sizeof(MEMORYSTATUS);
    GlobalMemoryStatus(&MemStat);
    m_ulNewUsges = MemStat.dwMemoryLoad;
    strUsges.Format(L"%u", m_ulNewUsges);
    strUsges += L"%";

    return strUsges;
    }
    void memory::SetRefreshInterval(int iMs)
    {
     m_iRefreshInterval = iMs;
     if(this->m_hWnd != NULL)
     {
    SetTimer(1, m_iRefreshInterval, NULL); 
     }
    }
    void memory::OnTimer(UINT_PTR nIDEvent)
    {
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    Invalidate();
    CWnd::OnTimer(nIDEvent);
    }
      

  4.   

    #include "StdAfx.h"
    #include "cpu.h"#define SystemBasicInformation       0
    #define SystemPerformanceInformation 2
    #define SystemTimeInformation        3#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))typedef struct
    {
        DWORD   dwUnknown1;
        ULONG   uKeMaximumIncrement;
        ULONG   uPageSize;
        ULONG   uMmNumberOfPhysicalPages;
        ULONG   uMmLowestPhysicalPage;
        ULONG   uMmHighestPhysicalPage;
        ULONG   uAllocationGranularity;
        PVOID   pLowestUserAddress;
        PVOID   pMmHighestUserAddress;
        ULONG   uKeActiveProcessors;
        BYTE    bKeNumberProcessors;
        BYTE    bUnknown2;
        WORD    wUnknown3;
    } SYSTEM_BASIC_INFORMATION;typedef struct
    {
        LARGE_INTEGER   liIdleTime;
        DWORD           dwSpare[76];
    } SYSTEM_PERFORMANCE_INFORMATION;typedef struct
    {
        LARGE_INTEGER liKeBootTime;
        LARGE_INTEGER liKeSystemTime;
        LARGE_INTEGER liExpTimeZoneBias;
        ULONG         uCurrentTimeZoneId;
        DWORD         dwReserved;
    } SYSTEM_TIME_INFORMATION;cpu::cpu(void)
    {
    }cpu::~cpu(void)
    {
    }
    void cpu::SetCpuUsgesNt()
    {
    SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
    SYSTEM_TIME_INFORMATION        SysTimeInfo;
    SYSTEM_BASIC_INFORMATION       SysBaseInfo;
    double                         dbIdleTime;
    double                         dbSystemTime;
    LONG                           status;
    typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);
    PROCNTQSI NtQuerySystemInformation; NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(GetModuleHandle(TEXT("ntdll")),"NtQuerySystemInformation");
    if (!NtQuerySystemInformation)
    {
    return;
    } // get number of processors in the system
    status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
    if (status != NO_ERROR)
    {
    return;
    } status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);
    if (status!=NO_ERROR)
    {
      return;
    } // get new CPU's idle time
    status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
    if (status != NO_ERROR)
    {
      return;
    } // if it's a first call - skip it
    if (m_liOldIdleTime.QuadPart != 0)
    {
    // CurrentValue = NewValue - OldValue
    dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(m_liOldIdleTime);
    dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(m_liOldSystemTime); // CurrentCpuIdle = IdleTime / SystemTime
    dbIdleTime = dbIdleTime / dbSystemTime; // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
    dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5; m_fNewUsges = (UINT)dbIdleTime;
    } // store new CPU's idle and system time
    m_liOldIdleTime = SysPerfInfo.liIdleTime;
    m_liOldSystemTime = SysTimeInfo.liKeSystemTime;
    strUsges.Format(L"%d", (int)m_fNewUsges);
    strUsges += L"%";
    }
    CString cpu::GetCpuUsges()
    {
    SetCpuUsgesNt();
    return strUsges;
    }