본문 바로가기
C & C++/C & C++

[Etc] 특정 프로세스 cpu 점유율 체크

by izen8 2011. 1. 26.
반응형

특정 프로세스 cpu 점유율 체크

pdh.lib 추가해주시고, 테스트 해보시길 바랍니다.
프로세스 명을 IEXPLORE 대신 다른걸 바꿔보시면 됩니다.
원본 함께 첨부합니다.
//==================================================
// PDHCounters - Matt Pietrek 1998
// Microsoft Systems Journal, May 1998
// FILE: PDHCounters.CPP
//==================================================
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <conio.h>
#pragma hdrstop
#include "pdh.h"

//=================================== Global Variabes ======================
HQUERY g_hQuery = 0;
HCOUNTER g_hCounter = 0;
TCHAR g_szCounterPath[256];
//===================================== Code ===============================

int main()
{  
    // Get a query handle
    PdhOpenQuery( NULL, 0, &g_hQuery );

    // If there's already a counter in use, remove it from the query.
    if ( g_hCounter )
        PdhRemoveCounter( g_hCounter );

    // Add the counter whose path was obtained via PdhBrowseCounters    
    PdhAddCounter( g_hQuery, "\Process(IEXPLORE)\% Processor Time", 0, &g_hCounter );

    // Copy the counter path to a global variable copy of the string    
    lstrcpy( g_szCounterPath, "\Process(IEXPLORE)\% Processor Time" );
 
    while ( 1 )
    {
        PDH_FMT_COUNTERVALUE fmtValue;

        if ( g_hCounter )
        {
            // Go update the data in the counters              
            PdhCollectQueryData( g_hQuery );

            // Get the formatted version of the counter value          
            PdhGetFormattedCounterValue(g_hCounter,PDH_FMT_LONG,0, &fmtValue);

            // Print out the counter path, the formatted value, and the
            // raw value.  (Note the use of %I64u to print a 64 bit number)
            _tprintf( _T("%s %8u"), g_szCounterPath, fmtValue.longValue);
        }
        //// Sleep for 1 second                      
        Sleep( 1000 );
    }
    return 0;
}

반응형

댓글