November 2004 Entries

make_pair in C#


Recently in the C# forum on the Code Project someone asked of a mechanism to implement a make_pair in C#. make_pair is provided in the Standard Template Library in C++ which allows you to easily maintain a pair of two specified types in one contained data element. While there are classes in place within the .NET Framework version 1.1 that allow you to perform similar operations, basically any class that implements the IDictionary interface provides this base functionality that make_pair exposes (i.e, Hashtable, HybridDictionary, ListDictionary, etc) although access to their members isn't as direct as a make_pair exposes. While that doesn't raise the level of difficulty, lets look at another option. When we look at what version 2.0 of the .NET Framework provides in terms of generics, we can easily implement a make_pair in C#. The following is an example I put together that will run under version 2.0.

public class make_pair<T1, T2>
public T1 first
get {return this.t1;}
set {this.t1 = value;}

public T2 second
get { return this.t2; }
set { this.t2 = value; }

public make_pair(T1 type1, T2 type2)
this.t1 = type1;
this.t2 = type2;
private T1 t1;
private T2 t2;

Now that we have that defined we can use it like this:

make_pair<int, int> mpi = new make_pair<int, int>(5, 10);
Console.WriteLine("1st:{0}\n2nd:{1}", mpi.first, mpi.second);
mpi.first = 50;
mpi.second = 25;
Console.WriteLine("1st:{0}\n2nd:{1}", mpi.first, mpi.second);



I was reading Chris Anderson's post on a new web browser he came across called Maxthon. It runs using the IE rendering and network stack but has lots of extra features such as tabbed windows, skinning, and a utility bar to launch external applications from within the browser. After just a few minutes of playing with it I am very impressed, check it out here.

Check to see if your .dll or .exe is a .NET assembly in unmanaged C++


Javier recently made an interesting post on how to check to see if a .dll or .exe is in fact a .NET assembly or not. This can also be done in unmanaged code so I took this as an opportunity to sit down and write a quick example of how we can check a file to see if it is a .NET Assembly. This is what I came up with.

#include "cor.h"
#include <stdio.h>
bool IsAssembly(char file[]);
int main(int argc, char* argv[])
bool bIsAssembly = false;
bIsAssembly = IsAssembly(argv[1]);
printf("I am a .NET Assembly.\n");
printf("I am not .NET Assembly.\n");
return 0;
bool IsAssembly(char file[])
bool bIsAssembly = false;
mdAssembly asmToken;
IMetaDataDispenser* pIMetaDataDispenser;
IMetaDataAssemblyImport* pIMetaDataAssemblyImport;
hr = CoCreateInstance(CLSID_CorMetaDataDispenser, 0,
wchar_t filename[MAX_PATH];
mbstowcs(filename, file, lstrlen(file)+1);
hr = pIMetaDataDispenser->OpenScope(filename, ofRead,
hr = pIMetaDataAssemblyImport->GetAssemblyFromScope(&asmToken);
if(hr == S_OK)
bIsAssembly = true;
bIsAssembly = false;
return bIsAssembly;

Great Weekend


Well, I had a great weekend, I went up and tailgated with Heath and his wife who I hadn't seen since they moved away to Redmond, the weather outside was excellent for such an event. We went up to tailgate for the Iowa State vs. Nebraska football game in Ames. Now Nebraska has always been known for dominating the college football scene but ever since Tom Osborne left their coaching staff Nebraska hasn't been the same. Iowa State came out and strong and won the game 34 - 27. It's funny what can happen during tailgating; someone even attempted to speak in Unicode (that's correct, speaking Unicode - you know who you are;-)). All in all, we had a blast and then Megan joined us for dinner later on that night. Glad they were able to come back for such a great occasion.