// CPU負荷情報を取得.
//

#include "cpu.h"

#pragma comment( lib,"Pdh.Lib" )

static HQUERY hQuery;
static HCOUNTER hCounter;
static int initFlag = 0 ;

void cpu_init() {
    PdhOpenQuery( NULL, 0, &hQuery );
    PdhAddCounter( hQuery, "\\Processor(_Total)\\% Processor Time", 0, &hCounter );
    PdhCollectQueryData( hQuery );
    initFlag = 1 ;
}

// CPU負荷率を取得.
double useCpu() {
    PDH_FMT_COUNTERVALUE FmtValue ;
    double ret = 0.0f ;
    if( initFlag == 0 ) {
        cpu_init() ;
    }
    if( !kbhit() ) {
        PdhCollectQueryData( hQuery );
        PdhGetFormattedCounterValue( hCounter, PDH_FMT_DOUBLE, NULL, &FmtValue );
        ret = FmtValue.doubleValue ;
    }
    return ret ;
}

// プロセスIDを取得.
int getProcessId() {
    return GetCurrentProcessId() ;
}

