Archive for April, 2007

Performance counter permissions

April 30, 2007

Am I doing this wrong? I would expect the code below to either allow me to create the performance counter category, or to throw a SecurityException when I Demand the permission to Administer performance counters. When I run it as an underpriveleged user, it does neither.

using System;
using System.Diagnostics;

namespace TestDemand
{
    class Program
    {
        private string _categoryName = "Bogus Category";
        private string _machineName = ".";

        static void Main(string[] args)
        {
            Program p = new Program();
            if (0 < args.Length &&
                "-d" == args[0].Substring(0, 2).ToLower())
            {
                p.DeleteCategory();
            }
            else
            {
                p.CreateCategory();
            }
        }

        private void CreateCategory()
        {
            try
            {
                if (!Demand(PerformanceCounterPermissionAccess.Read))
                {
                    Console.WriteLine("Failure: Could not get permission to read Performance Counter Categories");
                    return;
                }

                if (PerformanceCounterCategory.Exists(this._categoryName, this._machineName))
                {
                    Console.WriteLine("Failure: Category \"{0}\" already exists.", this._categoryName);
                    return;
                }

                if (Demand(PerformanceCounterPermissionAccess.Administer))
                {
                    Console.WriteLine("Got permission to Administer Performance Counter Categories");
                }
                else
                {
                    Console.WriteLine("Success: Could not get permission to Administer Performance Counter Categories");
                    return;
                }

                CounterCreationDataCollection counters = new CounterCreationDataCollection();
                counters.Add(new CounterCreationData("Some counter", "Counter help text",
                                                                           PerformanceCounterType.NumberOfItems32));
                PerformanceCounterCategory.Create(this._categoryName, "Category help string",
                    PerformanceCounterCategoryType.SingleInstance, counters);
            }
            catch (Exception e)
            {
                Console.WriteLine("Failure: {0}", e.Message);
                Console.WriteLine(e.StackTrace);
                return;
            }
            if (PerformanceCounterCategory.Exists(this._categoryName))
            {
                Console.WriteLine("Success: Category successfully created");
            }
            else
            {
                Console.WriteLine("Failure: No error but category was not created");
            }
        }
        private void DeleteCategory()
        {
            if (!Demand(PerformanceCounterPermissionAccess.Administer))
            {
                Console.WriteLine("Failure: Could not get permission to Administer Performance Counter Categories");
            }
            PerformanceCounterCategory.Delete(this._categoryName);
            if (PerformanceCounterCategory.Exists(this._categoryName))
            {
                Console.WriteLine("Failure: Category was not deleted");
            }
            else
            {
                Console.WriteLine("Success: Category successfully deleted");
            }
        }

        private bool Demand(PerformanceCounterPermissionAccess access)
        {
            bool result = true;
            try
            {
                PerformanceCounterPermission permission = new PerformanceCounterPermission();
                permission.PermissionEntries.Add(new PerformanceCounterPermissionEntry(
                    access, _machineName, _categoryName));
                permission.Demand();
            }
            catch (System.Security.SecurityException)
            {
                result = false;
            }
            return result;
        }
    }
}
Advertisements

Outlook error 800ccc0e

April 16, 2007

If you’re getting this error, and it’s not your firewall or antivirus settings, it may be your ISP.

Everyone in the house but me uses Outlook 2000 for their email (or did until this morning, when I switched Gareth over to Thunderbird). I use Gmail’s web interface. Lisa has been sending and receiving emails without problems, but Gareth has not been able to send email for some time, persistently getting error 0x800ccc0e.

Search for this error code in Google, and what you’ll get told is that your firewall or antivirus software is blocking port 25. It wasn’t until the bottom of the third page of hits that anyone made the obvious point that it could be my ISP blocking port 25. Obviously they let out SMTP to well known mail providers such as Google, but not to my dodgy hosting provider in the states, which explains why Lisa can get her mail but Gareth can’t – Lisa’s mail is forwarded from morrish.org to gmail.com, but Gareth’s isn’t (he has less of a spam problem). Said dodgy provider is aware of this, and also provides SMTP on port 587, which Telecom does not block.