A Few Words on World Hunger

According to National Geographic, the world population will reach the 9 billion mark by 2050 if the population in some developing countries continues to grow in the current trend. Other issues aside, one important question that we should ask ourselves is: will the planet produce enough food to feed so many by 2050?

While those of us that live in developed countries go about our business everyday having satisfied our basic needs, there are millions in other corners of the world that are starving to death, due to reasons often beyond their control – wars, natural disasters, and political isolation, just to name a few. Interestingly, the bulk of these people are responsible for producing the food that we so take for granted on our dinner table.  Anyone with half a wit can immediately discern the viscious cycle that this is going to cause. That’s right. Food-producers starve and die, which leaves less people to grow food; however, the planet’s population continues to grow regardless, which in turns demands more food that cannot be realistically produced because of the void left behind by the very food-producers.

We should do something about this.

It is, in my opinion, the inherent responsibiliy of developed nations to help those that have more dire needs than us. We should start out with children, because they are, however cliché it might sound, the future. Their survival will help ensure that the effect of the inevitable world food shortage will be minimal in the coming decades. It saddens me to see stories like this  (http://www.nu.nl/hongersnood/2574292/kinderen-sterven-paden-doods-afrika.html) popping up on news websites almost on a daily basis. This Dutch article talks about the fact that women and children are the most vulnerable in a famine;  some kids suffer permanent damage to their brains as a result of that, if they are lucky enough to survive the famine (or unlucky depending on your point of view).  UN’s World Food Program will contribute enough to provide for 2.5 million of those underfed children in the near future with the hope that more donations will stream in to alleviate the situation. This is just one example of course. There are dozens of other non-profit organizations that play an important role in providing aid to countries in need.

Nevertheless, donating money and food to the famished is effective only in the short term. Natural disasters such as droughts will continue to strike, often the same region and charity is simply not a viable solution in the long term. So we turn to science. There is an old proverb — ‘Give a man a fish; you have fed him for today. Teach a man to fish; and you have fed him for a lifetime.’ — which I am sure most people have heard of before. This applies here as well. Charity can only go so far. At the end of the day, research and development will better ensure the sustainability of our food supply than continuing to give ‘fish’ to those who are hungry. Examples like water management, innovative seed technology and even education will go a long way in shaping a better prospect for their lives, and consequently our lives. This can only be done with our help.You will find one example here (http://www.albawaba.com/helping-nations-grow-crops-will-reduce-hunger-us-379013)

Give generously to those in need, especially to children. At the end of the day, you aren’t just helping them. You are also helping yourself and your future generations with growing and maintaining our food supply.

Vitamin B1 vs Mosquito bites

I had an interesting conversation with a colleague the other day and one of the topics that she brought up was the myth that Vitamin B1, also known as thiamin, is said to prevent mosquito bites when consumed. She swore by this method as it seemed to have worked for someone she knows. Prior to this conversation, I was never aware of such a simple, yet effective-sounding insect repelling method. So being the skeptic that I was, I took the idea with a grain of salt.

A brief look on the Internet has afforded me no consensus on this topic. In some cases, people who are previously prone to mosquito bites are now free from the daily routine of mosquito hunting before bed; yet in other cases, the introduction of additional thiamin to their diet has done nilch to the invasion of the swarm at night. So to believe or not to believe?

There is one slight issue that I see with this assertion. It’s the validity of it. So far, any scientific study done on this topic has produced no significant results. That is not to say that we can simply conclude from this that Vitamin B1 has no effect on repelling mosquitoes from your vicinity. Which target a mosquito chooses is dependent on a complicated formula of chemical reactions that occur both inside and outside one’s body. The difficulties involved in isolating these factors have probably hindered any serious effort to discover empirically verifiable evidence needed to identify causal relationships, if any. Therefore, without any knowledge of these relationships, people can only resort to the trial and error approach.

In my opinion, thiamin most likely does have some sort of effect on deterring those pesky insects; however, the extent of this effect is heavily influenced by an aggregation of factors in the unique environment that a given victim happens to be in at the time, the various chemical interactions within the victim and the cavalcade of unique events that occur pre and during a mosquito onslaught.

So should you be taking Vitamin B1 tablets when you are being bitten? My answer is why not? Thiamin has some very useful and established health benefits. It helps maintain your cardiovascular activities as well as alleviate stress by converting carbohydrates into glucose, which in turn strengthens the nervous system. You should be eating foods rich in thiamin, such as beans, regardless of whether your ultimate goal is to prevent mosquito bites. Extra supplement in the form of B1 tablets can only be more beneficial to you.

Creating a COM object from scratch with C#

What is a COM object

COM (Component Object Model) is a very outdated software standard that has since been largely superceded by the .NET framework. The main purpose for its existence is that regardless of the language it’s programmed in, its members, when exposed via unique GUIDs, can be accessed by different processes written in different languages, thus giving rise to interprocess communication. Although I encourage you to create .NET objects, there are situations where creating a COM object is the better, if not the only option available to you. A good example of this is writing external routines for SQL databases that operate on SQL 2000 or below, or SQL 2005/2008 databases that operate in 2000 compatibility mode. SQL 2000 or below only supports calling COM objects from its stored procedures where as SQL 2005 or above introduces the ability to enable CLR (Common Language Runtime) on a database, which enables programmers to bind external .NET objects to SQL functions and stored procedures.

Creating a COM object in C#

There are various bits of information available on the Internet that show you how to do this. However, none of them seems to take you from the beginning to the end. This is what I will strive to achieve in this blog post.

Prerequisites:

  1. Microsoft Visual C# 2010 Express (download here)
  2. The guidgen.exe tool (download here)
  3. The sn.exe tool (if you have Visual studio installed, then it’s found here: C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin and if not, you can download the .NET SDK here)
  4. Have at least .NET Framework 2.0 installed (latest version can be downloaded here)
  5. The regasm.exe tool, found here after 4. is satisfied: (for .NET 2.0, 3.0 or 3.5) %windir%\Microsoft.NET\Framework\v2.0.50727; (for .NET 4.0) %windir%\Microsoft.NET\Framework\v4.0.30319
  6. Some experience with .NET programming (not essential, but definitely helps with understanding the example here. Note that this can be translated into VB .NET, you just need to know the corresponding syntaxes.)

Steps:

1. Creating a new project in C#

Start by creating a new project in C# Express, when presented with the templates to choose from, select Class Library, give it a meaningful name (I shall name it ComClassExample) and hit OK. This will create two .cs files, one being AssemblyInfo.cs, which is common to all class libraries and class1.cs, which you can rename to something more meaningful. Renaming it will also rename your public class name (I will rename it to ComClassExample.cs). Save the project (File -> Save all). Open the ComClassExample.cs file so we can start editing.

Before we start creating our class, add this namespace to your existing list; our entire example will make use of it:

using System.Runtime.InteropServices;

2. Constructing the class

The first thing to consider is of course the class itself. Create a basic class with a constructor and simple method that returns a value of int type:

public class ComClassExample
{
 // constructor - does nothing in this example
 public ComClassExample() {}

 // a method that returns an int
 public int AddTheseUp(int adder1, int adder2)
 {
  return adder1 + adder2;
 }
}

3. Presenting our class through an interface

Remember that even though the eventual objective of this exercise is to expose the dll to the world of COM, the code you are writing is still managed and there needs to be a way to tell the COM clients how to access the public methods/properties of your class. You will also need to determine how you want your COM objects to be called – at compile-time or run-time (i.e. early binding or late binding). Script languages such as Javascript are not compiled and thus have no way of determining the type of COM object it’s calling (in other words, they only support late-binding). Therefore, it’s a lot safer to enable an interface for late-binding than early binding (you can also enable both) unless you have a really good reason to exclude late-binding.

Create a public interface and add an abstract definition of every method or property you wish to expose from your class object in the interface. You can optionally add the attribute [DispId] to each method to manually control how COM services will call them.

public interface IComClassExample
{
 [DispId(1)]
 int AddTheseUp(int adder1, int adder2);
}

4. Making the connection

OK, so we have the flesh and blood of the code covered, we will need bones and nerves to correlate them. Firstly, we need to let our class know which interface will be responsible for exposing its methods (you can have multiple interfaces and classes in your namespace if you want, but we will stick with 1 in this example for simplicity’s sake). You can achieve this by derivation, which in this case is to implement the interface. Since we manually created the interface, we need to tell the compiler not to generate another automatically as this will cause confusion. We can do this by using the ClassInterface attribute and setting its value to ClassInterfaceType.None. The code for your class will now look like this:

[ClassInterface(ClassInterfaceType.None)]
public class ComClassExample : IComClassExample
{
  // constructor - does nothing in this example
  public ComClassExample() { }

  // a method that returns an int
  public int AddTheseUp(int adder1, int adder2)
  {
     return adder1 + adder2;
  }
}

Back to our interface, by default, an interface is enabled for both early and late-binding, however in this case we want it to be set to late-binding only. We can use the InterafaceType attribute to achieve this. Note that the InterfaceIsDispatch type corresponds to late-binding (see here for more info about IDispatch and IUnknown interfaces):

[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IComClassExample {
 [DispId(1)]
 int AddTheseUp(int adder1, int adder2);
}

5. Making them unique

Every COM assembly and its public types (i.e. interfaces, classes and et cetera) need a unique GUID. By default, Visual C# 2010 assigns a unique GUID to the entire assembly automatically at compile-time, but it does not do so for the public types. You have two options here. You can either manually generate GUIDs with the guidgen.exe tool and assign them to each type individually or you can use regasm.exe to automatically generate them for you. In this case, we are going to proceed with the former approach.

Start up guidgen.exe and pick Registry format. Generate a new GUID for each of your public types (in the case of this example, one class plus one interface) and paste it into the Guid attribute. Your code should now resemble something like this, note that I moved the square brackets to accommendate more attributes and a comma is used to separate them:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ComClassExample
{
 [Guid("7068AC34-DBB0-4e40-84A7-C2243355E2D7"),
 InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
 public interface IComClassExample
 {
  [DispId(1)]
  int AddTheseUp(int adder1, int adder2);
 }

 [Guid("863AEADA-EE73-4f4a-ABC0-3FB384CB41AA"),
 ClassInterface(ClassInterfaceType.None)]
 public class ComClassExample : IComClassExample
 {
  // constructor - does nothing in this example
  public ComClassExample() { }

  // a method that returns an int
  public int AddTheseUp(int adder1, int adder2)
  {
    return adder1 + adder2;
  }
 }
}

6. Going underground with cryptography

We are almost done. We will now need to give our assembly a strong name. A strong name gives this assembly a unique signature and allows it and its cousins (i.e. different versions of the same module) to run side-by-side. We do this with the sn.exe utility.

Run the following command and copy the resulting file to the project’s root directory (c:\Users\John.Doe\My Documents\Visual Studio 2010\Projects\ComClassExample\ComClassExample):

sn -k ComClassExample.snk

Now we need to add a link to the .snk file to your project. Right click on your project name -> Add -> Existing item. Browse to your project directory, highlight the .snk file (You might have to change file types drop down to All Files in order for the file to become visible), click on the down arrow beside the Add button and  select Add as Link. With that done, we now need to make the compiler aware that we want to use this .snk file to sign our assembly with a strong name.  Go to the project properties, select the Signing tab and click on Sign the assembly to enable the section. Select .snk file from the dropdown list and save the project.

7. Wrapping it all up

There are a few more project options that we need to modify before we F6 the solution. Go to project properties once again. This time, go to the Application tab. Under Target framework, choose your desired version. Your decision on this will be influenced by factors such as which .NET Framework version the target machines that you will using this dll on have installed. We will select .NET Framework 4 for this example. Now click on the Assembly Information… button. Ensure that the Make assembly COM-Visible option is checked. Close the dialogue box. Now click on the Build tab and check the Rigister for COM interop box. Save your project

Just a note on the Make assembly COM-Visible option. If you open up the AssemblyInfo.cs file, you will notice that the [assembly: ComVisible()] option has now been set to true. If you have a large assembly that contains multiple classes and interfaces, you might not wish to expose every one of them to COM. In that case, you can set this option to false (or do so through project properties as mentioned above), and explicitly tag every interface/class that you wish to expose with the ComVisible attribute. This obviously gives you granular control over their COM visibility; however, for the purpose of this example, we will want to expose everything and making the assembly globally COM-visible seems more appropriate.

8. Compiling it and taking it out for a walk

We can now compile our assembly by clicking on Build -> Build Solution. It should compile without any warnings or errors. The compiler does all the registration for you on your development PC, so you don’t have to manually do so.

There are all sorts of ways to test this. Here are three ways:

i) Vbscript

Create a new .vbs file, edit it with the following lines, save it and run it:

dim objTest, intResult
Set objTest = WScript.CreateObject ("ComClassExample.ComClassExample")

intResult = objTest.AddTheseUp (100, 200)
Wscript.echo "Result = " & intResult

ii) Powershell

Start up Powershell ISE, edit it with the following code and hit F5 to run:

$object = new-object -ComObject ComClassExample.ComClassExample
write-host $object.AddTheseUp(100, 200)

iii) Javascript

Create a new .html file, insert the following code and run it through a browser (you might want to enable ActiveX):

<HTML>
<HEAD>
<SCRIPT language="javascript">
function Test() {
var objTest = new ActiveXObject("ComClassExample.ComClassExample");
alert(objTest.AddTheseUp(100, 200));
}
</SCRIPT>
</HEAD>
<BODY>
<INPUT Type="button" onClick="Test()" Value="Click here to test!">
</BODY>
</HTML>

9. Deploying the dll on another machine

It’s all rosy on your development machine. What if you want to deploy the dll to another machine? It’s very simple.

Copy the dll (should be located in your project directory/bin/release) to your target machine. Ignore the .pdb and .tlb files. Go to the target machine and locate the regasm.exe tool (again depending on the version of .NET framework your dll is compiled in, choose the most appropriate version of regasm.exe. If in doubt, refer to the prerequisites section of this blog). Run the following command to register the dll.

regasm ComClassExample.dll /codebase /tlb /nologo

That’s pretty much it. You can use the script examples in 8. to test the dll out on the target machine. I hope I have been able to help you. If you have any questions, please leave a comment below. Thanks.

Kinect – a new way to stay fit

Kinect, an Xbox 360 accessory is a relatively new technology to the health and wellbeing scene. I have searched exhaustively on the Internet for (non-game related) articles on this topic that formally embraces this new technology as an alternative form of exercise, but atlas, the results are few and far inbetween. There are websites such as BBC that have fantastic articles on fitness and health in general (such as [here]), but what disappoints me is the lack of mention on the more alternative, less known options.

Kinect titles such as Kinect Sports, Dance Central and Your shape: fitness evolved are all excellent examples of cheap, disciplined and home-based alternatives to gym and outdoor activities. They keep the players motivated through a variety of exercises, are not restricted by the weather or travel distances, and are cheaper to own than gym memberships. In my opinion, they should be especially attractive to the niche of individuals who have little time or budget to spend on fitness (that is, not counting actual gamers). That being said, however, they should not be used to replace the other exercise methods. Striking a good balance between the different forms is a technique that you will need to work out and implement yourself based on your own schedule and needs.

Italian starters – demystified

I am sure that there are a number of you Italian cuisine lovers out there who have wondered about the same thing. Why are there two different types of starters on an Italian menu, an oddity out of most other European cuisines. I decided to do some research on this topic, and for your convenience, I will attempt to give an explanation for each and what it traditionally involves.

1) Antipasti

To help you understand this, I shall break the word down. The Italian prefix anti-, in this particular case, means prior to or before, which is equivalent to the English prefix ante- (as in antechamber). The word pasti is the plural of pasto meaning a meal. So sewing everything together, we have pre-meal dishes. This is usually the first course (or first first course, rather) served in an Italian restaurant. It includes every traditional appetizer,  from salami or meat slices to caprese (shortened form of insalata caprese – a salad consisting of tomatoes, mozzarella cheese and basil leaves bathed in olive oil) with the sole exception of pastas.

2) Primi Piatti

primi piatti is the plural form of primo piatto meaning literally first dishes (in Italian, like in all the other Romance languages, the adjective almost always agrees with the gender of the noun). As the name subtly suggests, this course is the first course, or the starter of a meal, even though it is usually served after the antipasto, which is in fact not considered a part of the official meal.  The primi piatti menu consists entirely of pastas, which come in a myriad of forms such as ravioli, spaghetti, and penne, cooked in various ways to prepare your tastebuds for the main course – secondi piatti.