private static string MakePath(PDH_COUNTER_PATH_ELEMENTS pathElements, bool isWildcard) { IntPtr sizeOfCounterPath = new IntPtr(0); if (isWildcard) { pathElements.InstanceIndex = 0; pathElements.InstanceName = "*"; pathElements.ParentInstance = null; } uint resultCode = Apis.PdhMakeCounterPath(ref pathElements, IntPtr.Zero, ref sizeOfCounterPath, PdhMakeCounterPathFlags.PDH_PATH_STANDARD_FORMAT); if (resultCode == PdhResults.PDH_MORE_DATA) { IntPtr fullPathBufferPointer = Marshal.AllocHGlobal(sizeOfCounterPath.ToInt32() * 2); try { resultCode = Apis.PdhMakeCounterPath(ref pathElements, fullPathBufferPointer, ref sizeOfCounterPath, PdhMakeCounterPathFlags.PDH_PATH_STANDARD_FORMAT); if (resultCode == PdhResults.PDH_CSTATUS_VALID_DATA) { return(Marshal.PtrToStringUni(fullPathBufferPointer)); } } finally { Marshal.FreeHGlobal(fullPathBufferPointer); } } throw new Exception(string.Format(CultureInfo.CurrentCulture, GetEventResources.CounterPathTranslationFailed, resultCode)); }
private void AddCounters(IEnumerable <string> validPaths) { uint resultCode = (uint)PdhResults.PDH_CSTATUS_VALID_DATA; foreach (string validPath in validPaths) { IntPtr counterPointer; resultCode = Apis.PdhAddCounter(this._safeQueryHandle, validPath, IntPtr.Zero, out counterPointer); if (resultCode == PdhResults.PDH_CSTATUS_VALID_DATA) { CounterHandleNInstance instance = new CounterHandleNInstance() { CounterHandle = counterPointer, InstanceName = null }; PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = PdhHelper.ParsePath(validPath); if (pCounterPathElements.InstanceName != null) { instance.InstanceName = pCounterPathElements.InstanceName.ToLower(CultureInfo.InvariantCulture); } if (!this._consumerPathToHandleAndInstanceMap.ContainsKey(validPath.ToLower(CultureInfo.InvariantCulture))) { this._consumerPathToHandleAndInstanceMap.Add(validPath.ToLower(CultureInfo.InvariantCulture), instance); } } } }
public static string TranslateLocalCounterPath(string englishPath) { PDH_COUNTER_PATH_ELEMENTS counterPathElements = PdhHelper.ParsePath(englishPath); string counterName = counterPathElements.CounterName.ToLower(CultureInfo.InvariantCulture), objectName = counterPathElements.ObjectName.ToLower(CultureInfo.InvariantCulture); string[] counterNames = (string[])Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009").GetValue("Counter"); int counterIndex = -1, objectIndex = -1; for (int i = 1; i < counterNames.Length; i++) { string currentCounter = counterNames[i]; if (currentCounter.ToLower(CultureInfo.InvariantCulture) == counterName) { counterIndex = Convert.ToInt32(counterNames[i - 1], CultureInfo.InvariantCulture); if ((counterIndex != -1) && (objectIndex != -1)) { break; } continue; } if (currentCounter.ToLower(CultureInfo.InvariantCulture) == objectName) { objectIndex = Convert.ToInt32(counterNames[i - 1], CultureInfo.InvariantCulture); } if ((counterIndex != -1) && (objectIndex != -1)) { break; } } counterPathElements.ObjectName = PdhHelper.LookupPerfNameByIndex(counterPathElements.MachineName, (uint)objectIndex); counterPathElements.CounterName = PdhHelper.LookupPerfNameByIndex(counterPathElements.MachineName, (uint)counterIndex); return(PdhHelper.MakePath(counterPathElements, false)); }
private static string MakePath(PDH_COUNTER_PATH_ELEMENTS pathElements, bool isWildcard) { IntPtr sizeOfCounterPath = new IntPtr(0); if (isWildcard) { pathElements.InstanceIndex = 0; pathElements.InstanceName = "*"; pathElements.ParentInstance = null; } uint resultCode = Apis.PdhMakeCounterPath(ref pathElements, IntPtr.Zero, ref sizeOfCounterPath, PdhMakeCounterPathFlags.PDH_PATH_STANDARD_FORMAT); if (resultCode == PdhResults.PDH_MORE_DATA) { IntPtr fullPathBufferPointer = Marshal.AllocHGlobal(sizeOfCounterPath.ToInt32() * 2); try { resultCode = Apis.PdhMakeCounterPath(ref pathElements, fullPathBufferPointer, ref sizeOfCounterPath, PdhMakeCounterPathFlags.PDH_PATH_STANDARD_FORMAT); if (resultCode == PdhResults.PDH_CSTATUS_VALID_DATA) { return Marshal.PtrToStringUni(fullPathBufferPointer); } } finally { Marshal.FreeHGlobal(fullPathBufferPointer); } } throw new Exception(string.Format(CultureInfo.CurrentCulture, GetEventResources.CounterPathTranslationFailed, resultCode)); }
public static extern uint PdhMakeCounterPath(ref PDH_COUNTER_PATH_ELEMENTS pCounterPathElements, IntPtr szFullPathBuffer, ref IntPtr pcchBufferSize, uint dwFlags);