.NET Nomad

What I've learned along the way

  Home  |   Contact  |   Syndication    |   Login
  16 Posts | 1 Stories | 79 Comments | 0 Trackbacks

News

Archives

Post Categories

Friday, October 07, 2011 #

Post to see if this "wakes up" the blog.

Monday, February 22, 2010 #

One of the things I love about NHibernate is the vibrant community that works so hard to continually improve the library.  I’ve been using NHibernate off and on for a number of years on projects that had short development cycles and it never failed to provide me with a solid framework for my Data Access Layer.

One particularly interesting thing is that NHibernate can generate Create, Update, and Delete scripts that allow you to manage your database and keep it more or less in sync with your entity classes.  It isn’t really a secret to how this works.  You build up a configuration object and the SchemaExport or SchemaUpdate objects look through your mapping information to determine what needs to happen with the database.  The only annoying catch was that there wasn’t a clean, out-of-the-box way to integrate this code with a Continuous Integration environment.

A few solutions surfaced (like these two), but all of them were dependant on a specific CI environment, and would obviously only work in the context of a CI environment.  There was also a solution that allowed you to automatically generate schemas on SessionFactory initialization, but that is a very dangerous practice as it could easily leak into you production build.  That kind of thing only has to happen once for you to wind up on the unemployment line.

So, I’ve recently developed a general purpose command line tool and an accompanying library that should allow developers more flexibility when integrating schema generation into their continuous integration environment of choice.  Further, since it is just a command line tool you can use it outside your CI environment which is great for developers like me that are constantly prototyping on their working copy.

The tool is called NHibernate Schema Tool (nst), and is currently hosted on Codeplex

Using the tool is pretty simple:

 

> nst /c:hibernate.cfg.xml /a:AssemblyWithMappingsAndModels.dll

 

The above command will connect to the database specified in your NHibernate configuration file, and use the mapping files embedded in the AssemblyWithMappingsAndModels.dll as input to the schema generation process.

By default nst will issue a create command which will result in a DDL script that will drop all artifacts in the target database before re-creating them.  You can control the operation by using the /o switch to specify either Create, Update, or Delete.  For example, if I just added a new entity to my domain model and it maps to a new table in the database I can simply say:

 

> nst /c:hibernate.cfg.xml /a:AssemblyWithMappingsAndModels.dll /o:Update

 

This should only add the missing database artifacts and not cause any data loss.

One other thing that I thought was missing from other solutions was any sort of flexibility in where mapping information could come from.  You simply had to adhere to the standard that your mapping files where embedded in the same assembly as your domain classes.  Whether violating this convention is advisable is up to the tech lead on the project, but let’s say you have a situation where you have to store your mapping files in a directory on the file system and your domain classes are obviously in an assembly.  The NHibernate Schema Tool supports this scenario just fine:

 

> nst /c:hibernate.cfg.xml /d:DirectoryWithMappingFiles /m:AssemblyWithDomainClasses.dll

 

The /d switch allows you to specify the directory (or directories separated by ;) that contain your hbm.xml files.  You must also supply the assemblies containing the corresponding domain classes by using the /m switch.

How about one more scenario? I try my best to adhere to the POCO & Persistence Ignorance principles.  In my mind you can’t satisfy both if you are storing ORM specific data in the same assemblies as your domain classes.  So, I made nst support that scenario:

 

> nst /c:hibernate.cfg.xml /a:AssemblyWithOnlyMappings.dll /m:AssemblyWithOnlyModels.dll

 

The above command looks for embedded mapping files from AssemblyWithOnlyMappings and expects the associated domain classes to be defined in AssemblyWithOnlyModels.

If it hasn’t been made clear yet the /a, /d, and /m options are all able to take in multiple values by using a semi-color (;) to separate them.  You can also mix and match by specifying all three switches as input to nst.

 

I hope that the NHibernate Schema Tool makes it a little easier to integrate schema generation for NHibernate mappings with current and future Continuous Integration environments and that it can aid other developers when prototyping out new entities for the domain model.

 

Again, you can grab the bits from the project's page at Codeplex.


Sunday, February 07, 2010 #

Whenever I am working on a project that requires client-side coding, I immediately starting thinking in JQuery.  There are other javascript frameworks out there, but so far I haven’t found one that allowed me to structure my client-side applications quite as cleanly as JQuery does.

The only thing JQuery doesn’t support out-of-the-box is a decent templating system for emitting DOM elements.  Typically you’d wind up building up large strings and passing them to a method like append, html, etc in order to attach them to the DOM.  There are also numerous plug-ins that allow you to structure tags using plain javascript objects.  Aside from being tedious, this method is highly error prone and difficult to debug.  Further, it doesn’t really let you separate your markup from your code, the two things become quickly linked and you wind up with a very messy application.

Recently, I found the Tempest plugin written by Nick Fitzgerald.  It takes a simple, but powerful approach to templatng using JQuery.  The documentation provided on the plugin’s page is pretty straight forward and I encourage you to go read it before continuing on with this article.

The iter Tag

One of the things I really liked about Tempest was that it allows you to implement your own custom tags that can be called inside of a template.  The number of uses for this is infinite, and it is even how Tempest defines its own if tag.

Very early on in my use of Tempest I ran across a scenario that prompted me to create my own tag.  I had an array of javascript objects similar to the following:

[{ tid: '123', team_name: 'Team A', roster: [{ name: 'Sal' }, { name: 'Bob'}] },
 { tid: '321', team_name: 'Team B', roster: [{ name: 'Sally' }, { name: 'Linda' }, { name: 'Thelma'}]}]

As you can see, each object in the collection contains a member called “roster” which is itself a collection of complex objects.  The difficulty I ran into was that I wanted to make the following call to Tempest:

$(function() {
    var teams = [{ tid: '123', team_name: 'Team A', roster: [{ name: 'Sal' }, { name: 'Bob'}] },
                 { tid: '321', team_name: 'Team B', roster: [{ name: 'Sally' }, { name: 'Linda' }, { name: 'Thelma'}]}];
                        
    $('#teams').html($.tempest('team-list', teams));
});

Now, Tempest is smart enough to know that I’ve passed it an array of objects and is therefore going to apply the “team-list” template to each of the elements in the “teams” array.  However, there is no way to tell the template that it needs to iterate over each element in the “roster” array.  This lead me to define the following custom tag:

$.tempest.tags.iter = {
 expectsEndTag: false,
 render: function(context) {
     return $('<span>').append($.tempest(
         this.args[0],
         context[this.args[1]]
     ).clone()).html();
 }
};

In order to call this tag inside of a template I would use the following syntax:

{% iter roster-members roster %}

Where iter is the name of my custom tag, “roster-members” is the name of the Tempest template I want to apply, and “roster” is the name of the property on the context object that contains my list of elements.

What happens here is that when the Tempest template engine see my custom tag, it executes the “render” method that I’ve defined passing it a context object (in this case an element from the teams array).  The template engine will also populate an array called “args” that contains each argument that I’ve passed in my call to the custom tag.

The iter tag’s render method expects the first argument (i.e. args[0]) to be the name of a pre-defined Tempest template.  The second argument (i.e. args[1]) is expected to be the name of an attribute on the context object that contain the collection it is to iterate over.

The “render” method of any Tempest custom tag is required to be a string.  In the case of iter, this string is the result of calling tempest to format some data using a pre-defined template.  The rest of the code wrapping the call to tempest is simply some JQuery hackery that lets us get the entire string of HTML that we are going to return.

 

An Example of using iter

The following is a complete example of using the iter tag as defined above.  If you find yourself using Tempest as much as I have, you would be well advised to create a stand alone Javascript file called “tempesttags.js” in which you define your own library of custome tempest tags.  Include this file after the tempest plugin itself and you should be able to share them from project to project.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Tempest Example</title>
    <script type="text/javascript" language="javascript" src="/Scripts/jquery-1.3.2.js"></script>
    <script type="text/javascript" language="javascript" src="/Scripts/jquery.tempest-0.3.1.min.js"></script>
</head>
<body>
    <script type="text/javascript" language="javascript">

        $(function() {

            $.tempest.tags.iter = {
                expectsEndTag: false,
                render: function(context) {
                    return $('<span>').append($.tempest(
                        this.args[0],
                        context[this.args[1]]
                    ).clone()).html();
                }
            };

            var data = [{ tid: '123', team_name: 'Team A', roster: [{ name: 'Sal' }, { name: 'Bob'}] },
                        { tid: '321', team_name: 'Team B', roster: [{ name: 'Sally' }, { name: 'Linda' }, { name: 'Thelma'}]}];

            $('div#team-holder').html($.tempest('team-details', data));
        });
    </script>
    
    <textarea title="team-details" class="tempest-template" style="display: none;" cols="0">
        <div id="{{ tid }}" class="team-details">
            <span>{{ team_name }}</span>  
            <ul>
                {% iter roster-member roster %}
            </ul>          
        </div>
    </textarea>
    
    <textarea title="roster-member" class="tempest-template" style="display: none;" cols="0">
        <li>{{ name }}</li>
    </textarea>
    
    <div id="team-holder"></div>
</body>
</html>

Thursday, October 29, 2009 #

Back Links

LINQ Overview, part zero

LINQ Overview, part one (Extension Methods)

LINQ Overview, part two (Lambda Expressions)

 

Note: I realize it has been a really long time since I've posted anything.  It is both exciting and humbling that I continue to receive such positive feedback on these articles.  In fact, that is why I am trying to put in the effort and finish off this series before moving on to more recent topics.  This nomad has been on some interesting journeys these past months, and I am really excited to share what I've learned.

Data types, and why they matter

In the world of computer programming data typing is one of the most hotly debated issues.  There are two major schools of thought as far as type systems go. 

The first is static typing in which the programmer is forced by the programming environment to write her code so that the type of data being manipulated is known at compile-time.  The purpose of course being that if the environment knows the type of data being manipulated it can, in theory, better enforce the rules that govern how the data should be manipulated and produce an error if the rules are not followed.  It would be a mistake to assume exactly when the environment knows the types involved, however.  For example, the .NET CLR is a statically typed programming environment but it provides facilities for reflection and therefore sometimes type checking is done at run-time as opposed to compile-time.  Take a look at the following code sample:

static void TypeChecking()
{
   string s = "a string for all seasons";
   double d = s;

   object so = s;
   d = (double)so;
}

If you look at line two, you'll see we are trying to assign a string value to a double variable.  This trips up the compiler since string can not be implicitly cast to double.  This is an example of compile-time type checking and demonstrates why it can be useful as in this case it would have saved us a few minutes of debugging time. 

On line four we are assigning our string value to an object variable.  Since anything can be stored in an object variable we are essentially masking the type of the value from the compiler.  This means that on the subsequent line our explicit cast of so to double doesn't cause a compilation error.  Basically, the compiler doesn't have enough information to determine if the cast will fail because it doesn't know for certain what data type you will be storing in so until runtime.  Of course, since C# is a statically typed language the cast will generate an exception when it is executed.  Don't minimize the damage that this can pose.  What if a line like this was laying around in some production code and that method was never properly tested? You'd wind up with an exception in your production application and that's bad!

The second major school of thought in regards to typing is referred to as dynamic typing and as its name implies, dynamic type systems are a little bit more flexible about what they will accept at compile-time.  Dynamic type systems, especially if you have a solid background in traditionally static typed environments, may be a little harder to grok at first, but the most important thing to understand when working with a dynamic type system is that it doesn't consider a variable as having a type; only values have types.

As with anything in modern programming languages, everything is shades of gray.  For example even within the realm of static type systems there are those that are considered to be strongly typed and those that are considered to be weakly typed.  The difference being that weakly typed languages allow for programs to be written where the exact type of a variable isn't known at compile-time. 

The "why they matter" bit should be self evident.  You simply can not write effective code in a high-level language like C#, Python, or even C without interacting with the type system and therefore you can't write effective code without understanding the type system.  As a language, C# is currently tied to the CLR and therefore its syntax is designed to help you write statically typed code, more often than not code intended to provide compile-time type checks.  However, as the language and developer community using it have evolved there has been a greater call to at least simulate some aspects of dynamic languages.

 

The var keyword

Introduced in C# 3.0 the var keyword can be used in place of the type in local variable definitions.  It can't be used as a return type, the type of a parameter in a method or delegate definition, or in place of a property or field's type declaration.  The var keyword relies on C#3.0's type inference feature.  We first encountered type inference in part one where we saw that we didn't have to explicitly state our generic parameters as long as we made it obvious to the compiler what the types involved were.  A simple example is as follows:

        static void UseVar()
        {
            var list = new List<Book>();
            var book = new Book();

            book.Title = "The C Programming Language";

            list.Add(book);

            foreach (var b in list)
                Console.WriteLine(b.Title);            
        }

In the above we are using var on three separate lines, but all for the same purpose.  We've simply used the var keyword in place of the data type in our variable declarations and let the compiler handle figuring out what the correct type should be.  The first two uses are pretty straightforward and will save you a  lot of typing.  The third use of var is also common, but be forewarned as it will only work well with collections that implement IEnumerable<T>.  Collections that implement the non-generic IEnumerable interface do not provide the compiler with enough information to infer the type of b.

You have to be careful with var as even though the compiler can tell what type you meant when you wrote it, you or the other developers you work with might not be able to in three or four months.  You also have to look out for situations like the following:

        static void UnexpectedInference()
        {
            var d = 2.5;
            float f = d;
        }

As you can see you may have meant for number to be a float, but the compiler decided that you were better off with a double.  Naturally, if you wrote the following the compiler would have more information as to your intent and act appropriately:

        static void UnexpectedInference()
        {
            var d = 2.5f;
            float f = d;
        }

The var keyword is nice and all, but was it added to the language just to stave off carpal tunnel? No, they needed to provide the var keyword so that they could give you something more powerful, anonymous types.

 

Anonymous Types

To look at why var was introduced into the language, lets look at the following program.

    class Program
    {
        static void Main(string[] args)
        {
            var books = new List<Book>() {
                new Book() { 
                    Title = "The Green Mile",
                    Genre = "Drama",
                    Author = new Author() {
                        Name = "Stephen King",
                        Age = 62,
                        Titles = 1000
                    }
                },
                new Book() {
                    Title = "Pandora Star",
                    Genre = "Science Fiction",
                    Author = new Author() {
                        Name = "Peter F. Hamilton",
                        Age = 49,
                        Titles = 200
                    }
                }
            };

            var kings = from b in books
                       where b.Author.Name == "Stephen King"
                       select new { Author = b.Author.Name, BookTitle = b.Title };

            foreach (var k in kings)
            {
                Console.WriteLine("{0} wrote {1}", k.Author, k.BookTitle);
            }            

            Console.ReadLine();
        }
    }

    public class Author
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public int Titles { get; set; }
    }

    public class Book
    {
        public Author Author { get; set; }
        public string Title { get; set; }
        public string Genre { get; set; }
    }

The above code is pretty straight forward so let's focus on the LINQ query :

           var kings = from b in books
                       where b.Author.Name == "Stephen King"
                       select new { Author = b.Author.Name, BookTitle = b.Title };

Do you notice anything interesting? WOAH! We just used the new operator to instantiate an object without saying what type it was, in fact, you couldn't tell the compiler what type it was even if you wanted to.  If you can't write out the name of the type, then how are you supposed to declare a variable of that type? Oh, right, we have the var keyword which uses type inference to figure out the correct data type to use!

So, how does C# 3.0 provide such a cool feature? It is supposed to be a statically typed language, right? Well, the answer to that isn't actually complicated in the least.  Think about it.  What do compilers do? They read in a program definition and then generate code in, typically, a lower level language like machine code or CLR byte code.  We have already established that type inference is being used to determine the correct data type for our var variable above, but the obvious problem is that we didn't define the type it winds up using.  After reading in our query the compiler can tell we are defining a class that has two properties, Author and BookTitle.  Further, it knows we are assigning System.String values to both properties.  Therefore, it can deduce the exact class definition required for our code to work in a statically type checked way.  If you were to fire up reflector you'd be able to find the following class definition:

    [DebuggerDisplay(@"\{ Author = {Author}, BookTitle = {BookTitle} }", Type="<Anonymous Type>"), CompilerGenerated]
    internal sealed class <>f__AnonymousType0<<Author>j__TPar, <BookTitle>j__TPar>
    {
        // Fields
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private readonly <Author>j__TPar <Author>i__Field;
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private readonly <BookTitle>j__TPar <BookTitle>i__Field;

        // Methods
        [DebuggerHidden]
        public <>f__AnonymousType0(<Author>j__TPar Author, <BookTitle>j__TPar BookTitle);
        [DebuggerHidden]
        public override bool Equals(object value);
        [DebuggerHidden]
        public override int GetHashCode();
        [DebuggerHidden]
        public override string ToString();

        // Properties
        public <Author>j__TPar Author { get; }
        public <BookTitle>j__TPar BookTitle { get; }
    }

First of all, the above looks pretty funky.  In fact, if you copy and paste it into visual studio it isn't going to compile.  The import thing to realize is that the class was built for you at compile-time not at runtime.  Another interesting point is that the compiler is smart enough to reuse this class if your code calls for a second anonymous type with the same properties.

All in all, anonymous types work like any other class with the same constraint (i.e. internal sealed).  See, I told you that this stuff isn't magic!

 

LINQ Tie In

Anonymous types are cool and all, but due to the restrictions on their use I am sure you'll find, as I have, that they are most useful in LINQ queries.  The way in which you would use an anonymous type in conjunction with LINQ is as follows:

           var kings = from b in books
                       where b.Author.Name == "Stephen King"
                       select new { Author = b.Author.Name, BookTitle = b.Title };

As you can see above, we are using an anonymous type in the select part of the query to define what our result set looks like.  In database terminology this is called a projection, and our example above is really no different than selecting a subset of columns from a table in a relational database.

At this point you may be saying, "so what? In the example you could have just selected a book object and gotten access to the author using normal dot notation".  You'd be correct in that instance, however consider the LINQ query below:

            var kings = from b in books
                        join a in authors on b.AuthorID equals a.ID
                        where a.Name == "Stephen King"
                        select new { Author = a.Name, BookTitle = b.Title };

In the above, we are essentially joining two collections of in memory objects on what amounts to a foreign key, i.e. they both know the ID of the author.  Hopefully you can now see the utility of anonymous types.  If we had simply selected the books, we would then need to do a subsequent query in order to find the corresponding author.

The only other place I see a lot of value in using anonymous types is to flatten out a set of related objects for the purpose of databinding or other UI rendering.

 

Summary

We have now examined the anonymous type feature added to C#3.0 as well as how to use the new var keyword.  It is my guess that you'll find yourself using var quite frequently, but only if you do a lot of LINQ will you being using it for anonymous types.  The next segment will focus on the actual architecture of LINQ, once you understand that there really isn't anything mysterious left.


Saturday, January 03, 2009 #

If you are like me, you may be surprised to find that your colleagues don't know some fundamental things about Generics in .NET. 

I just began working on a new project at work and during the design phase I was assured that our company had a great API full of extensions for a commercial content management solution.  This API, I was told, would revolutionize how I used the product and take a lot of burden off my team of developers (some of which had used the API on previous projects and thought it was spot on).  Being the trusting lad that I am, I agreed to this site unseen (lesson learned I assure you).  The following snippet of code exemplifies the use of .NET Generics throughout the API and represents the first instance of code smell that would spiral into sleepless nights of refactoring the API like a doctor fighting gangrene:

public class ObjectFactory<T>
    {

        public static T CreateObject()
        {

            T obj = (T)typeof(T).GetConstructor(new Type[0]).Invoke(new object[0]);

            return obj;

        }

    }

As you can see from the above we not only have an example of a misunderstanding of Generics, but also one that displays very unsafe practices in reflection.  For those of you that don't wish to interpret the code above the line in question:

T obj = (T)typeof(T).GetConstructor(new Type[0]).Invoke(new object[0]);

is simply using reflection to get a reference to T's default constructor and then invoking it in order to instantiate an object of type T.  This is all well and good, but what happens if T doesn't have a default constructor?  In the snippet shown above such a situation would result in a NullReferenceException being thrown at runtime.  In this case a NullReferenceException, although accurate, isn't semantically correct since it doesn't tell you what the true error is (i.e. T does not have a default constructor).  The following code still uses reflection, but is rewritten to be a bit more semantically correct:

public static T CreateObject() {

   ConstructorInfo cons = typeof(T).GetConstructor(Type.EmptyTypes);
   T obj = default(T);

   if(cons == null)
       throw new NoDefaultConstructorException();

   try
   {

        obj = (T)cons.Invoke(null);

   }
   catch (Exception ex)
   {

        throw new Exception("Exception encountered while invoking default constructor", ex);

   }

   return obj;

}

Awesome. We now do some basic error checking and throw a more appropriate exception.  This is still not ideal because our application can only detect the larger error, the fact that someone is using a T that has no default constructor, at runtime.  It would be much better if we push the burden of checking this fact to the compiler.  In order to do this we make use of .NET Generic Constraints as so:

 

public class ObjectFactory<T> where T : new()
    {

        public static T CreateObject()
        {

            return new T();

        }
    }

}
 

If we write a simple program to use our ObjectFactory, we can see that specifying a class that does not provide a default constructor will now generate a compiler error:

class Program
    {
        static void Main(string[] args)
        {

            HasDefaultConstructor obj = ObjectFactory<HasDefaultConstructor>.CreateObject();
            NoDefaultConstructor noCons = ObjectFactory<NoDefaultConstructor>.CreateObject();

            Console.WriteLine("Done");
            Console.ReadKey();

        }
    }

Much better! 

There is a relatively small number of Constraints that can be applied and it is how one chooses to combine them that makes them powerful.  The full list of constraints can be found on MSDN.


Wednesday, February 13, 2008 #

Wow. Turns out this thing might be useful to more than just me.  Anyway, I've added P/Invoke calls to allow access to the WinPCap functions that are specific to Windows as well as the packet filtering functions.  I've added a port of the packet filtering example as well.  I can't say I've tested all the calls yet, regardless, at this point I am going to start moving up to a higher level.  I think we should now have access to almost all the calls available in the native WinPCap, but feel free to leave a comment to let me know otherwise.

Download Solution - Nomad.Net.PacketCapture.zip


Thursday, January 31, 2008 #

As I stated in my last post, I am currently geeking out on packet capture software.  The WireShark network analysis tool is pretty awesome and is built using the WinPCap library which is itself a port of libpcap to the Win32 environment.  The unfortunate part, or at least for us .NET developers, is that there is no (IMHO of course) good .NET binding to WinPCap.  Well, I've gone ahead and spent some time getting an initial wrapper done using P/Invoke.

I've taken a different initial strategy from the other project I found and I think it makes more sense in the long run.  The other project attempts to provide a more elegant binding by wrapping the native functions of WinPCap in a couple of simple objects.  This isn't necessarily bad, however the author decided to hide the actual P/Invoke calls, which means if you (like me) don't like his class structure, you can't simply use the P/Invokes directly to build your own.  So, I went ahead and I wrote my own set of P/Invoke calls to bind to WinPCap.

I tried to stick to the following rules:

  1. All P/Invoke method name exactly match the native function names
  2. All managed type names used in marshalling exactly match the native struct names
  3. All method parameters in the P/Invokes have the same name as in their native functions
  4. Marshal parameters as closely as possible (i.e. don't just use IntPtr for everything)

Of course, I couldn't stick to the rules 100% due to some technical limitations, but in most cases it is obvious where I deviated.

We should see the following advantages from this:

  1. All documentation already in existence for WinPCap is still 100% relevant.
  2. All C/C++ code samples using the native WinPCap can be ported (more or less) directly into C# code.
  3. Developers now have the freedom to build their own tools using WinPCap in .NET using whatever class structure they'd like

The Library

Aside from the supporting structs that are used to marshal the unmanaged types, there are only two classes you need to be concerned with in the library.

WinPCapConstants - Holds the constants translated from the #defines of the pcap.h header file

WinPCapDriver - Static class that holds all P/Invoke declarations used to bind to WinPCap.  All functions except those that are listed in the WinPCap docs as Windows specific or dealing with packet filtering are currently available.  The Windows specific functions and packet filtering functions will be added as the project progresses.

 

There is also one handy extension method that I added to help with marshalling the various data structures used by WinPCap.  It is defined as follows:

namespace Nomad.Net.PacketCapture.Interop
{
    public static class IntPtrExtensions
    {

        public static TStruct AsStruct<TStruct>(this IntPtr ptr)
        {

            return (TStruct)Marshal.PtrToStructure(ptr, typeof(TStruct));

        }

    }
}

Normally, we'd have to write something like the following to marshal a pcap_if structure from unmanaged to managed code:

pcap_if nic = (pcap_if)Marshal.PtrToStructure(nicPointer, typeof(pcap_if));

This is pretty heinous...In C we would have been able to just cast or dereference the pointer.  With the extension method above, the code becomes more readable:

pcap_if nic = nicPointer.AsStruct<pcap_if>();

It isn't all that much shorter in terms of length, but it reads much more like English.

 

Going Forward

My next step is to continue porting the original WinPCap examples from C to C#.  This will help me test the P/Invoke calls and learn more about WinPCap and how it is supposed to operate.  After that I will create my own higher-level wrapper using C# to abstract the details.  Again, the best part of all this is that if you don't like my high level code then you should still at least be able to access WinPCap via .NET, albeit in a more raw form.

The following code should work, but you first need to install WinPCap.  I also recommend you download the developer pack so you can compare the native C examples with the couple of .NET ones I've done so far. The differences are relatively minor, but important since they mostly have to do with features/limitations of mixing C and .NET.  Also, the HTML based documentation for WinPCap is still valid, and I have yet to put much in the way of comments in my code. 

Consider the following downloads as Alpha quality at best:

Download Solution - Nomad.Net.PacketCapture.zip


Wednesday, January 30, 2008 #

In my copious amount of free time I've been messing around with network analysis and security.  I've always been generally interested in networking technology, but have never really had much practical exposure to it.  Sometimes, however, it is nice to be able to analyze a network and see what kind of information is actually coming across the wire.  In my last article I mentioned a tool called WireShark which is a free, open source network analyzer aka packet sniffer.

WireShark is a great tool and has its own set of extension points, but I wanted lower level access to the packets being captured.  My understanding of the politics and genesis is lacking, but it seems like the WinPCap library is the Windows version of the libpcap packet capture library from the *NIX world.  Naturally, WinPCap is coded in C and even though I have some background in it, the tool I am looking to develop requires a lot of UI work.  Instead of stepping back into the land of MFC/Win32, I tried to locate a Managed version of WinPCap.  The closest thing I could find was this Ancient Project on CodeProject.com.  It hasn't been updated since 2003 and isn't a "fully" managed wrapper (also, the source code in the download is just to the example, not the wrapper).

I figured, "If this guy can do PInvoke, so can I".  Thus, I downloaded the WinPCap developer pack and attempted to open the example solution in Visual Studio 2008.  Visual Studio 2008 alerted me to the fact that I had to upgrade the project (which was actually a VS 6.0 .dsw file) and I happily agreed.  The upgrade went smooth so I attempted to compile the solution, but received the following error:

"error C3163: '_vsnprintf': attributes inconsistent with previous declaration    c:\program files\Microsoft visual studio 9.0\vc\include\stdio.h    358    savedump"

Crap. Apparently this is a common problem when compiling older C++ code with the Visual Studio 2008 C++ compiler.  Now, I didn't find a solution for this on the net specific to WinPCap, but several forum posts across other projects lead me to the following solution.

First, find the pcap-stdinc.h file on your system. It should be located in: "...\WpdPack_4_0_2\WpdPack\Include"

Next, locate the following code near the bottom of your header:

#define snprintf _snprintf
#define vsnprintf _vsnprintf
#define inline __inline

The problem, as we can tell from the compiler error, is that the "#define vsnprintf _vsnprintf" causes some incompatibilities with what is already in stdio.h.  Modify your code to the following and save the header:

#define snprintf _snprintf

#if !defined( __MINGW32__ )
# if _MSC_VER < 1500
    #define vsnprintf _vsnprintf
# endif
#endif

#define inline __inline

You should now be able to compile all the examples in the solution!

All that we've done is check the version of the compiler at compile time.  If the version is prior to MSC++ 9.0 then we go ahead and do the #define.  Otherwise, we don't do the #define and rely on what is in stdio.h.

This solution is general in nature, i.e. anything that defines _vsnprintf may exhibit this issue, but specific in the sense that the exact location of the code to modify will vary by project.  In the case of WinPCap, everything is groovy at this point.  Now I just need to learn everything I can about PInvoke : )

 

Tuesday, January 29, 2008 #

Download Solution - OfflineHtml.zip

So, one of the cool controls available to us in WinForms is System.Windows.Forms.WebBrowser.

The WebBrowser control is essentially a managed wrapper around some COM interfaces that bind to Internet Explorer and provides us with several interesting capabilities.  First of all, one can use WebBrowser to easily display a web page in a WinForms application.  All you have to do is set the WebBrowser.Url property and the control takes care of getting the assets from across the wire and rendered on the screen.

WebBrowser also exposes some interesting events that allow a programmer to react when a document is loaded, navigation is peformed, etc.  There are probably a ton of places, including MSDN, where you can get that kind of information so I won't go over it here.  Instead, I am going to show something that isn't immediately obvious, but that I believe I found a clean solution to.

 

The Task

What I want to do is load an HTML page that is on my local computer without causing any network traffic, e.g. it won't load images on the page.  Similar to say, loading a web archive in Internet Explorer.  For our purposes let's use the Google Home Page as an example.

 

The First Attempt

I immediately set upon this task thinking it would be pretty easy.  From what I had gathered on MSDN, after loading a page the WebBrowser control's Document property is populated with an HtmlDocument object.  Similar to System.Xml.XmlDocument, HtmlDocument is a tree like representation of the web page's HTML DOM and it exposes some handy properties for manipulating the HTML elements rendered by the WebBrowser control.  For example, the following code demonstrates setting all of the "src" attributes of the HtmlDocument's img tags to the empty string:

public HtmlDocument StripImageLoading(HtmlDocument document)
{

    foreach (HtmlElement image in document.Images)
       image.SetAttribute("src", string.Empty);
            
    return document;

}

Iterating over the various HtmlElementCollection objects exposed through HtmlDocument's properties allows one to alter, and even add, HTML elements. 

This is great, but how do we actually get the WebBrowser control to load an HtmlDocument for us?  There are three primary methods, each of which I'll demonstrate with a code snippet.

 

Setting WebBrowser.Url:

public void LoadPage()
{

    WebBrowser browser = new WebBrowser();
    browser.Url = new Uri("http://www.google.com");

}

The "primary" way to load a page is to set WebBrowser.Url to a valid Uri object.  When this is done the WebBrowser will get all required data for the page via HTTP and render the results into our HtmlDocument (accessible via the WebBrowser.Document property).

 

Setting WebBrowser.DocumentText:

public void LoadPage()
{

    WebBrowser browser = new WebBrowser();
    browser.DocumentText = @"<html><img src=""http://www.domain.com/someimage.gif""</html>";
}

This is the first method that would enable us to achieve our offline viewing goal.  We simply set WebBrowser.DocumentText with a string of HTML and the control uses that to render the page.  The issue with this method is that any HREFs or SRC attributes will be resolved by the WebBrowser control.  In otherwords, in the above example the image file referenced in our <img> tag will actually be downloaded and rendered into the page on the screen.  This, is clearly not what we want.

 

Setting WebBrowser.DocumentStream:

public void LoadPage()
{

    WebBrowser browser = new WebBrowser();
    FileStream source = new FileStream(@"C:\page.html", FileMode.Open, FileAccess.Read);

    browser.DocumentStream = source;

}

This method allows us to access our page as a Stream.  The WebBrowser control will load the data from the Stream and again, render it into an HtmlDocument object.  Like the DocumentText property, however, it will resolve any HREF or SRC attributes and get the resources from the web.

 

The Hurdle

What we need to do at this point should be clear: we need to some how modify the HtmlDocument prior to the WebBrowser control rendering it on the screen.  I figured there would be an event exposed for this, seemingly obvious, desire.  I looked into the following events, hoping for a quick solution:

WebBrowser.DocumentCompleted - This event is fired AFTER the page is fully rendered, so it unfortunately doesn't help up.  We can still modify the HtmlDocument at this point, but since any referenced resources have already been downloaded, it is of little value in our situation.

WebBrowser.ProgressChanged - This event is fired as the page and its resources are being gathered.  It is fired asynchronously, so be very careful when using it.  That being said, I figured initially that I could wait for progress to be 100% and then I'd modify the document.  Unfortunately, this too did not work.

WebBrowser.FileDownload - Aside from DocumentCompleted, this seemed the most promising.  After all, perhaps I can check to see if the file being downloaded is an image, and if so, simply cancel the download.  No, that won't work because the FileDownload event simply takes an "EventArgs" parameter and therefore gives us no meaningful state on which to operate.

 

So, at this point we have no way of using events to accomplish our task.  We have to find another way.  As most developers do, I scanned the net to find out if this problem had already been cracked.  I didn't find an exact solution, but I did find something that helped at least spark my imagination.  I point you now to the blog of Jim Holmes.  I kind of know Jim a little from when I lived in Ohio and went to a few Dayton .NET Users Group meetings (of which Jim was/is the President).  Now, Jim is a very smart guy (in fact he has a great O'Reilly Book out right now) so I'm not sure what happened, but in his article I think he makes a few mistakes about how the WebBrowser control works and I will point those out when we come to them.  Like  I said though, his article at least sparked something in my mind: How do I get an empty HtmlDocument without going through the WebBrowser control?

 

The Solution

What we want is to load an HTML page from the local system without causing any actual network traffic.  To make our example more simplistic let's just say we don't want images to load at all.  My solution, for .NET 3.0/3.5 at least, is to introduce an Extension Method for the WebBrowser control that allows us to arbitrarily "filter" the HtmlDocument prior to loading it.  The entire solution is available for download at the beginning of this article, so I've chunked it up a bit for display purposes:

public static class WebBrowserExtensions
{

    /// <summary>
    /// Load an HTML document from a Stream and pass the text through a filter before the page is
    /// rendered in the WebBrowser control.
    /// </summary>
    /// <param name="browser">control that renders the filtered HTML</param>
    /// <param name="source">Stream containing the content to filter and render</param>
    /// <param name="filter">Delegate used to filter the source Stream</param>
    public static void ProcessRequest(this WebBrowser browser, Stream source, Func<HtmlDocument, HtmlDocument> filter)

 

As we know, Extension Methods must be defined in static classes, as public static members.  You can see the prototype for the ProcessRequest extension about.  It takes two parameters a Stream object that contains the "source" of the page and a delegate that takes an HtmlDocument and returns a modified HtmlDocument.

    using (WebBrowser tempBrowser = new WebBrowser())
    {

        //all data from the source as a string
        string sourceText = string.Empty;

        try
        {

            //read all the data from the source Stream
            using (StreamReader sourceReader = new StreamReader(source))
            {

                sourceText = sourceReader.ReadToEnd();

            }

        }
        catch (IOException ex)
        {

            throw new Exception("Could not read data from source stream", ex);

        }

It is important to note that the WebBrowser control is an absolute resource hog, so please use a using statement or other disposal pattern to property clean it up.  Also, we could have performed all of the operations in this method using the WebBrowser control we were given, but the drawback to that is the control would fire any registered event handlers.  We want our manipulation of the HtmlDocument to be as seamless as possible, and thus we operate on a temporary WebBrowser control. 

The above chunk of code also performs the mundane task of reading the entire Stream into a string and propagating any exceptions up the stack.

        //process any text we read from the source Stream
        if (!string.IsNullOrEmpty(sourceText))
        {

            HtmlDocument tempDocument = null;
            HtmlElement htmlRoot = null;            
            
            //navigate to "about: blank" to initialize an empty document
            tempBrowser.Navigate("about: blank");

Now, the above code contains something that Jim tells us to do which is navigate our browser to "about: blank".  As Jim states, correctly, this causes the HtmlDocument object to be created and initially empty.  Exactly what we want, in fact.  However, Jim also seems to imply that this step is always necessary prior to setting either the WebBrowser.DocumentText or WebBrowser.DocumentStream properties.  As the MSDN Documentation for DocumentText points out, WebBrowser will automatically navigate to "about: blank" each and every time either of these properties is set.

The reason that we are doing this is that we don't WANT to set DocumentText.  Remember, that will cause all of our resources to be loaded!  All we are trying to do is get an empty HtmlDocument object!

            //load the sourceText into the document.
            tempBrowser.Document.Write(sourceText);

Now that we have navigated to "about: blank", we can use the WebBrowser.Document property to access an empty HtmlDocument.  Further, we can use the HtmlDocument.Write method to populate the document with our HTML.  This is looking pretty nice so far!

            //now filter the document if a filter was specified
            if(filter != null)
                tempDocument = filter(tempBrowser.Document);

            //if the filter did not return a document, or no filter was specified, use the original document
            if (tempDocument == null)
                tempDocument = tempBrowser.Document;

The code from here on out is pretty standard.  We are applying any filter we've been given and keeping track of our temporary HtmlDocument object as it is being modified.

 

            //find the root HTML element, there can be only one!
            var htmlElements = tempDocument.GetElementsByTagName("html");

            if (htmlElements != null && htmlElements.Count > 0)
                htmlRoot = htmlElements[0];

            //now, extract the text and set it on the actual browser
            browser.DocumentText = htmlRoot.OuterHtml;

To wrap this method up, we get the root <html> tag and then set the WebBrowser.DocumentText property of the WebBrowser control we were given to the <html> tag's OutHtml (i.e. everything in the document including HTML tags and content).

By setting the DocumentText property, we are forcing the WebBrowser control to load our modified document.  We have accomplished our goal.  We can now modify the HtmlDocument BEFORE it gets rendered.

The Final Bits

For the sake of completeness, let's use the StripImageLoading method we created earlier to modify a "local" page:

public partial class MainForm : Form
{
    public MainForm()
    {

        InitializeComponent();

        //get google's home page
        FileStream source = new FileStream(@"C:\Development\VS2008\OfflineHtml\google.html", FileMode.Open, FileAccess.Read);

        //process the request
        mainBrowser.ProcessRequest(source, StripImageLoading);

    }

    public HtmlDocument StripImageLoading(HtmlDocument document)
    {

        foreach (HtmlElement image in document.Images)
            image.SetAttribute("src", string.Empty);
        
        return document;

    }

}

The above class opens a saved HTML file that contains the source HTML of the Google home page.  It then uses our ProcessRequest extension method to filter the HtmlDocument using the StripImageLoading method as its delegate.  The result when you run the code should be a missing image on the page. If you want to, go download a network analyzer like WireShark to confirm that no HTTP requests are being made as a result of rendering the page.

 

Summary

WebBrowser control is pretty cool.  It has a lot of useful features out of the box and is quite extensible.  In this article you've seen its basic usage and a slightly more advanced scenario for which new .NET 3.5 capabilities provide an extremely clean solution.  In fact, it is probably the first time I've really gotten an "Oh yeah! This feels right" when using extension methods outside of LINQ.  Of course, pretty much the same code will compile and work in a .NET 2.0 environment, you'll just have to comment out the "this" modifier in front of the first parameter of the extension method along with any code that uses it, i.e. turn ProcessRequest into a vanilla static method.


A couple of readers (at least one of which was thankfully vocal) complained about the blog's style.  I agree it is/was/will be pretty lame.  I am using the templates provided by geekswithblogs and I don't really have the time to create my own yet.  Let's see how things progress with this new updated style.

-Newman