⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmprocessprobe.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
字号:
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
       $Archive: $

      $Revision: $
          $Date: $
        $Author: $

    Description: Implementation of the ProcProbe class

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision: $ : $Date: $";
/*****************************************************************************/

#pragma comment( lib, "pdh.lib" )

#include "../../../stdafx.h"
#include <pdh.h>
#include <pdhmsg.h>
#include <winperf.h>
#include "VMProcessProbe.h"



/*****************************************************************************/
/*
     FUNCTION NAME:  VMProcessProbe::IsProcessRunning

       DESCRIPTION:  

             INPUT:  roLookingFor 
                     roMsgsOut 
            OUTPUT:  

           RETURNS:  bool 
*/
bool VMProcessProbe::IsProcessRunning( VMString& roOnHost, VMString& roLookingFor, VMString roMsgsOut )
{   
  VMString oLower;
  VMString oUpper;
  VMString oRunning;
   
  roMsgsOut = _T("");

  if ( ScanAllProcesses( roOnHost, oRunning, roMsgsOut ) )
  {  
    oLower = oRunning;
    oLower.Lower();
    oUpper = oRunning;
    oUpper.Upper();

    if ( (-1) == oLower.Find( (const char*)roLookingFor ) && (-1) == oUpper.Find( (const char*)roLookingFor ) )
    {
      return( false );
    }
    else
    {
      return( true );
    }
  }
  else
  {
    return( false );
  }
}
/* End of function "VMProcessProbe::IsProcessRunning"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMProcessProbe::ScanAllProcesses

       DESCRIPTION:  

             INPUT:  VMString& roRunning 
                     VMString& roMsgOut 
            OUTPUT:  

           RETURNS:  bool  - 
*/
bool VMProcessProbe::ScanAllProcesses( VMString& roOnHost, VMString& roRunning, VMString& roMsgOut )
{
  roRunning = "";

  PDH_STATUS  pdhStatus = PdhScanProcs( roOnHost, roRunning );
  if ( ERROR_SUCCESS != pdhStatus )
  {
    switch ( pdhStatus )
    {
      case PDH_MORE_DATA:
      {
        roMsgOut = _T( "PDH_MORE_DATA" );
      }
      break; 

      case PDH_INSUFFICIENT_BUFFER:
      {
        roMsgOut = _T( "PDH_INSUFFICIENT_BUFFER" );
      }
      break; 

      case PDH_INVALID_ARGUMENT:
      {
        roMsgOut = _T( "PDH_INVALID_ARGUMENT" );
      }
      break; 

      case PDH_MEMORY_ALLOCATION_FAILURE:
      {
        roMsgOut = _T( "PDH_MEMORY_ALLOCATION_FAILURE" );
      }
      break; 

      case PDH_CSTATUS_NO_MACHINE:
      {
        roMsgOut = _T( "PDH_CSTATUS_NO_MACHINE" );
      }
      break; 

      case PDH_CSTATUS_NO_OBJECT:
      {
        roMsgOut = _T( "PDH_CSTATUS_NO_OBJECT" );
      }
      break; 
    }
    return( false );
  }
  else
  {
    return( true );
  }
}
/* End of function "VMProcessProbe::ScanAllProcesses"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMProcessProbe::PdhScanProcs

       DESCRIPTION:  

             INPUT:  roRunning 
            OUTPUT:  none

           RETURNS:  PDH_STATUS  
*/
PDH_STATUS VMProcessProbe::PdhScanProcs( VMString& roOnHost, VMString& roRunning )
{
  PDH_STATUS  pdhStatus               = ERROR_SUCCESS;
  TCHAR*      pchCounterListBuffer    = NULL;
  DWORD       dwCounterListSize       = 0;
  TCHAR*      pchInstanceListBuffer   = NULL;
  DWORD       dwInstanceListSize      = 0;
  LPTSTR      pchThisInstance         = NULL;
  char        chHostName[MAX_COMPUTERNAME_LENGTH + 1];
  unsigned long ulHostNameSize = MAX_COMPUTERNAME_LENGTH;

  // call the function to determine the required buffer size for the data
  //
  strcpy( chHostName, "\\\\" );
  strcat( chHostName, (const char*)roOnHost );
 
  pdhStatus = PdhEnumObjectItems ( NULL,                   // reserved
                                   chHostName,             // local machine
                                   TEXT("Process"),        // object to enumerate
                                   pchCounterListBuffer,   // pass in NULL buffers
                                   &dwCounterListSize,     // an 0 length to get
                                   pchInstanceListBuffer,  // required size 
                                   &dwInstanceListSize,    // of the buffers in chars
                                   PERF_DETAIL_WIZARD,     // counter detail level
                                   0 ); 

  if ( ERROR_SUCCESS == pdhStatus ) 
  {
    // allocate the buffers and try the call again
    //
    pchCounterListBuffer  = new TCHAR[ ( dwCounterListSize * sizeof( TCHAR ) ) ];
    pchInstanceListBuffer = new TCHAR[ ( dwInstanceListSize * sizeof( TCHAR ) ) ];

    if ( ( NULL != pchCounterListBuffer ) && ( NULL != pchInstanceListBuffer ) ) 
    {
      pdhStatus = PdhEnumObjectItems ( NULL,                   // reserved
                                       chHostName,             // local machine
                                       TEXT("Process"),        // object to enumerate
                                       pchCounterListBuffer,   // pass in NULL buffers
                                       &dwCounterListSize,     // an 0 length to get
                                       pchInstanceListBuffer,  // required size 
                                       &dwInstanceListSize,    // of the buffers in chars
                                       PERF_DETAIL_WIZARD,     // counter detail level
                                       0 );     
      if ( ERROR_SUCCESS == pdhStatus ) 
      {
        // walk the return instance list
        //
        for ( pchThisInstance = pchInstanceListBuffer;
             *pchThisInstance != 0;
              pchThisInstance += lstrlen( pchThisInstance ) + 1 ) 
        {
          roRunning +=  pchThisInstance;
          roRunning += "/";
        }
      }
      else
      {
        return( pdhStatus );
      }
    } 
    else 
    {
      return( FAILED_MALLOC );
    }
    if ( NULL != pchCounterListBuffer ) 
    {
      delete [] pchCounterListBuffer;
    }
    if ( NULL != pchInstanceListBuffer ) 
    {
      delete [] pchInstanceListBuffer;
    }
  } 
  else 
  {
    return( pdhStatus );
  }
  return( 0 );
}
/* End of function "VMProcessProbe::PdhScanProcs"
/*****************************************************************************/



/*****************************************************************************/
/* Check-in history */
/*
 *$Log: $
*/
/*****************************************************************************/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -