www.gusucode.com > VC++网络监视仪表模块源码程序 > VC++网络监视仪表模块源码程序\code\Thread.cpp

    //Download by http://www.NewXing.com
#include "stdafx.h"
#include <winperf.h>
#include <stdio.h>
#include <pdh.h>
#include <pdhmsg.h>

#include "AnalogMeterTestView.h"
#include "RegPerf.h"


BOOL bStop=TRUE;


//
//  仪表1监视线程  
//
void  Meter1_thread(LPVOID pParam)
{
    CAnalogMeterTestView * pView = (CAnalogMeterTestView *)pParam;
	CString name = pView->m_strComputer1;


	//
	// set current thread to HIGHEST PRIORITY
	//
	::SetThreadPriority(::GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
 
	//
    // PDH Query Handle for these counters
	//
    static HQUERY   hQuery = NULL;
    HCOUNTER hCounter;
    PDH_STATUS  pdhStatus;
    PDH_RAW_COUNTER RawValue ;// counter value 

    PDH_COUNTER_PATH_ELEMENTS  pdh_Path;

	name = "\\\\"+name;
    pdh_Path.szMachineName = name.GetBuffer(name.GetLength());
    pdh_Path.szObjectName = "Processor";
    pdh_Path.szInstanceName = "0";
    pdh_Path.szParentInstance = NULL;
    pdh_Path.dwInstanceIndex = 0;
    pdh_Path.szCounterName = "% Processor Time";



    CHAR  szCounterBuffer[MAX_PATH];

	DWORD  length = MAX_PATH;

    PdhMakeCounterPath ( &pdh_Path,
		                szCounterBuffer,
						&length,
						0);

    DWORD     dwType;
    PDH_FMT_COUNTERVALUE    pValue;
	int i,hello;


    if (hQuery == NULL) 
	{
        pdhStatus = PdhOpenQuery (NULL, 0, &hQuery);
    }

    //
    // try to add the counter to the query
	//
    pdhStatus = PdhAddCounter (hQuery,
		                       szCounterBuffer, 
							   0, 
							   &hCounter);
    if (pdhStatus != ERROR_SUCCESS) 
	{
   		printf("PdhAddCounter  Error!\n");

        switch(pdhStatus)
		{
			case PDH_CSTATUS_BAD_COUNTERNAME:
				break;
			case PDH_CSTATUS_NO_COUNTER :
				break;
			case PDH_CSTATUS_NO_COUNTERNAME :
				break;
			case PDH_CSTATUS_NO_MACHINE  :
				break;
			default:
				hello=1;
		}
		return ;
	}

    pdhStatus = PdhCollectQueryData (hQuery);
    if (pdhStatus != ERROR_SUCCESS) 
	{
        printf("PdhCollectQueryData Loop  Error! \n");
	    return;
	}

    Sleep(1000);

	// collect data 
	while(bStop)
	{
        // get the current values of the query data
        pdhStatus = PdhCollectQueryData (hQuery);
        if (pdhStatus != ERROR_SUCCESS) 
		{
            printf("PdhCollectQueryData Loop  Error! \n");
		    return;
		}

        pdhStatus = PdhGetFormattedCounterValue( hCounter, 
		                                         PDH_FMT_DOUBLE, 
											     &dwType, 
											     &pValue);
        if(pValue.doubleValue<0) 
			 pValue.doubleValue = (FLOAT)0.0f;

		pView->m_lastAngleDeg[0] = pValue.doubleValue;
        pView->m_bChanged[0] = TRUE;

		Sleep(1000);
	}    

	PdhCloseQuery(hQuery);
}

#define IsLocalComputer(a , LocalComputerName) (!lstrcmp(a,LocalComputerName))
#define IsRemoteComputer(a) (!IsLocalComputer(a))
#define szComputerPrefix         TEXT("\\\\") 

//
//  仪表2监视线程  
//
void  Meter2_thread(LPVOID pParam)
{
    CAnalogMeterTestView * pView = (CAnalogMeterTestView *)pParam;
	CString MachineName = pView->m_strComputer2;
//	MachineName = "\\\\"+MachineName;

	HKEY   hkResult; 
	HKEY   hKey;
	TCHAR  LocalComputerName[MAX_COMPUTERNAME_LENGTH + 3];
	DWORD  dwLength; 

 	::SetThreadPriority(::GetCurrentThread(),THREAD_PRIORITY_HIGHEST);

    dwLength = MAX_COMPUTERNAME_LENGTH ;
	::GetComputerName(LocalComputerName,&dwLength);


	LPCTSTR p = MachineName.GetBuffer(MachineName.GetLength());


	if(IsLocalComputer(LocalComputerName,p))
	{ // local computer
		hKey = HKEY_PERFORMANCE_DATA;
	}
	else
	{ // remote computer
        MachineName = "\\\\"+MachineName;

        LPTSTR p = MachineName.GetBuffer(MachineName.GetLength());
		LONG lRet =	::RegConnectRegistry (
                         p,
                         HKEY_PERFORMANCE_DATA,
                         &hKey);
	    if(lRet!=ERROR_SUCCESS)
		{
		    AfxMessageBox("Can not to registry...");
		    return;
		}
	}

    //
    // Get the index for the object.
	//

    char  szIndex[256] = "";
    XYGetIndexByName( "Processor", szIndex ,hKey);


    COMPUTE_DATA   PreCounter;
    COMPUTE_DATA   CurrentCounter;    
    FLOAT PerfData;

	//
	// collect first data
	//
    XYGetRawData(&PreCounter,szIndex,hKey);
    Sleep(1000);
    

    // collect data
	while(bStop)
	{
        XYGetRawData(&CurrentCounter,szIndex,hKey);
        PerfData=XYGetPerfData(PreCounter,CurrentCounter);
        PreCounter.llRawData = CurrentCounter.llRawData;
        PreCounter.llData100NS =CurrentCounter.llData100NS;

		pView->m_lastAngleDeg[1] = PerfData;
        pView->m_bChanged[1] = TRUE;

		Sleep(1000);
	}    

	// Close handle to the key.
    RegCloseKey( hKey );
}