private static ProcessInfo[] GetProcessInfos(PerformanceCounterLib library, int processIndex, int threadIndex, byte[] data) { Hashtable hashtable = new Hashtable(); ArrayList list = new ArrayList(); GCHandle handle = new GCHandle(); try { handle = GCHandle.Alloc(data, GCHandleType.Pinned); IntPtr ptr = handle.AddrOfPinnedObject(); Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK structure = new Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK(); Marshal.PtrToStructure(ptr, structure); IntPtr ptr2 = (IntPtr) (((long) ptr) + structure.HeaderLength); Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION perf_instance_definition = new Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION(); Microsoft.Win32.NativeMethods.PERF_COUNTER_BLOCK perf_counter_block = new Microsoft.Win32.NativeMethods.PERF_COUNTER_BLOCK(); for (int j = 0; j < structure.NumObjectTypes; j++) { Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE perf_object_type = new Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE(); Marshal.PtrToStructure(ptr2, perf_object_type); IntPtr ptr3 = (IntPtr) (((long) ptr2) + perf_object_type.DefinitionLength); IntPtr ptr4 = (IntPtr) (((long) ptr2) + perf_object_type.HeaderLength); ArrayList list2 = new ArrayList(); for (int k = 0; k < perf_object_type.NumCounters; k++) { Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION perf_counter_definition = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION(); Marshal.PtrToStructure(ptr4, perf_counter_definition); string counterName = library.GetCounterName(perf_counter_definition.CounterNameTitleIndex); if (perf_object_type.ObjectNameTitleIndex == processIndex) { perf_counter_definition.CounterNameTitlePtr = (int) GetValueId(counterName); } else if (perf_object_type.ObjectNameTitleIndex == threadIndex) { perf_counter_definition.CounterNameTitlePtr = (int) GetValueId(counterName); } list2.Add(perf_counter_definition); ptr4 = (IntPtr) (((long) ptr4) + perf_counter_definition.ByteLength); } Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION[] perf_counter_definitionArray = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION[list2.Count]; list2.CopyTo(perf_counter_definitionArray, 0); for (int m = 0; m < perf_object_type.NumInstances; m++) { Marshal.PtrToStructure(ptr3, perf_instance_definition); IntPtr ptr5 = (IntPtr) (((long) ptr3) + perf_instance_definition.NameOffset); string strA = Marshal.PtrToStringUni(ptr5); if (!strA.Equals("_Total")) { IntPtr ptr6 = (IntPtr) (((long) ptr3) + perf_instance_definition.ByteLength); Marshal.PtrToStructure(ptr6, perf_counter_block); if (perf_object_type.ObjectNameTitleIndex == processIndex) { ProcessInfo info = GetProcessInfo(perf_object_type, (IntPtr) (((long) ptr3) + perf_instance_definition.ByteLength), perf_counter_definitionArray); if (((info.processId != 0) || (string.Compare(strA, "Idle", StringComparison.OrdinalIgnoreCase) == 0)) && (hashtable[info.processId] == null)) { string str3 = strA; if (str3.Length == 15) { if (strA.EndsWith(".", StringComparison.Ordinal)) { str3 = strA.Substring(0, 14); } else if (strA.EndsWith(".e", StringComparison.Ordinal)) { str3 = strA.Substring(0, 13); } else if (strA.EndsWith(".ex", StringComparison.Ordinal)) { str3 = strA.Substring(0, 12); } } info.processName = str3; hashtable.Add(info.processId, info); } } else if (perf_object_type.ObjectNameTitleIndex == threadIndex) { ThreadInfo info2 = GetThreadInfo(perf_object_type, (IntPtr) (((long) ptr3) + perf_instance_definition.ByteLength), perf_counter_definitionArray); if (info2.threadId != 0) { list.Add(info2); } } ptr3 = (IntPtr) ((((long) ptr3) + perf_instance_definition.ByteLength) + perf_counter_block.ByteLength); } } ptr2 = (IntPtr) (((long) ptr2) + perf_object_type.TotalByteLength); } } finally { if (handle.IsAllocated) { handle.Free(); } } for (int i = 0; i < list.Count; i++) { ThreadInfo info3 = (ThreadInfo) list[i]; ProcessInfo info4 = (ProcessInfo) hashtable[info3.processId]; if (info4 != null) { info4.threadInfoList.Add(info3); } } ProcessInfo[] array = new ProcessInfo[hashtable.Values.Count]; hashtable.Values.CopyTo(array, 0); return array; }
private static ProcessInfo[] GetProcessInfos(PerformanceCounterLib library, int processIndex, int threadIndex, byte[] data) { Hashtable hashtable = new Hashtable(); ArrayList list = new ArrayList(); GCHandle handle = new GCHandle(); try { handle = GCHandle.Alloc(data, GCHandleType.Pinned); IntPtr ptr = handle.AddrOfPinnedObject(); Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK structure = new Microsoft.Win32.NativeMethods.PERF_DATA_BLOCK(); Marshal.PtrToStructure(ptr, structure); IntPtr ptr2 = (IntPtr)(((long)ptr) + structure.HeaderLength); Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION perf_instance_definition = new Microsoft.Win32.NativeMethods.PERF_INSTANCE_DEFINITION(); Microsoft.Win32.NativeMethods.PERF_COUNTER_BLOCK perf_counter_block = new Microsoft.Win32.NativeMethods.PERF_COUNTER_BLOCK(); for (int j = 0; j < structure.NumObjectTypes; j++) { Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE perf_object_type = new Microsoft.Win32.NativeMethods.PERF_OBJECT_TYPE(); Marshal.PtrToStructure(ptr2, perf_object_type); IntPtr ptr3 = (IntPtr)(((long)ptr2) + perf_object_type.DefinitionLength); IntPtr ptr4 = (IntPtr)(((long)ptr2) + perf_object_type.HeaderLength); ArrayList list2 = new ArrayList(); for (int k = 0; k < perf_object_type.NumCounters; k++) { Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION perf_counter_definition = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION(); Marshal.PtrToStructure(ptr4, perf_counter_definition); string counterName = library.GetCounterName(perf_counter_definition.CounterNameTitleIndex); if (perf_object_type.ObjectNameTitleIndex == processIndex) { perf_counter_definition.CounterNameTitlePtr = (int)GetValueId(counterName); } else if (perf_object_type.ObjectNameTitleIndex == threadIndex) { perf_counter_definition.CounterNameTitlePtr = (int)GetValueId(counterName); } list2.Add(perf_counter_definition); ptr4 = (IntPtr)(((long)ptr4) + perf_counter_definition.ByteLength); } Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION[] perf_counter_definitionArray = new Microsoft.Win32.NativeMethods.PERF_COUNTER_DEFINITION[list2.Count]; list2.CopyTo(perf_counter_definitionArray, 0); for (int m = 0; m < perf_object_type.NumInstances; m++) { Marshal.PtrToStructure(ptr3, perf_instance_definition); IntPtr ptr5 = (IntPtr)(((long)ptr3) + perf_instance_definition.NameOffset); string strA = Marshal.PtrToStringUni(ptr5); if (!strA.Equals("_Total")) { IntPtr ptr6 = (IntPtr)(((long)ptr3) + perf_instance_definition.ByteLength); Marshal.PtrToStructure(ptr6, perf_counter_block); if (perf_object_type.ObjectNameTitleIndex == processIndex) { ProcessInfo info = GetProcessInfo(perf_object_type, (IntPtr)(((long)ptr3) + perf_instance_definition.ByteLength), perf_counter_definitionArray); if (((info.processId != 0) || (string.Compare(strA, "Idle", StringComparison.OrdinalIgnoreCase) == 0)) && (hashtable[info.processId] == null)) { string str3 = strA; if (str3.Length == 15) { if (strA.EndsWith(".", StringComparison.Ordinal)) { str3 = strA.Substring(0, 14); } else if (strA.EndsWith(".e", StringComparison.Ordinal)) { str3 = strA.Substring(0, 13); } else if (strA.EndsWith(".ex", StringComparison.Ordinal)) { str3 = strA.Substring(0, 12); } } info.processName = str3; hashtable.Add(info.processId, info); } } else if (perf_object_type.ObjectNameTitleIndex == threadIndex) { ThreadInfo info2 = GetThreadInfo(perf_object_type, (IntPtr)(((long)ptr3) + perf_instance_definition.ByteLength), perf_counter_definitionArray); if (info2.threadId != 0) { list.Add(info2); } } ptr3 = (IntPtr)((((long)ptr3) + perf_instance_definition.ByteLength) + perf_counter_block.ByteLength); } } ptr2 = (IntPtr)(((long)ptr2) + perf_object_type.TotalByteLength); } } finally { if (handle.IsAllocated) { handle.Free(); } } for (int i = 0; i < list.Count; i++) { ThreadInfo info3 = (ThreadInfo)list[i]; ProcessInfo info4 = (ProcessInfo)hashtable[info3.processId]; if (info4 != null) { info4.threadInfoList.Add(info3); } } ProcessInfo[] array = new ProcessInfo[hashtable.Values.Count]; hashtable.Values.CopyTo(array, 0); return(array); }