Esempio n. 1
0
        /// <include file='doc\PerformanceCounterManager.uex' path='docs/doc[@for="PerformanceCounterManager.Initialize"]/*' />
        /// <devdoc>
        ///     Initializes perf com dll internal tables
        /// </devdoc>
        /// <internalonly/>
        private void Initialize()
        {
            if (FirstEntry)
            {
                lock (this) {
                    if (FirstEntry)
                    {
                        RegistryKey parentKey = null;
                        perfObjects = new Hashtable();
                        //SECREVIEW: This is being loaded by NT, it is executed
                        //                         when perfmon collects the data for all the
                        //                         registered counters.
                        RegistryPermission registryPermission = new RegistryPermission(PermissionState.Unrestricted);
                        registryPermission.Assert();
                        try {
                            parentKey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services");
                            if (parentKey == null)
                            {
                                return;
                            }

                            string[] categoryNames = parentKey.GetSubKeyNames();
                            for (int i = 0; i < categoryNames.Length; i++)
                            {
                                try {
                                    RegistryKey currentKey = parentKey.OpenSubKey(categoryNames[i] + "\\Performance");
                                    if (currentKey == null)
                                    {
                                        continue;
                                    }

                                    object systemDllName = currentKey.GetValue("Library");
                                    if (systemDllName == null || !(systemDllName is string) || String.Compare((string)systemDllName, PerformanceCounterLib.PerfShimName, true, CultureInfo.InvariantCulture) != 0)
                                    {
                                        continue;
                                    }

                                    object openEntryPoint = currentKey.GetValue("Open");
                                    if (openEntryPoint == null || !(openEntryPoint is string) || (string)openEntryPoint != PerformanceCounterLib.OpenEntryPoint)
                                    {
                                        continue;
                                    }

                                    object collectEntryPoint = currentKey.GetValue("Collect");
                                    if (collectEntryPoint == null || !(collectEntryPoint is string) || (string)collectEntryPoint != PerformanceCounterLib.CollectEntryPoint)
                                    {
                                        continue;
                                    }

                                    object closeEntryPoint = currentKey.GetValue("Close");
                                    if (closeEntryPoint == null || !(closeEntryPoint is string) || (string)closeEntryPoint != PerformanceCounterLib.CloseEntryPoint)
                                    {
                                        continue;
                                    }

                                    object counterDisabled = currentKey.GetValue("Disable Performance Counters");
                                    if (counterDisabled != null && (int)counterDisabled != 0)
                                    {
                                        continue;
                                    }

                                    object firstCounterId = currentKey.GetValue("First Counter");
                                    if (firstCounterId == null)
                                    {
                                        continue;
                                    }

                                    object lastCounterId = currentKey.GetValue("Last Counter");
                                    if (lastCounterId == null)
                                    {
                                        continue;
                                    }

                                    object firstHelpId = currentKey.GetValue("First Help");
                                    if (firstHelpId == null)
                                    {
                                        continue;
                                    }

                                    object countersData = currentKey.GetValue("Counter Types");
                                    int[]  counterTypes = null;
                                    if (countersData == null)
                                    {
                                        counterTypes = new int[0];
                                    }
                                    else
                                    {
                                        string[] counterStrs;
                                        if (countersData is string[])
                                        {
                                            counterStrs = (string[])countersData;
                                        }
                                        else if (countersData is byte[])
                                        {
                                            counterStrs = PerformanceCounterLib.GetStrings((byte[])countersData);
                                        }
                                        else
                                        {
                                            counterStrs = new string[0];
                                        }

                                        counterTypes = new int[counterStrs.Length];
                                        for (int index = 0; index < counterTypes.Length; index++)
                                        {
                                            counterTypes[index] = Int32.Parse(counterStrs[index]);
                                        }
                                    }

                                    countersData = currentKey.GetValue("Counter Names");
                                    int[]    counterNameHashCodes = null;
                                    string[] counterNames         = null;
                                    if (countersData == null)
                                    {
                                        counterNameHashCodes = new int[0];
                                        counterNames         = new string[0];
                                    }
                                    else
                                    {
                                        string[] counterStrs;
                                        if (countersData is string[])
                                        {
                                            counterStrs = (string[])countersData;
                                        }
                                        else if (countersData is byte[])
                                        {
                                            counterStrs = PerformanceCounterLib.GetStrings((byte[])countersData);
                                        }
                                        else
                                        {
                                            counterStrs = new string[0];
                                        }

                                        counterNameHashCodes = new int[counterStrs.Length];
                                        counterNames         = new string[counterStrs.Length];
                                        for (int index = 0; index < counterTypes.Length; index++)
                                        {
                                            counterNames[index]         = counterStrs[index].ToLower(CultureInfo.InvariantCulture);
                                            counterNameHashCodes[index] = counterNames[index].GetHashCode();
                                        }
                                    }

                                    if ((int)firstCounterId != -1 && (int)firstHelpId != -1)
                                    {
                                        ObjectData data = new ObjectData();
                                        data.CategoryName         = categoryNames[i].ToLower(CultureInfo.InvariantCulture);
                                        data.CategoryNameHashCode = data.CategoryName.GetHashCode();
                                        data.CounterTypes         = counterTypes;
                                        data.CounterNames         = counterNames;
                                        data.CounterNameHashCodes = counterNameHashCodes;
                                        data.FirstCounterId       = (int)firstCounterId;
                                        data.FirstHelpId          = (int)firstHelpId;
                                        perfObjects.Add((int)firstCounterId, data);
                                    }
                                }
                                catch (Exception) {
                                }
                            }
                        }
                        finally {
                            if (parentKey != null)
                            {
                                parentKey.Close();
                            }

                            RegistryPermission.RevertAssert();
                        }

                        FirstEntry = false;
                    }
                }
            }
        }