Monday, 28 November 2011

Creating a Generic 32bit Wrapper Application

Visual Studio Platform Target Settings

A couple of days ago I was asked if there is a way to interop with a native 32bit library from within an 64bit .NET application. Since the bitness is defined at process level and in .NET the bitness of the starting project determines the bitness of the process, there is only one way to achieve that: you have to spawn a new process, a 32bit process.

The following example of a generic 32bit wrapper application demonstrates how to execute managed code in a simple manner. The wrapper, targeting the x86 platform, is able to execute the specified static method "Main" of the given assembly. The assembly doesn't have to be an executing assembly itself.

// usage: RunAs32Bit.exe [ASSEMBLY] [ARGS ...]
static void Main(string[] args)
{
  var assembly = Assembly.LoadFile(Path.GetFullPath(args[0]));
  var type = assembly.GetTypes().FirstOrDefault(t => t.GetMethod("Main") != null);
  var member = type.GetMethod("Main");
  var arguments = args.Where((v, i) => i > 0).ToArray();

  member.Invoke(type, new object[] { arguments });
}

Spawning a new process that will do some stuff (e.g. interop) may also mean that you have to do some inter-process communication (IPC). I suggest you to use WCF for that approach.

posted on Monday, 28 November 2011 12:12:35 (GMT Standard Time, UTC+00:00)  #    Comments [0]
Thursday, 08 September 2011

Cleaner Event Declarations in C#

The traditional event declaration in C# looks something like this:

public event EventHandler Executed;
protected virtual void OnExecuted()
{
  if (this.Executed != null)
    this.Executed(this, EventArgs.Empty);
}

The gratuitous null check in the virtual OnXYZ event caller has always pestered me and I just learned an interesting alternate syntax to avoid this boiler plate code! Behold the cleaner alternative:

public event EventHandler Executed = (o, e) => { };
protected virtual void OnExecuted()
{
  this.Executed(this, EventArgs.Empty);
}

When declaring the event start by assigning it to a lambda, then your event will never be null. Of course there is a minor performance impact to this, but we’re not prematurely optimizing right?

posted on Thursday, 08 September 2011 05:56:04 (GMT Daylight Time, UTC+01:00)  #    Comments [0]
Tuesday, 06 September 2011

Randomize List sort order using LINQ

I wanted to display 5 random records of a List on my site, imagining a loop that generates a bunch of random indexes and pulling 5 elements from the list.

I decided to use a more elegant solution, by using LINQ with only two lines of code:

var rnd = new Random();
list = list.OrderBy(x => rnd.Next()).ToList();

To take n random elements:

var rnd = new Random();
list = list.OrderBy(x => rnd.Next()).Take(n).ToList();

Encapsulated by using extension methods:

public static IEnumerable<T> OrderByRandom<T>(this IEnumerable<T> source)
{
  var rnd = new Random();
  return source.OrderBy(x => rnd.Next());
}

public static IEnumerable<T> TakeRandom<T>(this IEnumerable<T> source, int n)
{
  return source.OrderByRandom().Take(n);
}
posted on Tuesday, 06 September 2011 05:09:04 (GMT Daylight Time, UTC+01:00)  #    Comments [0]