internal static PerformanceCounterLib GetPerformanceCounterLib(string machineName, CultureInfo culture)
        {
            SharedUtils.CheckEnvironment();
            string lcid = culture.LCID.ToString("X3", CultureInfo.InvariantCulture);

            if (machineName.CompareTo(".") == 0)
            {
                machineName = ComputerName.ToLower(CultureInfo.InvariantCulture);
            }
            else
            {
                machineName = machineName.ToLower(CultureInfo.InvariantCulture);
            }
            if (libraryTable == null)
            {
                lock (InternalSyncObject)
                {
                    if (libraryTable == null)
                    {
                        libraryTable = new Hashtable();
                    }
                }
            }
            string key = machineName + ":" + lcid;

            if (libraryTable.Contains(key))
            {
                return((PerformanceCounterLib)libraryTable[key]);
            }
            PerformanceCounterLib lib = new PerformanceCounterLib(machineName, lcid);

            libraryTable[key] = lib;
            return(lib);
        }
        public static bool Exists(string logName, string machineName)
        {
            bool flag;

            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString("InvalidParameterFormat", new object[] { "machineName" }));
            }
            new EventLogPermission(EventLogPermissionAccess.Administer, machineName).Demand();
            if ((logName == null) || (logName.Length == 0))
            {
                return(false);
            }
            SharedUtils.CheckEnvironment();
            _UnsafeGetAssertPermSet().Assert();
            RegistryKey eventLogRegKey = null;
            RegistryKey key2           = null;

            try
            {
                eventLogRegKey = GetEventLogRegKey(machineName, false);
                if (eventLogRegKey == null)
                {
                    return(false);
                }
                key2 = eventLogRegKey.OpenSubKey(logName, false);
                flag = key2 != null;
            }
            finally
            {
                if (eventLogRegKey != null)
                {
                    eventLogRegKey.Close();
                }
                if (key2 != null)
                {
                    key2.Close();
                }
                CodeAccessPermission.RevertAssert();
            }
            return(flag);
        }
        public static EventLog[] GetEventLogs(string machineName)
        {
            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString("InvalidParameter", new object[] { "machineName", machineName }));
            }
            new EventLogPermission(EventLogPermissionAccess.Administer, machineName).Demand();
            SharedUtils.CheckEnvironment();
            string[] subKeyNames = new string[0];
            _UnsafeGetAssertPermSet().Assert();
            RegistryKey eventLogRegKey = null;

            try
            {
                eventLogRegKey = GetEventLogRegKey(machineName, false);
                if (eventLogRegKey == null)
                {
                    throw new InvalidOperationException(SR.GetString("RegKeyMissingShort", new object[] { @"SYSTEM\CurrentControlSet\Services\EventLog", machineName }));
                }
                subKeyNames = eventLogRegKey.GetSubKeyNames();
            }
            finally
            {
                if (eventLogRegKey != null)
                {
                    eventLogRegKey.Close();
                }
                CodeAccessPermission.RevertAssert();
            }
            EventLog[] logArray = new EventLog[subKeyNames.Length];
            for (int i = 0; i < subKeyNames.Length; i++)
            {
                logArray[i] = new EventLog(subKeyNames[i], machineName);
            }
            return(logArray);
        }
 private static RegistryKey FindSourceRegistration(string source, string machineName, bool readOnly, bool wantToCreate)
 {
     if ((source != null) && (source.Length != 0))
     {
         SharedUtils.CheckEnvironment();
         _UnsafeGetAssertPermSet().Assert();
         RegistryKey eventLogRegKey = null;
         try
         {
             eventLogRegKey = GetEventLogRegKey(machineName, !readOnly);
             if (eventLogRegKey == null)
             {
                 return(null);
             }
             StringBuilder builder     = null;
             string[]      subKeyNames = eventLogRegKey.GetSubKeyNames();
             for (int i = 0; i < subKeyNames.Length; i++)
             {
                 RegistryKey key2 = null;
                 try
                 {
                     RegistryKey key3 = eventLogRegKey.OpenSubKey(subKeyNames[i], !readOnly);
                     if (key3 != null)
                     {
                         key2 = key3.OpenSubKey(source, !readOnly);
                         if (key2 != null)
                         {
                             return(key3);
                         }
                     }
                 }
                 catch (UnauthorizedAccessException)
                 {
                     if (builder == null)
                     {
                         builder = new StringBuilder(subKeyNames[i]);
                     }
                     else
                     {
                         builder.Append(", ");
                         builder.Append(subKeyNames[i]);
                     }
                 }
                 catch (SecurityException)
                 {
                     if (builder == null)
                     {
                         builder = new StringBuilder(subKeyNames[i]);
                     }
                     else
                     {
                         builder.Append(", ");
                         builder.Append(subKeyNames[i]);
                     }
                 }
                 finally
                 {
                     if (key2 != null)
                     {
                         key2.Close();
                     }
                 }
             }
             if (builder != null)
             {
                 throw new SecurityException(SR.GetString(wantToCreate ? "SomeLogsInaccessibleToCreate" : "SomeLogsInaccessible", new object[] { builder.ToString() }));
             }
         }
         finally
         {
             if (eventLogRegKey != null)
             {
                 eventLogRegKey.Close();
             }
             CodeAccessPermission.RevertAssert();
         }
     }
     return(null);
 }
        public static void DeleteEventSource(string source, string machineName)
        {
            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString("InvalidParameter", new object[] { "machineName", machineName }));
            }
            new EventLogPermission(EventLogPermissionAccess.Administer, machineName).Demand();
            SharedUtils.CheckEnvironment();
            _UnsafeGetAssertPermSet().Assert();
            Mutex mutex = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                SharedUtils.EnterMutex("netfxeventlog.1.0", ref mutex);
                RegistryKey key = null;
                using (key = FindSourceRegistration(source, machineName, true))
                {
                    if (key == null)
                    {
                        if (machineName == null)
                        {
                            throw new ArgumentException(SR.GetString("LocalSourceNotRegistered", new object[] { source }));
                        }
                        throw new ArgumentException(SR.GetString("SourceNotRegistered", new object[] { source, machineName, @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog" }));
                    }
                    string name = key.Name;
                    int    num  = name.LastIndexOf('\\');
                    if (string.Compare(name, num + 1, source, 0, name.Length - num, StringComparison.Ordinal) == 0)
                    {
                        throw new InvalidOperationException(SR.GetString("CannotDeleteEqualSource", new object[] { source }));
                    }
                }
                try
                {
                    key = FindSourceRegistration(source, machineName, false);
                    key.DeleteSubKeyTree(source);
                    if (!SkipRegPatch)
                    {
                        string[]  strArray = (string[])key.GetValue("Sources");
                        ArrayList list     = new ArrayList(strArray.Length - 1);
                        for (int i = 0; i < strArray.Length; i++)
                        {
                            if (strArray[i] != source)
                            {
                                list.Add(strArray[i]);
                            }
                        }
                        string[] array = new string[list.Count];
                        list.CopyTo(array);
                        key.SetValue("Sources", array, RegistryValueKind.MultiString);
                    }
                }
                finally
                {
                    if (key != null)
                    {
                        key.Flush();
                        key.Close();
                    }
                    CodeAccessPermission.RevertAssert();
                }
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                }
            }
        }
        public static void Delete(string logName, string machineName)
        {
            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString("InvalidParameterFormat", new object[] { "machineName" }));
            }
            if ((logName == null) || (logName.Length == 0))
            {
                throw new ArgumentException(SR.GetString("NoLogName"));
            }
            if (!ValidLogName(logName, false))
            {
                throw new InvalidOperationException(SR.GetString("BadLogName"));
            }
            new EventLogPermission(EventLogPermissionAccess.Administer, machineName).Demand();
            SharedUtils.CheckEnvironment();
            _UnsafeGetAssertPermSet().Assert();
            RegistryKey eventLogRegKey = null;
            Mutex       mutex          = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                SharedUtils.EnterMutex("netfxeventlog.1.0", ref mutex);
                try
                {
                    eventLogRegKey = GetEventLogRegKey(machineName, true);
                    if (eventLogRegKey == null)
                    {
                        throw new InvalidOperationException(SR.GetString("RegKeyNoAccess", new object[] { @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog", machineName }));
                    }
                    using (RegistryKey key2 = eventLogRegKey.OpenSubKey(logName))
                    {
                        if (key2 == null)
                        {
                            throw new InvalidOperationException(SR.GetString("MissingLog", new object[] { logName, machineName }));
                        }
                        EventLog log = new EventLog(logName, machineName);
                        try
                        {
                            log.Clear();
                        }
                        finally
                        {
                            log.Close();
                        }
                        string path = null;
                        try
                        {
                            path = (string)key2.GetValue("File");
                        }
                        catch
                        {
                        }
                        if (path != null)
                        {
                            try
                            {
                                File.Delete(path);
                            }
                            catch
                            {
                            }
                        }
                    }
                    eventLogRegKey.DeleteSubKeyTree(logName);
                }
                finally
                {
                    if (eventLogRegKey != null)
                    {
                        eventLogRegKey.Close();
                    }
                    CodeAccessPermission.RevertAssert();
                }
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                }
            }
        }
Esempio n. 7
0
            private unsafe void Initialize(int fileMappingSize)
            {
                string mappingName = null;

                SharedUtils.CheckEnvironment();
                if (SharedUtils.CurrentEnvironment == SharedUtils.W2kEnvironment)
                {
                    mappingName = "Global\\" + FileMappingName;
                }
                else
                {
                    mappingName = FileMappingName;
                }

                IntPtr securityDescriptorPointer = IntPtr.Zero;

                try {
                    // The sddl string consists of these parts:
                    // D:           it's a DACL
                    // (A;;         this is an allow ACE
                    // FRFWGRGW;;;  allow file read, file write, generic read and generic write
                    // WD)          granted to Everyone
                    string sddlString = "D:(A;OICI;FRFWGRGW;;;WD)";
                    if (!UnsafeNativeMethods.ConvertStringSecurityDescriptorToSecurityDescriptor(sddlString, NativeMethods.SDDL_REVISION_1,
                                                                                                 out securityDescriptorPointer, IntPtr.Zero))
                    {
                        throw new InvalidOperationException(SR.GetString(SR.SetSecurityDescriptorFailed));
                    }

                    NativeMethods.SECURITY_ATTRIBUTES securityAttributes = new NativeMethods.SECURITY_ATTRIBUTES();
                    securityAttributes.lpSecurityDescriptor = securityDescriptorPointer;
                    securityAttributes.bInheritHandle       = false;


                    fileMappingHandle = UnsafeNativeMethods.CreateFileMapping(new HandleRef(null, (IntPtr)(-1)), securityAttributes, NativeMethods.PAGE_READWRITE,
                                                                              0, fileMappingSize, mappingName);
                    int error = Marshal.GetLastWin32Error();
                    if (fileMappingHandle == (IntPtr)0 &&
                        (error == NativeMethods.ERROR_ALREADY_EXISTS || error == NativeMethods.ERROR_ACCESS_DENIED))
                    {
                        fileMappingHandle = UnsafeNativeMethods.OpenFileMapping(NativeMethods.FILE_MAP_WRITE, false, mappingName);
                    }
                }
                finally {
                    SafeNativeMethods.LocalFree(securityDescriptorPointer);
                }

                if (fileMappingHandle == (IntPtr)0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.CantCreateFileMapping));
                }


                FileViewAddress = UnsafeNativeMethods.MapViewOfFile(new HandleRef(this, fileMappingHandle), NativeMethods.FILE_MAP_WRITE, 0, 0, 0);
                if (FileViewAddress == (IntPtr)0)
                {
                    throw new InvalidOperationException(SR.GetString(SR.CantMapFileView));
                }

                FileMappingSize = fileMappingSize;
                SafeNativeMethods.InterlockedCompareExchange(FileViewAddress, 4, 0);
                AppDomain.CurrentDomain.DomainUnload += new EventHandler(this.OnAppDomainUnload);
            }