有地方说注册表可以,可我不知道是哪一个键啊?
还有一种判断进程执行时间的方法好像比较复杂,
有没有系统函数调用?

解决方案 »

  1.   

    xp下有个nt.dll ,load进来,调用他的NtQuerySystemInformation 函数,什么都有了,要是系统比较低级象98那种的没nt.dll的也许要用注册表了,这个不知道了,没用过
      

  2.   

    NtQuerySystemInformation ,自己可以到微软上看看
      

  3.   

    //cpu 的使用率
    //只在bcb里编译过,#include <windows.h>
    #include <conio.h>
    #include <stdio.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;
    // ntdll!NtQuerySystemInformation (NT specific!)
    //
    // The function copies the system information of the
    // specified type into a buffer
    //
    // NTSYSAPI
    // NTSTATUS
    // NTAPI
    // NtQuerySystemInformation(
    //    IN UINT SystemInformationClass,    // information type
    //    OUT PVOID SystemInformation,       // pointer to buffer
    //    IN ULONG SystemInformationLength,  // buffer size in bytes
    //    OUT PULONG ReturnLength OPTIONAL   // pointer to a 32-bit
    //                                       // variable that receives
    //                                       // the number of bytes
    //                                       // written to the buffer 
    // );
    typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);PROCNTQSI NtQuerySystemInformation;
    void main(void)
    {
        SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
        SYSTEM_TIME_INFORMATION        SysTimeInfo;
        SYSTEM_BASIC_INFORMATION       SysBaseInfo;
        double                         dbIdleTime;
        double                         dbSystemTime;
        LONG                           status;
        LARGE_INTEGER                  liOldIdleTime = {0,0};
        LARGE_INTEGER                  liOldSystemTime = {0,0};    NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(
                                              GetModuleHandle("ntdll"),
                                             "NtQuerySystemInformation"
                                             );    if (!NtQuerySystemInformation)
            return;    // get number of processors in the system
        status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
        if (status != NO_ERROR)
            return;
        
    printf("\nCPU Usage (press any key to exit):    ");
        while(!kbhit())
        {
            // get new system time
        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 (liOldIdleTime.QuadPart != 0)
           {
                // CurrentValue = NewValue - OldValue
                dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
                dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);            // CurrentCpuIdle = IdleTime / SystemTime
                dbIdleTime = dbIdleTime / dbSystemTime;            // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
                dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;            printf("\b\b\b\b%3d%%",(UINT)dbIdleTime);
           }        // store new CPU's idle and system time
            liOldIdleTime = SysPerfInfo.liIdleTime;
            liOldSystemTime = SysTimeInfo.liKeSystemTime;        // wait one second
            Sleep(1000);
        }
        printf("\n");
    }
      

  4.   

    获得CPU主频率:
    CPU主频率、厂家测试源程序(VC下嵌入汇编)CString CKillJSDlg::GetCPUFactoryName()
    {
    int nCPUName[ 20 ] = { 0 };
    _asm
    {
    mov eax, 0
    cpuid
    mov nCPUName[ 0 ], ebx
    mov nCPUName[ 4 ], edx
    mov nCPUName[ 8 ], ecx
    }
    return ( TCHAR* )nCPUName;
    }CString CKillJSDlg::GetCPUFrequence()
    {
    int nTime [ 2 ];
    int nCPUClock;
    _asm
    {
    rdtsc
    mov nTime[ 0 ], edx
    mov nTime[ 1 ], eax
    }
    Sleep( 1000 );
    _asm
    {
    rdtsc
    sub eax, nTime[ 1 ]
    sub edx, nTime[ 0 ]
    mov nCPUClock, eax
    }
    CString str;
    str.Format( "%dMHz", nCPUClock / 1000000 );
    return str;
    }
    获得cpu占用率:
    =====================================================参考一:
    // cpusagent.cpp (Windows NT/2000)
    //
    // Getting the CPU usage in percent on Windows NT/2000
    //
    // (c)2000 Ashot Oganesyan K, SmartLine, Inc
    // mailto:[email protected], http://www.protect-me.com, http://www.codepile.com#include <windows.h>
    #include <conio.h>
    #include <stdio.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;
    // ntdll!NtQuerySystemInformation (NT specific!)
    //
    // The function copies the system information of the
    // specified type into a buffer
    //
    // NTSYSAPI
    // NTSTATUS
    // NTAPI
    // NtQuerySystemInformation(
    // IN UINT SystemInformationClass, // information type
    // OUT PVOID SystemInformation, // pointer to buffer
    // IN ULONG SystemInformationLength, // buffer size in bytes
    // OUT PULONG ReturnLength OPTIONAL // pointer to a 32-bit
    // // variable that receives
    // // the number of bytes
    // // written to the buffer 
    // );
    typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);PROCNTQSI NtQuerySystemInformation;
    void main(void)
    {
    SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
    SYSTEM_TIME_INFORMATION SysTimeInfo;
    SYSTEM_BASIC_INFORMATION SysBaseInfo;
    double dbIdleTime;
    double dbSystemTime;
    LONG status;
    LARGE_INTEGER liOldIdleTime = {0,0};
    LARGE_INTEGER liOldSystemTime = {0,0};NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(
    GetModuleHandle("ntdll"),
    "NtQuerySystemInformation"
    );if (!NtQuerySystemInformation)
    return;// get number of processors in the system
    status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
    if (status != NO_ERROR)
    return;printf("\nCPU Usage (press any key to exit): ");
    while(!_kbhit())
    {
    // get new system time
    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 (liOldIdleTime.QuadPart != 0)
    {
    // CurrentValue = NewValue - OldValue
    dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
    dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);// CurrentCpuIdle = IdleTime / SystemTime
    dbIdleTime = dbIdleTime / dbSystemTime;// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
    dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;printf("\b\b\b\b%3d%%",(UINT)dbIdleTime);
    }// store new CPU's idle and system time
    liOldIdleTime = SysPerfInfo.liIdleTime;
    liOldSystemTime = SysTimeInfo.liKeSystemTime;// wait one second
    Sleep(1000);
    }
    printf("\n");
    }
    参考二下面这个类是从一个project中提出来的,原作者是Norm Almond 
    // CpuUsage.h: interface for the CCpuUsage class.
    //
    //////////////////////////////////////////////////////////////////////#if !defined(AFX_CPUUSAGE_H__60CF4F03_9F01_41E8_A9FB_51F065D5F3C2__INCLUDED_)
    #define AFX_CPUUSAGE_H__60CF4F03_9F01_41E8_A9FB_51F065D5F3C2__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000#include <pdh.h>
    #include <pdhmsg.h>#pragma comment(lib,"PDH.lib")
    #define MAX_RAW_VALUES 20const char szCounterName[] = "\\Processor(_Total)\\% Processor Time";typedef struct _tag_PDHCounterStruct {
    HCOUNTER hCounter; // Handle to the counter - given to use by PDH Library
    int nNextIndex; // element to get the next raw value
    int nOldestIndex; // element containing the oldes raw value
    int nRawCount; // number of elements containing raw values
    PDH_RAW_COUNTER a_RawValue[MAX_RAW_VALUES]; // Ring buffer to contain raw values
    } PDHCOUNTERSTRUCT, *PPDHCOUNTERSTRUCT;
    class CCpuUsage 
    {
    public:
    CCpuUsage();
    virtual ~CCpuUsage();
    BOOL Init();
    int GetUsage();protected:PPDHCOUNTERSTRUCT m_pCounterStruct;
    HQUERY m_hQuery;
    };#endif // !defined(AFX_CPUUSAGE_H__60CF4F03_9F01_41E8_A9FB_51F065D5F3C2__INCLUDED_)
    // CpuUsage.cpp: implementation of the CCpuUsage class.
    //
    //////////////////////////////////////////////////////////////////////#include "stdafx.h"
    #include "CpuUsage.h"#ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif//////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////CCpuUsage::CCpuUsage()
    {
    m_hQuery = NULL;
    m_pCounterStruct = NULL;}CCpuUsage::~CCpuUsage()
    {PdhCloseQuery(m_hQuery);
    delete m_pCounterStruct;
    }
    BOOL CCpuUsage::Init()
    {
    if (ERROR_SUCCESS != PdhOpenQuery(NULL, 1, &m_hQuery))
    return FALSE;m_pCounterStruct = (PPDHCOUNTERSTRUCT) new PDHCOUNTERSTRUCT;PDH_STATUS pdh_status = PdhAddCounter(m_hQuery, szCounterName, (DWORD) m_pCounterStruct, &(m_pCounterStruct->hCounter));
    if (ERROR_SUCCESS != pdh_status) 
    {
    return FALSE;
    }return TRUE;
    }
    int CCpuUsage::GetUsage()
    {
    PDH_FMT_COUNTERVALUE pdhFormattedValue;PdhCollectQueryData(m_hQuery);if (ERROR_SUCCESS != PdhGetFormattedCounterValue( 
    m_pCounterStruct->hCounter,
    PDH_FMT_LONG,
    NULL,
    &pdhFormattedValue )) 
    {
    return 0;
    }return pdhFormattedValue.longValue;
    }
      

  5.   

    http://www.codeguru.com/Cpp/W-P/system/performancestatistics/article.php/c2845/#more