vector<double> GetCounterVector(PDH_HCOUNTER &hCounter) {
	vector<double> proc;
	PDH_STATUS status = ERROR_SUCCESS;
	DWORD dwBufferSize = 0;
	DWORD dwItemCount = 0;
	PDH_FMT_COUNTERVALUE_ITEM *pItems = NULL;
	status = PdhGetFormattedCounterArray(hCounter, PDH_FMT_DOUBLE,
			&dwBufferSize, &dwItemCount, pItems);
	if (PDH_MORE_DATA == status) {
		pItems = (PDH_FMT_COUNTERVALUE_ITEM *) malloc(dwBufferSize);
		if (pItems) {
			status = PdhGetFormattedCounterArray(hCounter, PDH_FMT_DOUBLE,
					&dwBufferSize, &dwItemCount, pItems);
			if (ERROR_SUCCESS == status) {

				for (DWORD i = 0; i < dwItemCount; i++) {
					proc.push_back(pItems[i].FmtValue.doubleValue);
				}
			} else {
				WriteFileLogf(
						L"PdhGetFormattedCounterArray2 failed with 0x%x \n",
						status);
			}

			free(pItems);
			pItems = NULL;
			dwBufferSize = dwItemCount = 0;
		} else {
			WriteFileLogf(L"PdhGetFormattedCounterArray2 failed with 0x%x.\n",
					status);
		}
	}

	else {
		WriteFileLogf(
				L"PdhGetFormattedCounterArray failed with 0x%x. counter: %x \n",
				status, &hCounter);
	}
	return proc;
}
double GetCounterTotal(PDH_HCOUNTER &hCounter) {
	double value = 0;
	PDH_STATUS status = ERROR_SUCCESS;
	DWORD dwBufferSize = 0;         // Size of the pItems buffer
	DWORD dwItemCount = 0;          // Number of items in the pItems buffer
	PDH_FMT_COUNTERVALUE_ITEM *pItems = NULL;
	status = PdhGetFormattedCounterArray(hCounter, PDH_FMT_DOUBLE,
			&dwBufferSize, &dwItemCount, pItems);
	if (PDH_MORE_DATA == status) {
		pItems = (PDH_FMT_COUNTERVALUE_ITEM *) malloc(dwBufferSize);
		if (pItems) {
			status = PdhGetFormattedCounterArray(hCounter, PDH_FMT_DOUBLE,
					&dwBufferSize, &dwItemCount, pItems);
			if (ERROR_SUCCESS == status) {

				for (DWORD i = 0; i < dwItemCount; i++) {
					value += pItems[i].FmtValue.doubleValue;
				}
			}

			free(pItems);
			pItems = NULL;
			dwBufferSize = dwItemCount = 0;
		} else {

			WriteFileLogf(L"PdhGetFormattedCounterArray2 failed with 0x%x.\n",
					status);
			throw status;
		}
	}

	else {
		WriteFileLogf(
				L"PdhGetFormattedCounterArray failed with 0x%x. counter: %x \n",
				status, &hCounter);
		throw status;
	}
	return value;
}
Beispiel #3
0
std::unique_ptr<char[]> PerformanceCounter::NextArray(DWORD format,
                                                      DWORD* count) {
  auto status = PdhCollectQueryData(query_);
  if (status != ERROR_SUCCESS)
    return nullptr;

  DWORD length = 0;
  status = PdhGetFormattedCounterArray(counter_, format | PDH_FMT_NOCAP100,
                                       &length, count, nullptr);
  if (status != PDH_MORE_DATA)
    return nullptr;

  auto buffer = std::make_unique<char[]>(length);
  if (buffer == nullptr)
    return nullptr;

  auto items = reinterpret_cast<PDH_FMT_COUNTERVALUE_ITEM*>(buffer.get());
  status = PdhGetFormattedCounterArray(counter_, format | PDH_FMT_NOCAP100,
                                       &length, count, items);
  if (status != ERROR_SUCCESS)
    return nullptr;

  return std::move(buffer);
}
Beispiel #4
0
BOOL QueryPerfData(printInfoStruct& pI)
{
	PDH_HQUERY hQuery = NULL;
	PDH_HCOUNTER hCounter = NULL;
	PDH_FMT_COUNTERVALUE_ITEM *pDisplayValues = NULL;
	DWORD dwBufferSize = 0, dwItemCount = 0;

	if (pI.wsFullPath.empty()) {
		std::wcout << "No performance counter path given!\n";
		return FALSE;
	}

	PDH_STATUS status = PdhOpenQuery(NULL, NULL, &hQuery);
	if (FAILED(status))
		goto die;

	status = PdhAddCounter(hQuery, pI.wsFullPath.c_str(), NULL, &hCounter);
	if (FAILED(status))
		goto die;

	status = PdhCollectQueryData(hQuery);
	if (FAILED(status))
		goto die;

	/* 
	/* Most counters need two queries to provide a value.
	/* Those which need only one will return the second.
	 */
	Sleep(pI.dwPerformanceWait);

	status = PdhCollectQueryData(hQuery);
	if (FAILED(status))
		goto die;

	status = PdhGetFormattedCounterArray(hCounter, pI.dwRequestedType,
										 &dwBufferSize, &dwItemCount, pDisplayValues);
	if (status != PDH_MORE_DATA)
		goto die;

	pDisplayValues = reinterpret_cast<PDH_FMT_COUNTERVALUE_ITEM*>(new BYTE[dwBufferSize]);
	status = PdhGetFormattedCounterArray(hCounter, pI.dwRequestedType,
										 &dwBufferSize, &dwItemCount, pDisplayValues);

	if (FAILED(status))
		goto die;

	switch (pI.dwRequestedType)
	{
	case (PDH_FMT_LONG):
		pI.dValue = pDisplayValues[0].FmtValue.longValue;
	case (PDH_FMT_LARGE) :
		pI.dValue = pDisplayValues[0].FmtValue.largeValue;
	default:
		pI.dValue = pDisplayValues[0].FmtValue.doubleValue;
	}

	delete[]pDisplayValues;

	return TRUE;

die:
	FormatPDHError(status);
	delete[]pDisplayValues;
	return FALSE;
}