Ejemplo n.º 1
0
        // What if an app is locked back?  Why would we use this?
        internal static string GetLatestBuildDllDirectory(string machineName)
        {
            string      dllDir     = "";
            RegistryKey baseKey    = null;
            RegistryKey complusReg = null;

            try
            {
                baseKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName);
                if (baseKey == null)
                {
                    throw new InvalidOperationException(SR.Format(SR.RegKeyMissingShort, "HKEY_LOCAL_MACHINE", machineName));
                }

                complusReg = baseKey.OpenSubKey("SOFTWARE\\Microsoft\\.NETFramework");
                if (complusReg != null)
                {
                    string installRoot = (string)complusReg.GetValue("InstallRoot");
                    if (installRoot != null && installRoot != string.Empty)
                    {
                        // the "policy" subkey contains a v{major}.{minor} subkey for each version installed.  There are also
                        // some extra subkeys like "standards" and "upgrades" we want to ignore.

                        // first we figure out what version we are...
                        string      versionPrefix = "v" + Environment.Version.Major + "." + Environment.Version.Minor;
                        RegistryKey policyKey     = complusReg.OpenSubKey("policy");

                        // This is the full version string of the install on the remote machine we want to use (for example "v2.0.50727")
                        string version = null;

                        if (policyKey != null)
                        {
                            try
                            {
                                // First check to see if there is a version of the runtime with the same minor and major number:
                                RegistryKey bestKey = policyKey.OpenSubKey(versionPrefix);

                                if (bestKey != null)
                                {
                                    try
                                    {
                                        version = versionPrefix + "." + GetLargestBuildNumberFromKey(bestKey);
                                    }
                                    finally
                                    {
                                        bestKey.Close();
                                    }
                                }
                                else
                                {
                                    // There isn't an exact match for our version, so we will look for the largest version
                                    // installed.
                                    string[] majorVersions  = policyKey.GetSubKeyNames();
                                    int[]    largestVersion = new int[] { -1, -1, -1 };
                                    for (int i = 0; i < majorVersions.Length; i++)
                                    {
                                        string majorVersion = majorVersions[i];

                                        // If this looks like a key of the form v{something}.{something}, we should see if it's a usable build.
                                        if (majorVersion.Length > 1 && majorVersion[0] == 'v' && majorVersion.Contains("."))
                                        {
                                            int[] currentVersion = new int[] { -1, -1, -1 };

                                            string[] splitVersion = majorVersion.Substring(1).Split('.');

                                            if (splitVersion.Length != 2)
                                            {
                                                continue;
                                            }

                                            if (!Int32.TryParse(splitVersion[0], out currentVersion[0]) || !Int32.TryParse(splitVersion[1], out currentVersion[1]))
                                            {
                                                continue;
                                            }

                                            RegistryKey k = policyKey.OpenSubKey(majorVersion);
                                            if (k == null)
                                            {
                                                // We may be able to use another subkey
                                                continue;
                                            }
                                            try
                                            {
                                                currentVersion[2] = GetLargestBuildNumberFromKey(k);

                                                if (currentVersion[0] > largestVersion[0] ||
                                                    ((currentVersion[0] == largestVersion[0]) && (currentVersion[1] > largestVersion[1])))
                                                {
                                                    largestVersion = currentVersion;
                                                }
                                            }
                                            finally
                                            {
                                                k.Close();
                                            }
                                        }
                                    }

                                    version = "v" + largestVersion[0] + "." + largestVersion[1] + "." + largestVersion[2];
                                }
                            }
                            finally
                            {
                                policyKey.Close();
                            }

                            if (version != null && version != string.Empty)
                            {
                                StringBuilder installBuilder = new StringBuilder();
                                installBuilder.Append(installRoot);
                                if (!installRoot.EndsWith("\\", StringComparison.Ordinal))
                                {
                                    installBuilder.Append("\\");
                                }
                                installBuilder.Append(version);
                                dllDir = installBuilder.ToString();
                            }
                        }
                    }
                }
            }
            catch
            {
                // ignore
            }
            finally
            {
                if (complusReg != null)
                {
                    complusReg.Close();
                }

                if (baseKey != null)
                {
                    baseKey.Close();
                }
            }

            return(dllDir);
        }
Ejemplo n.º 2
0
 private void ThrowReadOnly()
 {
     throw new InvalidOperationException(SR.Format(SR.ReadOnlyCounter));
 }
Ejemplo n.º 3
0
        /// <summary>
        ///     Intializes required resources
        /// </summary>
        private void InitializeImpl()
        {
            bool tookLock = false;

            try
            {
                Monitor.Enter(InstanceLockObject, ref tookLock);

                if (!_initialized)
                {
                    string currentCategoryName = _categoryName;
                    string currentMachineName  = _machineName;

                    if (currentCategoryName == string.Empty)
                    {
                        throw new InvalidOperationException(SR.Format(SR.CategoryNameMissing));
                    }
                    if (_counterName == string.Empty)
                    {
                        throw new InvalidOperationException(SR.Format(SR.CounterNameMissing));
                    }

                    if (ReadOnly)
                    {
                        if (!PerformanceCounterLib.CounterExists(currentMachineName, currentCategoryName, _counterName))
                        {
                            throw new InvalidOperationException(SR.Format(SR.CounterExists, currentCategoryName, _counterName));
                        }

                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.MultiInstanceOnly, currentCategoryName));
                            }
                        }
                        else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
                        {
                            if (!string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.SingleInstanceOnly, currentCategoryName));
                            }
                        }

                        if (_instanceLifetime != PerformanceCounterInstanceLifetime.Global)
                        {
                            throw new InvalidOperationException(SR.Format(SR.InstanceLifetimeProcessonReadOnly));
                        }

                        _initialized = true;
                    }
                    else
                    {
                        if (currentMachineName != "." && !string.Equals(currentMachineName, PerformanceCounterLib.ComputerName, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new InvalidOperationException(SR.Format(SR.RemoteWriting));
                        }

                        if (!PerformanceCounterLib.IsCustomCategory(currentMachineName, currentCategoryName))
                        {
                            throw new InvalidOperationException(SR.Format(SR.NotCustomCounter));
                        }

                        // check category type
                        PerformanceCounterCategoryType categoryType = PerformanceCounterLib.GetCategoryType(currentMachineName, currentCategoryName);
                        if (categoryType == PerformanceCounterCategoryType.MultiInstance)
                        {
                            if (string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.MultiInstanceOnly, currentCategoryName));
                            }
                        }
                        else if (categoryType == PerformanceCounterCategoryType.SingleInstance)
                        {
                            if (!string.IsNullOrEmpty(_instanceName))
                            {
                                throw new InvalidOperationException(SR.Format(SR.SingleInstanceOnly, currentCategoryName));
                            }
                        }

                        if (string.IsNullOrEmpty(_instanceName) && InstanceLifetime == PerformanceCounterInstanceLifetime.Process)
                        {
                            throw new InvalidOperationException(SR.Format(SR.InstanceLifetimeProcessforSingleInstance));
                        }

                        _sharedCounter = new SharedPerformanceCounter(currentCategoryName.ToLower(CultureInfo.InvariantCulture), _counterName.ToLower(CultureInfo.InvariantCulture), _instanceName.ToLower(CultureInfo.InvariantCulture), _instanceLifetime);
                        _initialized   = true;
                    }
                }
            }
            finally
            {
                if (tookLock)
                {
                    Monitor.Exit(InstanceLockObject);
                }
            }
        }
Ejemplo n.º 4
0
        internal static void CheckValidCounterLayout(CounterCreationDataCollection counterData)
        {
            // Ensure that there are no duplicate counter names being created
            Hashtable h = new Hashtable();

            for (int i = 0; i < counterData.Count; i++)
            {
                if (counterData[i].CounterName == null || counterData[i].CounterName.Length == 0)
                {
                    throw new ArgumentException(SR.InvalidCounterName);
                }

                int currentSampleType = (int)counterData[i].CounterType;
                if ((currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_BULK) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER_INV) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER_INV) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_RAW_FRACTION) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_SAMPLE_FRACTION) ||
                    (currentSampleType == Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_TIMER))
                {
                    if (counterData.Count <= (i + 1))
                    {
                        throw new InvalidOperationException(SR.CounterLayout);
                    }
                    else
                    {
                        currentSampleType = (int)counterData[i + 1].CounterType;


                        if (!PerformanceCounterLib.IsBaseCounter(currentSampleType))
                        {
                            throw new InvalidOperationException(SR.CounterLayout);
                        }
                    }
                }
                else if (PerformanceCounterLib.IsBaseCounter(currentSampleType))
                {
                    if (i == 0)
                    {
                        throw new InvalidOperationException(SR.CounterLayout);
                    }
                    else
                    {
                        currentSampleType = (int)counterData[i - 1].CounterType;

                        if (
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_BULK) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_100NSEC_MULTI_TIMER_INV) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_COUNTER_MULTI_TIMER_INV) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_RAW_FRACTION) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_SAMPLE_FRACTION) &&
                            (currentSampleType != Interop.Kernel32.PerformanceCounterOptions.PERF_AVERAGE_TIMER))
                        {
                            throw new InvalidOperationException(SR.CounterLayout);
                        }
                    }
                }

                if (h.ContainsKey(counterData[i].CounterName))
                {
                    throw new ArgumentException(SR.Format(SR.DuplicateCounterName, counterData[i].CounterName));
                }
                else
                {
                    h.Add(counterData[i].CounterName, string.Empty);

                    // Ensure that all counter help strings aren't null or empty
                    if (counterData[i].CounterHelp == null || counterData[i].CounterHelp.Length == 0)
                    {
                        counterData[i].CounterHelp = counterData[i].CounterName;
                    }
                }
            }
        }