Saturday, March 24, 2007

Sternr's Complete Programmer Tutorial

Many people ask me, "sternr, how can one be like you - such a brilliant, funny yet good looking programmer???" ,

Well my friends, I decided to do my duty to man kind, and reveal my 10 steps to become a hardcore programmer!

Sternr's Complete HowTo Tutorial for Becoming a Hardcore Programmer

1. You're not a programmer, you're a Software Engineer.
And a damn good one if I might say.

2. There is no combination of upper-case letters you haven't heard of.
You simple know it by its earlier code name.

3. The Dual Buzzword Principle - Use as many buzzwords as you can.
but beware - there is a small and extincting specie of bold programmers who might ask you to explain what it means.
But even these rare few will not dare to ask twice - and here comes the duality principle: always save a backup buzzword to explain the buzzword.

4. Every design\idea that is not yours has a flaw.
If you cant find it, than it's a security hole - and besides, you would've done it differently...

5. NEVER argue with the Sysadmin - unlike the programmer next door, he really know what he's talking about.

6. Your code is NOT complex and hard to maintain.
It's simply flexible. very flexible.

7. The future is here - advice and talk as much as you can about technologies & products that still have no release date.
You cant be contradicted, yet no hard question can be asked - the design is not final...

8. Install a Linux on at least one of your computers.
You don't have to study, like or use it. but you must have one.
Note: It is recommended to say once a day (don't overdue else you'd be asked questions you cant answer) why you prefer Linux

9. Command Line - Use it as much as you can.
Everyone knows its stupid, but it looks impressive.

10. This is the last, but most important.
In the Hi-Tech industry - and IT especially, people are measured by 2 things and 2 things only: Self Confidence, and Experience.

It is very unfortunate (one might notice that this has nothing to do with how smart or good one is...) but true.

And because of it, it doesn't matter how much or what mistakes you do, its simply how you react to them, its how you react in meetings, and especially under pressure.

So simply stay cool.

Like me ;)

Monday, March 19, 2007

IIS 6.0 & "Connection Dropped" HTTP.sys Error

If you're an IIS (6.0) admin, you're probably quite familiar with the HTTP.sys error log, and even more with the "Connection Dropped" mysterious error.
Charis Ad has a great post explaining everything about this apparently not so dangerous error, here's his explanation.

Thanks Chris!

Thursday, March 15, 2007

Whats Behind W3WP Number Three?

Why?
I mean, they've got different names, different id's maybe even different users, so why on earth does IIS 6.0 makes it so hard to know which w3wp.exe process is running which Application pool?!

Although Microsoft offers us the IISApp.vbs script - which does exactly what I want (and even allows you to do certain actions like pool recycling etc.), this script works only on the current machine and has no support for remote servers.

The quickest - yet dirtiest solution would be to run the script remotely - whether it's by using PSEXEC, or through telnet - it's all the same, but not really recommended, especially not on production...

So, what can we do?

WMI to the Rescue!

After some work, I've found out that each w3wp process hosting an Application pool has some very interesting command-line arguments, I'll discuss these arguments in the future but right now the important thing is - the last argument w3wp gets is - hold your breath - the AppPool name it runs! hooray!

Here's a little code sample on how to implement this using .Net:


using System.Management; // Add reference to System.Management

/* A simple Value Object representing one Application Pool */
public class AppPool()
{
    public int ProcessId;
    public string Name;
    public long UsedMemory;
}

/* Gets remote server's name, returns list of all AppPools */
private List<AppPool> getAppPools(string serverName)
{
    ManagementScope scope;

    /* Change the query to select * if you want more details */
    SelectQuery query = new SelectQuery(
"SELECT ProcessId, CommandLine, WorkingSetSize FROM Win32_Process Where Name = 'w3wp.exe'");

    ManagementObjectSearcher searcher;
    List<AppPool> appPools = new List<AppPool>();

    /* If you do not have sufficient rights, impersonate */
    ConnectionOptions options = new ConnectionOptions();
    options.Username = @"MyDomain\admin";
    options.Password = "pwd";
    options.EnablePrivileges = true;

    scope = new ManagementScope(
        @"\\" + serverName + @"\root\cimv2", options);
    scope.Connect();

    searcher = new ManagementObjectSearcher(scope, query);

    foreach (ManagementObject mob in searcher.Get())
    {
      AppPool appPool = new AppPool();
      PropertyData property = mob.Properties["CommandLine"];

      int pos;

      /* Now we parse the AppPool name from the args */
      appPool.Name = property.Value.ToString();
      pos = appPool.Name.Length - 1;
      appPool.Name = appPool.Name.Substring(0, pos);
      pos = appPool.Name.LastIndexOf("\"") + 1;
      appPool.Name = appPool.Name.Substring(pos);

      /* And find the processId for this AppPool */
      property = mob.Properties["ProcessId"];
      appPool.ProcessId = Convert.ToInt32(property.Value);

      /* General process info, i.e: memory usage */
      property = mob.Properties["WorkingSetSize"];
      appPool.UsedMemory = int.parse(property.Value.ToString());

      /* Convert from Bytes, to MBs */
      appPool.UsedMemory = appPool.UsedMemory / 1048576;

      appPools.Add(appPool);

    }

    return appPools;

}


Although I've written this code in .Net 2.0, except for the Generics, I think it should work on 1.x too...

Monday, March 12, 2007

T From Type Generics #2

Generics are fun. but from the first day I started playing with it, I was intrigued by one main question: how to really make it dynamic.

The hole problem lays in T - how the hell, do I create an instance of a generic type???

To be more clear, I'll give an example:


Lets say I have the following class:


public class GenericChild
{ /* Some Code Here */ }


Now, I want to be able to create GenericChild objects dynamically - at runtime, like the following:


public static void Main()
{
  object child = CreateChild(Type.GetType("System.Collections.ArrayList");


How will you implement the "CreateChild" method?


Type.MakeGenericType
Thanks to Oliver Sturm, I found out, that the Type object has been quite extended,
Take a look at the answer:


public static object CreateChild(Type t)
{
  Type origType = typeof(GenericChild<>);
  Type genType = origType.MakeGenericType(childType);
  return genType.Assembly.CreateInstance(genType.FullName);
}


Ah, the wonders of Generics ;)

Tuesday, February 20, 2007

The Search For The Ultimate Icon

In the last couple of weeks, I've been working on a Windows Application,
And while doing so, I've been exposed to the never ending chase after the perfect toolbar icon or tray image, and I have to say - I don't like it.

Well, my dear WinApp friends, search no more!

Thanks to Serge for enlightening us,it turns out M$ has already solve this annoyance for us (though they didn't bother to let us know...).

If you got to the VS.Net (2005) install dir:
***:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary
You'll find a nice zip file containing every icon, image, and even animation you've ever dreamed of! (no, there is no Angelina Jolie animation there...).