Esempio n. 1
0
HRESULT CRefClient::AddObjects()
///////////////////////////////////////////////////////////////////
//
//	AddObject will add a set of objects to the refresher.  The 
//	method will update m_aInstances with the instance data. 
//
//	Returns a status code. Use the SUCCEEDED() and FAILED() macros
//	to interpret results.
//
///////////////////////////////////////////////////////////////////
//ok
{
	HRESULT hRes = WBEM_NO_ERROR;

	long	lIndex = 0;
	WCHAR	wcsObjName[MAX_PATH + 50];

// Loop through all instances of Win32_BasicHiPerf and add them to the refresher
// =============================================================================

	for ( lIndex = 0; lIndex < clNumInstances; lIndex++ )
	{
		IWbemClassObject*	pObj = NULL;
		IWbemObjectAccess*	pAccess = NULL;
		long lID;

		// Set the object path (e.g. Win32_BasicHiPerf=1)
		// ==============================================

		StringCbPrintfW(wcsObjName, sizeof(wcsObjName), L"%s=%i", cwcsObjectPath, lIndex );
	
		// Add the object
		// ==============

		hRes = m_pConfig->AddObjectByPath( m_pNameSpace, wcsObjName, 0, NULL, &pObj, &lID );

		if ( FAILED( hRes ) )
		{
			printf( "AddObjectByPath() failed, 0x%x\n", hRes );
			return hRes;
		}

		// Save the IWbemObjectAccess interface
		// ====================================
		
		hRes = pObj->QueryInterface( IID_IWbemObjectAccess, (void**) &pAccess );

		pObj->Release();

		m_Instances[lIndex].Set(pAccess, lID);

		// Set does it's own AddRef()
		// ==========================

		pAccess->Release();
	}
	
	return hRes;
}
Esempio n. 2
0
HRESULT CRefClient::SetHandles()
///////////////////////////////////////////////////////////////////
//
//	SetHandles will initialize the IWbemObjectAccess handle values 
//	in the counter array.
//
//	Returns a status code. Use the SUCCEEDED() and FAILED() macros
//	to interpret results.
//
///////////////////////////////////////////////////////////////////
//ok
{
	HRESULT hRes = WBEM_NO_ERROR;
	IWbemClassObject*	pObj = NULL;
	IWbemObjectAccess*	pAccess = NULL;
	long lIndex;

// Get an IWbemObjectAccess interface to one of the sample objects
// ===============================================================
	// Create a sample object 
	// ======================
	BSTR strObj = SysAllocString( cwcsObjectPath );
	if (strObj == NULL)
	{
		hRes = E_OUTOFMEMORY;
		goto cleanup;
	}

	hRes = m_pNameSpace->GetObject( strObj, 0, NULL, &pObj, NULL );

	SysFreeString( strObj );

	if ( FAILED( hRes ) ) 
		goto cleanup;

	// Get the alternate interface
	// ===========================

	hRes = pObj->QueryInterface( IID_IWbemObjectAccess, ( LPVOID* )&pAccess );

	if ( FAILED( hRes ) ) 
		goto cleanup;

// Set the access handles for all of the counter properties
// ========================================================

	for ( lIndex = Ctr1; lIndex < NumCtrs; lIndex++ )
	{	
		long lHandle;

		hRes = pAccess->GetPropertyHandle( g_aCounters[lIndex].m_wcsName, NULL, &lHandle );

		if ( FAILED( hRes ) )
			goto cleanup;

		g_aCounters[lIndex].m_lHandle = lHandle;
	}
// Cleanup the object pointers
// ===========================

cleanup:
	if ( NULL != pObj )
	{
		pObj->Release();
		pObj = NULL;
	}

	if ( NULL != pAccess )
	{
		pAccess->Release();
		pAccess = NULL;
	}

	if ( FAILED( hRes ) )
	{
		printf( "SetHandles() failed, 0x%x\n", hRes );
	}

	return hRes;
}
Esempio n. 3
0
DWORD
WINAPI
BMF_MonitorProcess (LPVOID user)
{
  BMF_InitCOM ();

  EnterCriticalSection (&com_cs);

  process_stats_t& proc   = process_stats;
  const double     update = config.mem.interval;

  HRESULT hr;

  if (FAILED (hr = CoCreateInstance (
                     CLSID_WbemRefresher,
                     NULL,
                     CLSCTX_INPROC_SERVER,
                     IID_IWbemRefresher, 
                     (void**) &proc.pRefresher )
             )
     )
  {
    dll_log.Log(L" [WMI]: Failed to create Refresher Instance (%s:%d) -- 0x%X",
      __FILEW__, __LINE__, hr);
    goto PROC_CLEANUP;
  }

  if (FAILED (hr = proc.pRefresher->QueryInterface (
                     IID_IWbemConfigureRefresher,
                     (void **)&proc.pConfig )
             )
     )
  {
    dll_log.Log(L" [WMI]: Failed to Query Refresher Interface (%s:%d) -- 0x%X",
      __FILEW__, __LINE__, hr);
    goto PROC_CLEANUP;
  }

  IWbemClassObject *pClassObj = nullptr;

  HANDLE hProc = GetCurrentProcess ();

  DWORD   dwProcessSize = MAX_PATH;
  wchar_t wszProcessName [MAX_PATH];

  QueryFullProcessImageName (hProc, 0, wszProcessName, &dwProcessSize);

  wchar_t* pwszShortName = wcsrchr (wszProcessName, L'\\') + 1;
  wchar_t* pwszTruncName = wcsrchr (pwszShortName, L'.');

  if (pwszTruncName != nullptr)
    *pwszTruncName = L'\0';

  wchar_t wszInstance [512];
  wsprintf ( wszInstance,
               L"Win32_PerfFormattedData_PerfProc_Process.Name='%ws'",
                 pwszShortName );

  if (FAILED (hr = proc.pConfig->AddObjectByPath (
                     pNameSpace,
                     wszInstance,
                     0,
                     0,
                     &pClassObj,
                     0 )
             )
     )
  {
    dll_log.Log(L" [WMI]: Failed to AddObjectByPath (%s:%d) -- 0x%X",
      __FILEW__, __LINE__, hr);
    goto PROC_CLEANUP;
  }

  if (FAILED (hr = pClassObj->QueryInterface ( IID_IWbemObjectAccess,
                                               (void **)(&proc.pAccess ) )
             )
     )
  {
    dll_log.Log(L" [WMI]: Failed to Query WbemObjectAccess Interface (%s:%d)"
                L" -- 0x%X",
      __FILEW__, __LINE__, hr);
    pClassObj->Release ();
    pClassObj = nullptr;

    goto PROC_CLEANUP;
  }

  pClassObj->Release ();
  pClassObj = nullptr;

  CIMTYPE variant;
  if (FAILED (hr = proc.pAccess->GetPropertyHandle ( L"PageFileBytes",
                                                     &variant,
                                                     &proc.hPageFileBytes )
             )
     )
  {
    goto PROC_CLEANUP;
  }

  if (FAILED (hr = proc.pAccess->GetPropertyHandle ( L"PageFileBytesPeak",
                                                     &variant,
                                                     &proc.hPageFileBytesPeak )
             )
     )
  {
    goto PROC_CLEANUP;
  }

  if (FAILED (hr = proc.pAccess->GetPropertyHandle ( L"ThreadCount",
                                                     &variant,
                                                     &proc.hThreadCount )
             )
     )
  {
    goto PROC_CLEANUP;
  }

  if (FAILED (hr = proc.pAccess->GetPropertyHandle ( L"PrivateBytes",
                                                     &variant,
                                                     &proc.hPrivateBytes )
             )
     )
  {
    goto PROC_CLEANUP;
  }

  if (FAILED (hr = proc.pAccess->GetPropertyHandle ( L"WorkingSetPeak",
                                                     &variant,
                                                     &proc.hWorkingSetPeak )
             )
     )
  {
    goto PROC_CLEANUP;
  }

  if (FAILED (hr = proc.pAccess->GetPropertyHandle ( L"WorkingSet",
                                                     &variant,
                                                     &proc.hWorkingSet )
             )
     )
  {
    goto PROC_CLEANUP;
  }

  if (FAILED (hr = proc.pAccess->GetPropertyHandle ( L"VirtualBytesPeak",
                                                     &variant,
                                                     &proc.hVirtualBytesPeak )
             )
     )
  {
    goto PROC_CLEANUP;
  }

  if (FAILED (hr = proc.pAccess->GetPropertyHandle ( L"VirtualBytes",
                                                     &variant,
                                                     &proc.hVirtualBytes )
             )
     )
  {
    goto PROC_CLEANUP;
  }

  proc.pConfig->Release ();
  proc.pConfig = nullptr;

  int iter = 0;

  proc.lID = 1;

  LeaveCriticalSection (&com_cs);

  while (proc.lID != 0)
  {
    // Sleep until ready
    Sleep (DWORD (update * 1000.0));

    // Only poll WMI while the data view is visible
    if (! config.mem.show)
      continue;

    EnterCriticalSection (&com_cs);

    if (FAILED (hr = proc.pRefresher->Refresh (0L)))
    {
      goto PROC_CLEANUP;
    }

    proc.pAccess->ReadQWORD ( proc.hVirtualBytes,
                                &proc.memory.virtual_bytes );
    proc.pAccess->ReadQWORD ( proc.hVirtualBytesPeak,
                                &proc.memory.virtual_bytes_peak );

    proc.pAccess->ReadQWORD ( proc.hWorkingSet,
                                &proc.memory.working_set );
    proc.pAccess->ReadQWORD ( proc.hWorkingSetPeak,
                                &proc.memory.working_set_peak );

    proc.pAccess->ReadQWORD ( proc.hPrivateBytes,
                                &proc.memory.private_bytes );

    proc.pAccess->ReadDWORD ( proc.hThreadCount,
                                (DWORD *)&proc.tasks.thread_count );

    proc.pAccess->ReadQWORD ( proc.hPageFileBytes,
                                &proc.memory.page_file_bytes );
    proc.pAccess->ReadQWORD ( proc.hPageFileBytesPeak,
                                &proc.memory.page_file_bytes_peak );

    ++iter;

    LeaveCriticalSection (&com_cs);
  }

  EnterCriticalSection (&com_cs);

PROC_CLEANUP:
  // dll_log.Log (L" >> PROC_CLEANUP");

  if (proc.pAccess != nullptr)
  {
    proc.pAccess->Release ();
    proc.pAccess = nullptr;
  }

  if (proc.pConfig != nullptr)
  {
    proc.pConfig->Release ();
    proc.pConfig = nullptr;
  }

  if (proc.pRefresher != nullptr)
  {
    proc.pRefresher->Release ();
    proc.pRefresher = nullptr;
  }

  CoUninitialize ();

  LeaveCriticalSection (&com_cs);

  return 0;
}