Esempio n. 1
0
        private long MakePath(PDH_COUNTER_PATH_ELEMENTS pathElts, out string outPath, bool bWildcardInstances)
        {
            outPath = "";
            IntPtr pcchBufferSize = new IntPtr(0);

            if (bWildcardInstances)
            {
                pathElts.InstanceIndex  = 0;
                pathElts.InstanceName   = "*";
                pathElts.ParentInstance = null;
            }
            long num = PdhMakeCounterPath(ref pathElts, IntPtr.Zero, ref pcchBufferSize, 0);

            if (num == 0x800007d2L)
            {
                IntPtr szFullPathBuffer = Marshal.AllocHGlobal((int)(pcchBufferSize.ToInt32() * 2));
                try
                {
                    num = PdhMakeCounterPath(ref pathElts, szFullPathBuffer, ref pcchBufferSize, 0);
                    if (num == 0)
                    {
                        outPath = Marshal.PtrToStringUni(szFullPathBuffer);
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(szFullPathBuffer);
                }
            }
            return(num);
        }
Esempio n. 2
0
        private long ParsePath(string fullPath, ref PDH_COUNTER_PATH_ELEMENTS pCounterPathElements)
        {
            IntPtr pdwBufferSize = new IntPtr(0);
            long   num           = PdhParseCounterPath(fullPath, IntPtr.Zero, ref pdwBufferSize, 0);

            switch (num)
            {
            case 0x800007d2L:
            case 0:
            {
                IntPtr ptr2 = Marshal.AllocHGlobal(pdwBufferSize.ToInt32());
                try
                {
                    num = PdhParseCounterPath(fullPath, ptr2, ref pdwBufferSize, 0);
                    if (num == 0)
                    {
                        pCounterPathElements = (PDH_COUNTER_PATH_ELEMENTS)Marshal.PtrToStructure(ptr2, typeof(PDH_COUNTER_PATH_ELEMENTS));
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(ptr2);
                }
                break;
            }
            }
            return(num);
        }
Esempio n. 3
0
        private bool IsPathValid(ref PDH_COUNTER_PATH_ELEMENTS pathElts, out string outPath)
        {
            bool flag = false;

            outPath = "";
            IntPtr pcchBufferSize = new IntPtr(0);

            if (PdhMakeCounterPath(ref pathElts, IntPtr.Zero, ref pcchBufferSize, 0) != 0x800007d2L)
            {
                return(false);
            }
            IntPtr szFullPathBuffer = Marshal.AllocHGlobal((int)(pcchBufferSize.ToInt32() * 2));

            try
            {
                if (PdhMakeCounterPath(ref pathElts, szFullPathBuffer, ref pcchBufferSize, 0) != 0)
                {
                    return(flag);
                }
                outPath = Marshal.PtrToStringUni(szFullPathBuffer);
                if (!this.m_isPreVista)
                {
                    return(PdhValidatePathEx(this.m_hDataSource, outPath) == 0);
                }
                flag = PdhValidatePath(outPath) == 0;
            }
            finally
            {
                Marshal.FreeHGlobal(szFullPathBuffer);
            }
            return(flag);
        }
Esempio n. 4
0
        public long GetValidPaths(string machineName, string objectName, ref StringCollection counters, ref StringCollection instances, ref StringCollection validPaths)
        {
            PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS {
                MachineName = machineName,
                ObjectName  = objectName
            };

            foreach (string str in counters)
            {
                pathElts.CounterName = str;
                if (instances.Count == 0)
                {
                    string str2;
                    if (this.IsPathValid(ref pathElts, out str2))
                    {
                        validPaths.Add(str2);
                    }
                }
                else
                {
                    foreach (string str3 in instances)
                    {
                        string str4;
                        pathElts.InstanceName  = str3;
                        pathElts.InstanceIndex = 0;
                        if (this.IsPathValid(ref pathElts, out str4))
                        {
                            validPaths.Add(str4);
                        }
                    }
                }
            }
            return(0);
        }
Esempio n. 5
0
        public long AddRelogCountersPreservingPaths(PerformanceCounterSampleSet sampleSet)
        {
            long num = 0;

            foreach (PerformanceCounterSample sample in sampleSet.CounterSamples)
            {
                PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS();
                num = this.ParsePath(sample.Path, ref pCounterPathElements);
                if (num == 0)
                {
                    IntPtr ptr;
                    num = PdhAddRelogCounter(this.m_hQuery, sample.Path, (long)sample.CounterType, sample.DefaultScale, sample.TimeBase, out ptr);
                    if (num == 0)
                    {
                        CounterHandleNInstance instance = new CounterHandleNInstance {
                            hCounter = ptr
                        };
                        if (pCounterPathElements.InstanceName != null)
                        {
                            instance.InstanceName = pCounterPathElements.InstanceName.ToLower(CultureInfo.InvariantCulture);
                        }
                        if (!this.m_ReloggerPathToHandleAndInstanceMap.ContainsKey(sample.Path.ToLower(CultureInfo.InvariantCulture)))
                        {
                            this.m_ReloggerPathToHandleAndInstanceMap.Add(sample.Path.ToLower(CultureInfo.InvariantCulture), instance);
                        }
                    }
                }
            }
            if (this.m_ReloggerPathToHandleAndInstanceMap.Keys.Count <= 0)
            {
                return(num);
            }
            return(0);
        }
Esempio n. 6
0
        private long MakeAllInstancePath(string origPath, out string unifiedPath)
        {
            unifiedPath = origPath;
            PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS();
            long num = this.ParsePath(origPath, ref pCounterPathElements);

            if (num != 0)
            {
                return(num);
            }
            return(this.MakePath(pCounterPathElements, out unifiedPath, true));
        }
Esempio n. 7
0
        public long AddCounters(ref StringCollection validPaths, bool bFlushOldCounters)
        {
            if (bFlushOldCounters)
            {
                this.m_ConsumerPathToHandleAndInstanceMap.Clear();
            }
            bool flag = false;
            long num  = 0;

            foreach (string str in validPaths)
            {
                IntPtr ptr;
                num = PdhAddCounter(this.m_hQuery, str, IntPtr.Zero, out ptr);
                if (num == 0)
                {
                    CounterHandleNInstance instance = new CounterHandleNInstance {
                        hCounter     = ptr,
                        InstanceName = null
                    };
                    PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS();
                    num = this.ParsePath(str, ref pCounterPathElements);
                    if ((num == 0) && (pCounterPathElements.InstanceName != null))
                    {
                        instance.InstanceName = pCounterPathElements.InstanceName.ToLower(CultureInfo.InvariantCulture);
                    }
                    if (!this.m_ConsumerPathToHandleAndInstanceMap.ContainsKey(str.ToLower(CultureInfo.InvariantCulture)))
                    {
                        this.m_ConsumerPathToHandleAndInstanceMap.Add(str.ToLower(CultureInfo.InvariantCulture), instance);
                    }
                    flag = true;
                }
            }
            if (!flag)
            {
                return(num);
            }
            return(0);
        }
Esempio n. 8
0
 public long AddCounters(ref StringCollection validPaths, bool bFlushOldCounters)
 {
     if (bFlushOldCounters)
     {
         this.m_ConsumerPathToHandleAndInstanceMap.Clear();
     }
     bool flag = false;
     long num = 0;
     foreach (string str in validPaths)
     {
         IntPtr ptr;
         num = PdhAddCounter(this.m_hQuery, str, IntPtr.Zero, out ptr);
         if (num == 0)
         {
             CounterHandleNInstance instance = new CounterHandleNInstance {
                 hCounter = ptr,
                 InstanceName = null
             };
             PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS();
             num = this.ParsePath(str, ref pCounterPathElements);
             if ((num == 0) && (pCounterPathElements.InstanceName != null))
             {
                 instance.InstanceName = pCounterPathElements.InstanceName.ToLower(CultureInfo.InvariantCulture);
             }
             if (!this.m_ConsumerPathToHandleAndInstanceMap.ContainsKey(str.ToLower(CultureInfo.InvariantCulture)))
             {
                 this.m_ConsumerPathToHandleAndInstanceMap.Add(str.ToLower(CultureInfo.InvariantCulture), instance);
             }
             flag = true;
         }
     }
     if (!flag)
     {
         return num;
     }
     return 0;
 }
Esempio n. 9
0
 public long AddRelogCountersPreservingPaths(PerformanceCounterSampleSet sampleSet)
 {
     long num = 0;
     foreach (PerformanceCounterSample sample in sampleSet.CounterSamples)
     {
         PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS();
         num = this.ParsePath(sample.Path, ref pCounterPathElements);
         if (num == 0)
         {
             IntPtr ptr;
             num = PdhAddRelogCounter(this.m_hQuery, sample.Path, (long) sample.CounterType, sample.DefaultScale, sample.TimeBase, out ptr);
             if (num == 0)
             {
                 CounterHandleNInstance instance = new CounterHandleNInstance {
                     hCounter = ptr
                 };
                 if (pCounterPathElements.InstanceName != null)
                 {
                     instance.InstanceName = pCounterPathElements.InstanceName.ToLower(CultureInfo.InvariantCulture);
                 }
                 if (!this.m_ReloggerPathToHandleAndInstanceMap.ContainsKey(sample.Path.ToLower(CultureInfo.InvariantCulture)))
                 {
                     this.m_ReloggerPathToHandleAndInstanceMap.Add(sample.Path.ToLower(CultureInfo.InvariantCulture), instance);
                 }
             }
         }
     }
     if (this.m_ReloggerPathToHandleAndInstanceMap.Keys.Count <= 0)
     {
         return num;
     }
     return 0;
 }
Esempio n. 10
0
        //
        // TranslateLocalCounterPath() helper translates counter paths from English into the current locale language.
        // NOTE: we can only translate counter set and counter names.
        // Translated instance names come from providers
        // This function will leave them unchanged: 
        // however, it works for common cases like "*" and "_total"
        // and many instance names are just numbers, anyway.
        //
        // Also - this only supports local paths, b/c connecting to remote registry 
        // requires a different firewall exception. 
        // This function checks and Asserts if the path is not valid.
        //
        public uint TranslateLocalCounterPath(string englishPath, out string localizedPath)
        {
            uint res = 0;
            localizedPath = "";
            PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS();
            res = ParsePath(englishPath, ref pathElts);
            if (res != 0)
            {
                return res;
            }

            // Check if the path is local and assert if not:
            string machineNameMassaged = pathElts.MachineName.ToLower(CultureInfo.InvariantCulture);
            machineNameMassaged = machineNameMassaged.TrimStart('\\');
            Debug.Assert(machineNameMassaged == System.Environment.MachineName.ToLower(CultureInfo.InvariantCulture));

            string lowerEngCtrName = pathElts.CounterName.ToLower(CultureInfo.InvariantCulture);
            string lowerEngObjectName = pathElts.ObjectName.ToLower(CultureInfo.InvariantCulture);

            // Get the registry index
            RegistryKey rootKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009");
            string[] regCounters = (string[])rootKey.GetValue("Counter");


            // NOTE: 1-based enumeration because the name strings follow index strings in the array           
            Int32 counterIndex = -1;
            Int32 objIndex = -1;
            for (uint enumIndex = 1; enumIndex < regCounters.Length; enumIndex++)
            {
                string regString = regCounters[enumIndex];
                if (regString.ToLower(CultureInfo.InvariantCulture) == lowerEngCtrName)
                {
                    try
                    {
                        counterIndex = Convert.ToInt32(regCounters[enumIndex - 1], CultureInfo.InvariantCulture);
                    }
                    catch (Exception)
                    {
                        return (uint)PdhResults.PDH_INVALID_PATH;
                    }
                }
                else if (regString.ToLower(CultureInfo.InvariantCulture) == lowerEngObjectName)
                {
                    try
                    {
                        objIndex = Convert.ToInt32(regCounters[enumIndex - 1], CultureInfo.InvariantCulture);
                    }
                    catch (Exception)
                    {
                        return (uint)PdhResults.PDH_INVALID_PATH;
                    }
                }
                if (counterIndex != -1 && objIndex != -1)
                {
                    break;
                }
            }

            if (counterIndex == -1 || objIndex == -1)
            {
                return (uint)PdhResults.PDH_INVALID_PATH;
            }


            // Now, call retrieve the localized names of the object and the counter by index:
            string objNameLocalized;
            res = LookupPerfNameByIndex(pathElts.MachineName, (uint)objIndex, out objNameLocalized);
            if (res != 0)
            {
                return res;
            }
            pathElts.ObjectName = objNameLocalized;


            string ctrNameLocalized;
            res = LookupPerfNameByIndex(pathElts.MachineName, (uint)counterIndex, out ctrNameLocalized);
            if (res != 0)
            {
                return res;
            }
            pathElts.CounterName = ctrNameLocalized;

            // Assemble the path back by using the translated object and counter names:

            res = MakePath(pathElts, out localizedPath, false);


            return res;
        }
Esempio n. 11
0
        private uint MakeAllInstancePath(string origPath, out string unifiedPath)
        {
            unifiedPath = origPath;

            PDH_COUNTER_PATH_ELEMENTS elts = new PDH_COUNTER_PATH_ELEMENTS();

            uint res = ParsePath(origPath, ref elts);
            if (res != 0)
            {
                return res;
            }

            return MakePath(elts, out unifiedPath, true);
        }
Esempio n. 12
0
        private uint ParsePath(string fullPath, ref PDH_COUNTER_PATH_ELEMENTS pCounterPathElements)
        {
            IntPtr bufSize = new IntPtr(0);

            uint res = PdhParseCounterPath(fullPath,
                                           IntPtr.Zero,
                                           ref bufSize,
                                           0);
            if (res != PdhResults.PDH_MORE_DATA && res != 0)
            {
                //Console.WriteLine("error in PdhParseCounterPath: " + res);
                return res;
            }

            IntPtr structPtr = Marshal.AllocHGlobal(bufSize.ToInt32());

            try
            {
                res = PdhParseCounterPath(fullPath,
                                          structPtr,
                                          ref bufSize,
                                          0);
                if (res == 0)
                {
                    //
                    // Marshal.PtrToStructure will allocate managed memory for the object, 
                    // so the unmanaged ptr can be freed safely
                    //
                    pCounterPathElements = (PDH_COUNTER_PATH_ELEMENTS)Marshal.PtrToStructure(structPtr, typeof(PDH_COUNTER_PATH_ELEMENTS));
                }
            }
            finally
            {
                Marshal.FreeHGlobal(structPtr);
            }

            return res;
        }
Esempio n. 13
0
        private bool IsPathValid(ref PDH_COUNTER_PATH_ELEMENTS pathElts, out string outPath)
        {
            bool ret = false;
            outPath = "";
            IntPtr pPathBufferSize = new IntPtr(0);

            uint res = PdhMakeCounterPath(ref pathElts, IntPtr.Zero, ref pPathBufferSize, 0);
            if (res != PdhResults.PDH_MORE_DATA)
            {
                return false;
            }

            Int32 cChars = pPathBufferSize.ToInt32();
            IntPtr strPath = Marshal.AllocHGlobal(cChars * sizeof(char));

            try
            {
                res = PdhMakeCounterPath(ref pathElts, strPath, ref pPathBufferSize, 0);
                if (res == 0)
                {
                    outPath = Marshal.PtrToStringUni(strPath);

                    if (!_isPreVista)
                    {
                        ret = (PdhValidatePathEx(_hDataSource, outPath) == 0);
                    }
                    else
                    {
                        ret = (PdhValidatePath(outPath) == 0);
                    }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(strPath);
            }

            return ret;
        }
Esempio n. 14
0
        private uint MakePath(PDH_COUNTER_PATH_ELEMENTS pathElts, out string outPath, bool bWildcardInstances)
        {
            outPath = "";
            IntPtr pPathBufferSize = new IntPtr(0);

            if (bWildcardInstances)
            {
                pathElts.InstanceIndex = 0;
                pathElts.InstanceName = "*";
                pathElts.ParentInstance = null;
            }

            uint res = PdhMakeCounterPath(ref pathElts, IntPtr.Zero, ref pPathBufferSize, 0);
            if (res != PdhResults.PDH_MORE_DATA)
            {
                return res;
            }

            Int32 cChars = pPathBufferSize.ToInt32();
            IntPtr strPath = Marshal.AllocHGlobal(cChars * sizeof(char));

            try
            {
                res = PdhMakeCounterPath(ref pathElts, strPath, ref pPathBufferSize, 0);
                if (res == 0)
                {
                    outPath = Marshal.PtrToStringUni(strPath);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(strPath);
            }

            return res;
        }
Esempio n. 15
0
 private static extern long PdhMakeCounterPath(ref PDH_COUNTER_PATH_ELEMENTS pCounterPathElements, IntPtr szFullPathBuffer, ref IntPtr pcchBufferSize, long dwFlags);
Esempio n. 16
0
        public long AddRelogCounters(PerformanceCounterSampleSet sampleSet)
        {
            long num = 0;
            Dictionary <string, List <PerformanceCounterSample> > dictionary = new Dictionary <string, List <PerformanceCounterSample> >();

            foreach (PerformanceCounterSample sample in sampleSet.CounterSamples)
            {
                PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS();
                num = this.ParsePath(sample.Path, ref pCounterPathElements);
                if (num == 0)
                {
                    List <PerformanceCounterSample> list;
                    string str  = pCounterPathElements.MachineName.ToLower(CultureInfo.InvariantCulture);
                    string str2 = pCounterPathElements.ObjectName.ToLower(CultureInfo.InvariantCulture);
                    string str3 = pCounterPathElements.CounterName.ToLower(CultureInfo.InvariantCulture);
                    string key  = @"\\" + str + @"\" + str2 + @"\" + str3;
                    if (dictionary.TryGetValue(key, out list))
                    {
                        dictionary[key].Add(sample);
                    }
                    else
                    {
                        List <PerformanceCounterSample> list2 = new List <PerformanceCounterSample> {
                            sample
                        };
                        dictionary.Add(key, list2);
                    }
                }
            }
            foreach (string str5 in dictionary.Keys)
            {
                IntPtr ptr;
                string path = dictionary[str5][0].Path;
                if (dictionary[str5].Count > 1)
                {
                    num = this.MakeAllInstancePath(dictionary[str5][0].Path, out path);
                    if (num != 0)
                    {
                        continue;
                    }
                }
                num = PdhAddRelogCounter(this.m_hQuery, path, (long)dictionary[str5][0].CounterType, dictionary[str5][0].DefaultScale, dictionary[str5][0].TimeBase, out ptr);
                if (num == 0)
                {
                    foreach (PerformanceCounterSample sample2 in dictionary[str5])
                    {
                        PDH_COUNTER_PATH_ELEMENTS pdh_counter_path_elements2 = new PDH_COUNTER_PATH_ELEMENTS();
                        num = this.ParsePath(sample2.Path, ref pdh_counter_path_elements2);
                        if (num == 0)
                        {
                            CounterHandleNInstance instance = new CounterHandleNInstance {
                                hCounter = ptr
                            };
                            if (pdh_counter_path_elements2.InstanceName != null)
                            {
                                instance.InstanceName = pdh_counter_path_elements2.InstanceName.ToLower(CultureInfo.InvariantCulture);
                            }
                            if (!this.m_ReloggerPathToHandleAndInstanceMap.ContainsKey(sample2.Path.ToLower(CultureInfo.InvariantCulture)))
                            {
                                this.m_ReloggerPathToHandleAndInstanceMap.Add(sample2.Path.ToLower(CultureInfo.InvariantCulture), instance);
                            }
                        }
                    }
                }
            }
            if (this.m_ReloggerPathToHandleAndInstanceMap.Keys.Count <= 0)
            {
                return(num);
            }
            return(0);
        }
Esempio n. 17
0
 private long ParsePath(string fullPath, ref PDH_COUNTER_PATH_ELEMENTS pCounterPathElements)
 {
     IntPtr pdwBufferSize = new IntPtr(0);
     long num = PdhParseCounterPath(fullPath, IntPtr.Zero, ref pdwBufferSize, 0);
     switch (num)
     {
         case 0x800007d2L:
         case 0:
         {
             IntPtr ptr2 = Marshal.AllocHGlobal(pdwBufferSize.ToInt32());
             try
             {
                 num = PdhParseCounterPath(fullPath, ptr2, ref pdwBufferSize, 0);
                 if (num == 0)
                 {
                     pCounterPathElements = (PDH_COUNTER_PATH_ELEMENTS) Marshal.PtrToStructure(ptr2, typeof(PDH_COUNTER_PATH_ELEMENTS));
                 }
             }
             finally
             {
                 Marshal.FreeHGlobal(ptr2);
             }
             break;
         }
     }
     return num;
 }
Esempio n. 18
0
 private long MakeAllInstancePath(string origPath, out string unifiedPath)
 {
     unifiedPath = origPath;
     PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS();
     long num = this.ParsePath(origPath, ref pCounterPathElements);
     if (num != 0)
     {
         return num;
     }
     return this.MakePath(pCounterPathElements, out unifiedPath, true);
 }
Esempio n. 19
0
 public long GetValidPaths(string machineName, string objectName, ref StringCollection counters, ref StringCollection instances, ref StringCollection validPaths)
 {
     PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS {
         MachineName = machineName,
         ObjectName = objectName
     };
     foreach (string str in counters)
     {
         pathElts.CounterName = str;
         if (instances.Count == 0)
         {
             string str2;
             if (this.IsPathValid(ref pathElts, out str2))
             {
                 validPaths.Add(str2);
             }
         }
         else
         {
             foreach (string str3 in instances)
             {
                 string str4;
                 pathElts.InstanceName = str3;
                 pathElts.InstanceIndex = 0;
                 if (this.IsPathValid(ref pathElts, out str4))
                 {
                     validPaths.Add(str4);
                 }
             }
         }
     }
     return 0;
 }
Esempio n. 20
0
        public long TranslateLocalCounterPath(string englishPath, out string localizedPath)
        {
            string str5;
            string str6;
            long   num = 0;

            localizedPath = "";
            PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS();

            num = this.ParsePath(englishPath, ref pCounterPathElements);
            if (num != 0)
            {
                return(num);
            }
            string str  = pCounterPathElements.MachineName.ToLower(CultureInfo.InvariantCulture).TrimStart(new char[] { '\\' });
            string str2 = pCounterPathElements.CounterName.ToLower(CultureInfo.InvariantCulture);
            string str3 = pCounterPathElements.ObjectName.ToLower(CultureInfo.InvariantCulture);

            string[] strArray = (string[])Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009").GetValue("Counter");
            int      num2     = -1;
            int      num3     = -1;

            for (long i = 1; i < strArray.Length; i++)
            {
                string str4 = strArray[i];
                if (str4.ToLower(CultureInfo.InvariantCulture) == str2)
                {
                    try
                    {
                        num2 = Convert.ToInt32(strArray[(int)((IntPtr)(i - 1))], CultureInfo.InvariantCulture);
                        goto Label_0114;
                    }
                    catch (Exception)
                    {
                        return(0xc0000bc4);
                    }
                }
                if (str4.ToLower(CultureInfo.InvariantCulture) == str3)
                {
                    try
                    {
                        num3 = Convert.ToInt32(strArray[(int)((IntPtr)(i - 1))], CultureInfo.InvariantCulture);
                    }
                    catch (Exception)
                    {
                        return(0xc0000bc4);
                    }
                }
Label_0114:
                if ((num2 != -1) && (num3 != -1))
                {
                    break;
                }
            }
            if ((num2 == -1) || (num3 == -1))
            {
                return(0xc0000bc4);
            }
            num = this.LookupPerfNameByIndex(pCounterPathElements.MachineName, (long)num3, out str5);
            if (num != 0)
            {
                return(num);
            }
            pCounterPathElements.ObjectName = str5;
            num = this.LookupPerfNameByIndex(pCounterPathElements.MachineName, (long)num2, out str6);
            if (num != 0)
            {
                return(num);
            }
            pCounterPathElements.CounterName = str6;
            return(this.MakePath(pCounterPathElements, out localizedPath, false));
        }
Esempio n. 21
0
 private static extern long PdhMakeCounterPath(ref PDH_COUNTER_PATH_ELEMENTS pCounterPathElements, IntPtr szFullPathBuffer, ref IntPtr pcchBufferSize, long dwFlags);
Esempio n. 22
0
 private bool IsPathValid(ref PDH_COUNTER_PATH_ELEMENTS pathElts, out string outPath)
 {
     bool flag = false;
     outPath = "";
     IntPtr pcchBufferSize = new IntPtr(0);
     if (PdhMakeCounterPath(ref pathElts, IntPtr.Zero, ref pcchBufferSize, 0) != 0x800007d2L)
     {
         return false;
     }
     IntPtr szFullPathBuffer = Marshal.AllocHGlobal((int) (pcchBufferSize.ToInt32() * 2));
     try
     {
         if (PdhMakeCounterPath(ref pathElts, szFullPathBuffer, ref pcchBufferSize, 0) != 0)
         {
             return flag;
         }
         outPath = Marshal.PtrToStringUni(szFullPathBuffer);
         if (!this.m_isPreVista)
         {
             return (PdhValidatePathEx(this.m_hDataSource, outPath) == 0);
         }
         flag = PdhValidatePath(outPath) == 0;
     }
     finally
     {
         Marshal.FreeHGlobal(szFullPathBuffer);
     }
     return flag;
 }
Esempio n. 23
0
        public uint GetValidPaths(string machineName,
                                   string objectName,
                                   ref StringCollection counters,
                                   ref StringCollection instances,
                                   ref StringCollection validPaths)
        {
            uint res = 0;

            PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS();
            pathElts.MachineName = machineName;
            pathElts.ObjectName = objectName;

            foreach (string counterName in counters)
            {
                pathElts.CounterName = counterName;

                if (instances.Count == 0)
                {
                    string pathCandidate;
                    if (IsPathValid(ref pathElts, out pathCandidate))
                    {
                        validPaths.Add(pathCandidate);
                    }
                }
                else
                {
                    foreach (string instanceName in instances)
                    {
                        pathElts.InstanceName = instanceName;
                        pathElts.InstanceIndex = 0;

                        string pathCandidate;
                        if (IsPathValid(ref pathElts, out pathCandidate))
                        {
                            validPaths.Add(pathCandidate);
                        }
                    }
                }
            }
            return res;
        }
Esempio n. 24
0
 private long MakePath(PDH_COUNTER_PATH_ELEMENTS pathElts, out string outPath, bool bWildcardInstances)
 {
     outPath = "";
     IntPtr pcchBufferSize = new IntPtr(0);
     if (bWildcardInstances)
     {
         pathElts.InstanceIndex = 0;
         pathElts.InstanceName = "*";
         pathElts.ParentInstance = null;
     }
     long num = PdhMakeCounterPath(ref pathElts, IntPtr.Zero, ref pcchBufferSize, 0);
     if (num == 0x800007d2L)
     {
         IntPtr szFullPathBuffer = Marshal.AllocHGlobal((int) (pcchBufferSize.ToInt32() * 2));
         try
         {
             num = PdhMakeCounterPath(ref pathElts, szFullPathBuffer, ref pcchBufferSize, 0);
             if (num == 0)
             {
                 outPath = Marshal.PtrToStringUni(szFullPathBuffer);
             }
         }
         finally
         {
             Marshal.FreeHGlobal(szFullPathBuffer);
         }
     }
     return num;
 }
Esempio n. 25
0
        public uint AddCounters(ref StringCollection validPaths, bool bFlushOldCounters)
        {
            Debug.Assert(_hQuery != null && !_hQuery.IsInvalid);

            if (bFlushOldCounters)
            {
                _consumerPathToHandleAndInstanceMap.Clear();
            }

            bool bAtLeastOneAdded = false;
            uint res = 0;

            foreach (string counterPath in validPaths)
            {
                IntPtr counterHandle;
                res = PdhAddCounter(_hQuery, counterPath, IntPtr.Zero, out counterHandle);
                if (res == 0)
                {
                    CounterHandleNInstance chi = new CounterHandleNInstance();
                    chi.hCounter = counterHandle;
                    chi.InstanceName = null;

                    PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS();
                    res = ParsePath(counterPath, ref pathElts);
                    if (res == 0 && pathElts.InstanceName != null)
                    {
                        chi.InstanceName = pathElts.InstanceName.ToLower(CultureInfo.InvariantCulture);
                    }

                    if (!_consumerPathToHandleAndInstanceMap.ContainsKey(counterPath.ToLower(CultureInfo.InvariantCulture)))
                    {
                        _consumerPathToHandleAndInstanceMap.Add(counterPath.ToLower(CultureInfo.InvariantCulture), chi);
                    }

                    bAtLeastOneAdded = true;
                }
            }

            return bAtLeastOneAdded ? 0 : res;
        }
Esempio n. 26
0
 public long AddRelogCounters(PerformanceCounterSampleSet sampleSet)
 {
     long num = 0;
     Dictionary<string, List<PerformanceCounterSample>> dictionary = new Dictionary<string, List<PerformanceCounterSample>>();
     foreach (PerformanceCounterSample sample in sampleSet.CounterSamples)
     {
         PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS();
         num = this.ParsePath(sample.Path, ref pCounterPathElements);
         if (num == 0)
         {
             List<PerformanceCounterSample> list;
             string str = pCounterPathElements.MachineName.ToLower(CultureInfo.InvariantCulture);
             string str2 = pCounterPathElements.ObjectName.ToLower(CultureInfo.InvariantCulture);
             string str3 = pCounterPathElements.CounterName.ToLower(CultureInfo.InvariantCulture);
             string key = @"\\" + str + @"\" + str2 + @"\" + str3;
             if (dictionary.TryGetValue(key, out list))
             {
                 dictionary[key].Add(sample);
             }
             else
             {
                 List<PerformanceCounterSample> list2 = new List<PerformanceCounterSample> {
                     sample
                 };
                 dictionary.Add(key, list2);
             }
         }
     }
     foreach (string str5 in dictionary.Keys)
     {
         IntPtr ptr;
         string path = dictionary[str5][0].Path;
         if (dictionary[str5].Count > 1)
         {
             num = this.MakeAllInstancePath(dictionary[str5][0].Path, out path);
             if (num != 0)
             {
                 continue;
             }
         }
         num = PdhAddRelogCounter(this.m_hQuery, path, (long) dictionary[str5][0].CounterType, dictionary[str5][0].DefaultScale, dictionary[str5][0].TimeBase, out ptr);
         if (num == 0)
         {
             foreach (PerformanceCounterSample sample2 in dictionary[str5])
             {
                 PDH_COUNTER_PATH_ELEMENTS pdh_counter_path_elements2 = new PDH_COUNTER_PATH_ELEMENTS();
                 num = this.ParsePath(sample2.Path, ref pdh_counter_path_elements2);
                 if (num == 0)
                 {
                     CounterHandleNInstance instance = new CounterHandleNInstance {
                         hCounter = ptr
                     };
                     if (pdh_counter_path_elements2.InstanceName != null)
                     {
                         instance.InstanceName = pdh_counter_path_elements2.InstanceName.ToLower(CultureInfo.InvariantCulture);
                     }
                     if (!this.m_ReloggerPathToHandleAndInstanceMap.ContainsKey(sample2.Path.ToLower(CultureInfo.InvariantCulture)))
                     {
                         this.m_ReloggerPathToHandleAndInstanceMap.Add(sample2.Path.ToLower(CultureInfo.InvariantCulture), instance);
                     }
                 }
             }
         }
     }
     if (this.m_ReloggerPathToHandleAndInstanceMap.Keys.Count <= 0)
     {
         return num;
     }
     return 0;
 }
Esempio n. 27
0
        //
        // AddRelogCounters combines instances and adds counters to m_hQuery.
        // The counter handles and full paths 
        //
        public uint AddRelogCounters(PerformanceCounterSampleSet sampleSet)
        {
            Debug.Assert(_hQuery != null && !_hQuery.IsInvalid);

            uint res = 0;

            Dictionary<string, List<PerformanceCounterSample>> prefixInstanceMap = new Dictionary<string, List<PerformanceCounterSample>>();

            // 
            // Go through all the samples one, constructing prefixInstanceMap and adding new counters as needed
            //            
            foreach (PerformanceCounterSample sample in sampleSet.CounterSamples)
            {
                PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS();
                res = ParsePath(sample.Path, ref pathElts);
                if (res != 0)
                {
                    // Skipping for now, but should be a non-terminating error
                    continue;
                }

                string lowerCaseMachine = pathElts.MachineName.ToLower(CultureInfo.InvariantCulture);
                string lowerCaseObject = pathElts.ObjectName.ToLower(CultureInfo.InvariantCulture);
                string lowerCaseCounter = pathElts.CounterName.ToLower(CultureInfo.InvariantCulture);

                string lcPathMinusInstance = @"\\" + lowerCaseMachine + @"\" + lowerCaseObject + @"\" + lowerCaseCounter;

                List<PerformanceCounterSample> sampleList;
                if (prefixInstanceMap.TryGetValue(lcPathMinusInstance, out sampleList))
                {
                    prefixInstanceMap[lcPathMinusInstance].Add(sample);
                }
                else
                {
                    List<PerformanceCounterSample> newList = new List<PerformanceCounterSample>();
                    newList.Add(sample);
                    prefixInstanceMap.Add(lcPathMinusInstance, newList);
                }

                //Console.WriteLine ("Added path " + sample.Path + " to the 1ist map with prefix " + lcPathMinusInstance);                
            }

            // 
            // Add counters to the query, consolidating multi-instance with a wildcard path,
            // and construct m_ReloggerPathToHandleAndInstanceMap where each full path would be pointing to its counter handle 
            // and an instance name (might be empty for no-instance counter types).
            // You can have multiple full paths inside m_ReloggerPathToHandleAndInstanceMap pointing to the same handle.
            //

            foreach (string prefix in prefixInstanceMap.Keys)
            {
                IntPtr counterHandle;
                string unifiedPath = prefixInstanceMap[prefix][0].Path;

                if (prefixInstanceMap[prefix].Count > 1)
                {
                    res = MakeAllInstancePath(prefixInstanceMap[prefix][0].Path, out unifiedPath);
                    if (res != 0)
                    {
                        // Skipping for now, but should be a non-terminating error
                        continue;
                    }
                }

                res = PdhAddRelogCounter(_hQuery,
                                         unifiedPath,
                                         (UInt32)prefixInstanceMap[prefix][0].CounterType,
                                         prefixInstanceMap[prefix][0].DefaultScale,
                                         prefixInstanceMap[prefix][0].TimeBase,
                                         out counterHandle);
                if (res != 0)
                {
                    // Skipping for now, but should be a non-terminating error
                    // Console.WriteLine ("PdhAddCounter returned " + res + " for counter path " + unifiedPath);
                    continue;
                }

                //Console.WriteLine ("added  pdh query path:" + unifiedPath );

                //now, add all actual paths to m_ReloggerPathToHandleAndInstanceMap 
                foreach (PerformanceCounterSample sample in prefixInstanceMap[prefix])
                {
                    PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS();
                    res = ParsePath(sample.Path, ref pathElts);
                    if (res != 0)
                    {
                        // Skipping for now, but should be a non-terminating error
                        continue;
                    }

                    CounterHandleNInstance chi = new CounterHandleNInstance();

                    chi.hCounter = counterHandle;

                    if (pathElts.InstanceName != null)
                    {
                        chi.InstanceName = pathElts.InstanceName.ToLower(CultureInfo.InvariantCulture);
                    }

                    if (!_reloggerPathToHandleAndInstanceMap.ContainsKey(sample.Path.ToLower(CultureInfo.InvariantCulture)))
                    {
                        _reloggerPathToHandleAndInstanceMap.Add(sample.Path.ToLower(CultureInfo.InvariantCulture), chi);
                        //Console.WriteLine ("added map path:" + sample.Path );
                    }
                }
            }

            //TODO: verify that all counters are in the map

            return (_reloggerPathToHandleAndInstanceMap.Keys.Count > 0) ? 0 : res;
        }
Esempio n. 28
0
 public long TranslateLocalCounterPath(string englishPath, out string localizedPath)
 {
     string str5;
     string str6;
     long num = 0;
     localizedPath = "";
     PDH_COUNTER_PATH_ELEMENTS pCounterPathElements = new PDH_COUNTER_PATH_ELEMENTS();
     num = this.ParsePath(englishPath, ref pCounterPathElements);
     if (num != 0)
     {
         return num;
     }
     string str = pCounterPathElements.MachineName.ToLower(CultureInfo.InvariantCulture).TrimStart(new char[] { '\\' });
     string str2 = pCounterPathElements.CounterName.ToLower(CultureInfo.InvariantCulture);
     string str3 = pCounterPathElements.ObjectName.ToLower(CultureInfo.InvariantCulture);
     string[] strArray = (string[]) Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009").GetValue("Counter");
     int num2 = -1;
     int num3 = -1;
     for (long i = 1; i < strArray.Length; i++)
     {
         string str4 = strArray[i];
         if (str4.ToLower(CultureInfo.InvariantCulture) == str2)
         {
             try
             {
                 num2 = Convert.ToInt32(strArray[(int) ((IntPtr) (i - 1))], CultureInfo.InvariantCulture);
                 goto Label_0114;
             }
             catch (Exception)
             {
                 return 0xc0000bc4;
             }
         }
         if (str4.ToLower(CultureInfo.InvariantCulture) == str3)
         {
             try
             {
                 num3 = Convert.ToInt32(strArray[(int) ((IntPtr) (i - 1))], CultureInfo.InvariantCulture);
             }
             catch (Exception)
             {
                 return 0xc0000bc4;
             }
         }
     Label_0114:
         if ((num2 != -1) && (num3 != -1))
         {
             break;
         }
     }
     if ((num2 == -1) || (num3 == -1))
     {
         return 0xc0000bc4;
     }
     num = this.LookupPerfNameByIndex(pCounterPathElements.MachineName, (long) num3, out str5);
     if (num != 0)
     {
         return num;
     }
     pCounterPathElements.ObjectName = str5;
     num = this.LookupPerfNameByIndex(pCounterPathElements.MachineName, (long) num2, out str6);
     if (num != 0)
     {
         return num;
     }
     pCounterPathElements.CounterName = str6;
     return this.MakePath(pCounterPathElements, out localizedPath, false);
 }
Esempio n. 29
0
        //
        // AddRelogCountersPreservingPaths preserves all paths and adds as relog counters to m_hQuery.
        // The counter handles and full paths are added to m_ReloggerPathToHandleAndInstanceMap
        //
        public uint AddRelogCountersPreservingPaths(PerformanceCounterSampleSet sampleSet)
        {
            Debug.Assert(_hQuery != null && !_hQuery.IsInvalid);

            uint res = 0;

            // 
            // Go through all the samples one, constructing prefixInstanceMap and adding new counters as needed
            //            
            foreach (PerformanceCounterSample sample in sampleSet.CounterSamples)
            {
                PDH_COUNTER_PATH_ELEMENTS pathElts = new PDH_COUNTER_PATH_ELEMENTS();
                res = ParsePath(sample.Path, ref pathElts);
                if (res != 0)
                {
                    // Skipping for now, but should be a non-terminating error
                    continue;
                }

                IntPtr counterHandle;
                res = PdhAddRelogCounter(_hQuery,
                                         sample.Path,
                                         (uint)sample.CounterType,
                                         sample.DefaultScale,
                                         sample.TimeBase,
                                         out counterHandle);
                if (res != 0)
                {
                    // Skipping for now, but should be a non-terminating error
                    continue;
                }

                CounterHandleNInstance chi = new CounterHandleNInstance();

                chi.hCounter = counterHandle;
                if (pathElts.InstanceName != null)
                {
                    chi.InstanceName = pathElts.InstanceName.ToLower(CultureInfo.InvariantCulture);
                }

                if (!_reloggerPathToHandleAndInstanceMap.ContainsKey(sample.Path.ToLower(CultureInfo.InvariantCulture)))
                {
                    _reloggerPathToHandleAndInstanceMap.Add(sample.Path.ToLower(CultureInfo.InvariantCulture), chi);
                }
            }

            return (_reloggerPathToHandleAndInstanceMap.Keys.Count > 0) ? 0 : res;
        }