Archive

Archive for the ‘.NET’ Category

Code Challenge Results

September 16, 2010 4 comments

Thank you to all to those that submitted code solutions. We will be running another code contest in a few weeks. Check back soon to enter our next contest. Congratulations to our contest winner: Rob Howarth

See the bottom of this page for an example solution

Others who submitted correct solutions

  • Rob Howarth
  • Scott Monnig
  • Eric Haddan
  • Thomas Eyde
  • Imran Baloch
  • Marc Chu
  • Cesar Neves
  • Jaider Ariza
  • Kenneth Hall
  • Teymur Mammadov
  • Cam Luc
  • Peter Burrell
  • Anthony Clayton
  • Mark Lindell
  • Steve Smith
  • Chris McKenzie
  • Amit Parikh
  • David White
  • Ron Warholic
  • Josh Clark
  • James Baker
  • Richard Deeming
  • Erik Jõgi
  • MORRIS CRISTOPHER
  • Scott Holodak
  • James Curran
  • Marius Ene
  • Matt Crouse
  • Michel Grootjans
  • Daniel Griffie
  • Xiu
  • Mordechai Sandhaus
  • Stuart Cam
  • Raj Rao
  • Rafik Ramtoolah
  • Ali Derman
  • Eric King

Sample Solution

        public class Converter
        {
                public string Convert(int number)
                {
                        StringBuilder buffer = new StringBuilder();
                        int quotient = number;
                        int remainder = -1;
                        while (quotient != 0)
                        {
                                remainder = quotient % BaseNumber;
                                quotient = quotient / BaseNumber;
                                buffer.Insert(0, ConvertSingleBaseNumber(remainder));
                        }
                        return buffer.ToString();
                }

                protected virtual string ValidCharacters
                {
                        get { return "0123456789xyz"; }
                }
               
                private string ConvertSingleBaseNumber(int remainder)
                {
                        return ValidCharacters[remainder].ToString();
                }
               
                private int BaseNumber
                {
                        get
                        {
                                return ValidCharacters.Length;
                        }      
                }
        }

        public class BinaryConverter : Converter
        {
                protected override string ValidCharacters
                {
                        get
                        {
                                return "01";
                        }
                }
        }

        public class OctalConverter : Converter
        {
                protected override string ValidCharacters
                {
                        get
                        {
                                return "01234567";
                        }
                }
        }

        public class HexConverter : Converter
        {
                protected override string ValidCharacters
                {
                        get
                        {
                                return "0123456789abcdef";
                        }
                }
        }

Did you like this challenge? Maybe you found it easy? Were always looking for talented people to join our growing team. Take a look at what we do at http://www.thycoticsolutions.com/careers.html If you feel you have what it takes to work at Thycotic – submit a cover letter, resume, and solution to this problem to tddjobs@thycotic.com

Tidbits in dotNET Framework 4

June 30, 2010 2 comments

Kevin:Tidbits of .NET

June 30th | 2010

.NET Tidbits

Every now and then while exploring the .NET Framework 4 I come across something small, but very useful. In no particular order, here is a list of the top 10 new small changes to the framework that may help.

1. Environment.Is64BitOperatingSystem

This is a huge timesaver. Doing this previously required Platform Invoke to do properly (IntPtr.Size doesn’t work if your assembly is compiled for x86 only). Likewise, it has an additional helper Environment.Is64BitProcess. They aren’t something that will get used on a daily basis, but it’s great to have them when you need them.

2. System.Numerics

This is a namespace in a new assembly called System.Numerics. It contains two very useful types: BigInteger and Complex. BigInteger, like it sounds, is big. Unlike the primitives that already existed, such as Int32, Int64, etc; this has absolutely no limit to how big the number can be.Possible applications are calculators and cryptography when dealing with large prime numbers. Complex is a complex number that is used to represent number with an imaginary component, such as the square root of -1. The ToString implementation of this is disappointing. I would expect it to be “a + bi” notation, but rather uses “(a, b)”.

3. System.Lazy<>

This is class to better support lazy initialization of objects. A typical pattern would be a service locator that may or may not get all of its services used. Rather than the service locator setting everything up on startup, you can use the Lazy class internally to create the service when it is needed. It even has a constructor overload for thread safety. Here is a snippet:

public class ServiceLocator
{
    private readonly Lazy<IMyService> _lazyMyService
        = new Lazy<IMyService>(() => new MyService(), true);
    public IMyService MyService
    {
        get
        {
            return _lazyMyService.Value;
        }
    }
}

4. IEnumerable.Zip extension

A new extension method that is a part of LINQ is Zip. This allows you to merge two sequences together based on a predicate. It’s quite simple and was something that many considered an oversight in the original release of LINQ. Oversight or not, here it is!

5. Enum.TryParse

Ever been given an enumeration value in the form of a string, and needed to parse it in a safe way? Well now you can with the introduction of Enum.TryParse. It will not throw an exception like Parse will, and follows the same semantics as Int32.TryParse by returning a Boolean to indicate if it was successful or not, and using an “out” parameter to actually return the value.

6. System.Tuple

Many developers have been asking for Tuple support. A Tuple is an immutable structure that can contain two or more elements. Simple as that. They are created using Tuple.Create(), and support up to eight items. More dynamic languages, including the new F# language, make heavier use to tuples and are created using their own syntax.

7. System.Collections.StructualComparisons

Ever want to compare two arrays to see if they were the same? Sounds simple up front, but usually requires some sort of loop or LINQ operation. Now it’s possible to compare two arrays to see if their contents are the same, not if they are the exact same array. Here is snippet to illustrate the use:

var strings1 = new[] {"Hello", "World!"};
var strings2 = new[] {"Hello", "World!"};
int compared = StructuralComparisons.StructuralComparer.Compare(strings1, strings2);

In this case, the comparer returns 0 because there is no difference between the two arrays.

8. System.IO.File.ReadLines

ReadLines is a helpful method that allows you to lazily enumerate over the lines of a text file, rather than using ReadAllLines which isn’t efficient for large text files.

foreach(var line in File.ReadLines(".\\myfile.txt"))
{
    //parse the line and move on to the next one
}

9. MemoryMappedFile

This class is part of the System.IO.MemoryMappedFiles namespace. Its purpose is to provide a managed implementation of a memory mapped file. This is often desirable when working with extremely large files and remaining efficient. To do the same in previous versions of the .NET Framework, you would have to relied on platform invoke.

10. System.Net.Mail.SmtpClient.Dispose

This is more of a “gotcha” than a feature, but it also cleans up a known issue in the .NET Framework. The SmtpClient now implements IDisposable. This gives you control over when the SmtpClient sends a QUIT message to the SMTP server. If this isn’t called after you complete sending emails, it will leave the connection open and may result in unexpected behavior.

Kevin Jones is a Team Lead at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product. On Twitter? Follow Kevin

Code Contracts in dot NET

June 24, 2010 Leave a comment

Kevin Code Contracts

June 24th | 2010

Code Contracts in dot NET

It is often desirable for a developer, particularly those who make public APIs, to communicate to 3rd party developers what their code expects. For example, say I have a product that allows plugins to be written for it. A plugin can call an API I provided, “Print”. Print takes a single string, however, the string should never be null.
Nonetheless, we can’t guarantee a developer will never pass in null; there may be a bug in their application. To prevent serious errors from occurring, we need to ensure our parameter is never null. We can do this with an exception.

public void Print(string data)
{
    if (data == null)
        throw new ArgumentNullException("data",
"This parameter must not be null.");
    //Implementation omitted
}

This is a very typical pattern but it leaves us wanting more. The developer won’t know he is giving the API a string that cannot be used until he attempts to run the application. Nothing is stopping the compiler from compiling.
Statically checking if our code is violating a contract is a powerful feature. An experimental language called Spec# currently supports this but unfortunately, being experimental, it makes it difficult to use in real-world scenarios.
Enter Code Contracts. Now built into the .NET Framework 4, all you need to do is download an add-on for Visual Studio 2008 or 2010. In the .NET Framework 4, the magic happens in the System.Diagnostics.Contracts namespace. You can download the add-on from here:
http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx
Once installed, you will see a new tab on your project’s properties called “Code Contracts”.

I’ve configured a few key options here. I’ve set the “Perform Runtime Contract Checking” to Full, and checked the “Perform Static Contract Checking”, “Check in Background”, and “Show squigglies”. Next, we need to define the contracts. We can use the Contract class to indicate these contracts. Our method now looks like this:

public static void Print(string data)
{
    Contract.Requires<ArgumentNullException>(data != null);
    //Implementation omitted
}

This will have the same behavior as before: If data is null, then an ArgumentNullException is thrown. However, in addition to the runtime failure, we will also see a warning when we compile and squigglies under the code.

And of course, if we set “r” to something not null, the contract is happy! However, what happens if r is getting data from somewhere else?

static void Main(string[] args)
{
    string r = SomeMethodThatWillNeverReturnNull();
    Print(r);
}

private static string SomeMethodThatWillNeverReturnNull()
{
    return "notnullstring";
}

The contract analyzer isn’t small enough to check and see what the method actually does. However, the method could potentially return null. We know better than that, so we can tell the Code Contract analyzer that the return value is never null. This is done with a post condition using Contract.Ensure. A post condition is a contract that ensures a condition is met when the method is exiting. In our case, we want a post condition that says “SomeMethodThatWillNeverReturnNull” won’t return null.

private static string SomeMethodThatWillNeverReturnNull()
{
    Contract.Ensures(Contract.Result<string>() != null);
    return "notnullstring";
}

This tells us that the result of the method will never return null. The call to Print is now satisfied knowing that its source of data cannot return null. If SomeMethodThatWillNeverReturnNull could return null, then the post condition will fail.
Let’s go over what we are seeing. The Contract.Requires indicates that the Print method requires its data parameter as not null, before the method is actually executed. If the condition fails, an exception is raised at runtime, and a warning is generated at compile time.
Contract.Ensures is a post condition. It indicates that before the method exits, that condition must be met. Though it is a post condition, it’s still recommended that the constraint be placed at the top of the method. Let’s say we need our code to meet this contract:

  1. Print must never accept a null string.
  2. Print must never accept a string that is greater than 1000 characters.
  3. When print is called, we must set dataWasPrinted to true.

Here is what our contract looks like:

class Program
{
    private static bool dataWasPrinted = false;

    static void Main(string[] args)
    {
        string data = GetData();
        Print(data);
    }

    private static string GetData()
    {
        Contract.Ensures(Contract.Result<string>() != null);
        Contract.Ensures(Contract.Result<string>().Length <= 1000);
        return "data from file";
    }

    public static void Print(string data)
    {
        Contract.Requires<ArgumentNullException>(data != null);
        Contract.Requires<ArgumentNullException>(data.Length <= 1000);
        Contract.Ensures(dataWasPrinted == true);
        try
        {
            //Implementation omitted
        }
        finally
        {
            dataWasPrinted = true;
        }
    }
}

This is a powerful concept and can be extremely useful. I wouldn’t recommend putting contract validation on all of your code, rather just public methods that will be exposed to others. I might also recommend using it in critical cases where a specific contract must be met. It isn’t a replacement for unit tests or mock testing, rather another tool in the belt that makes a software developer’s life easy.

Kevin Jones is a Team Lead at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product. On Twitter? Follow Kevin

Categories: .NET, Custom Development

Observable Collections in .NET Framework 4

April 30, 2010 1 comment

Observable Collections in .NET Framework 4

April 30th | 2010

Observable Collections in .NET Framework 4

The observable pattern in software development has been something of interest to many. A former co-worker of mine called it “Hollywood programming: Don’t call us, we’ll call you”. It’s a feature that many Aspect Oriented Programming (AOP) models give, and many have hand-rolled their own.

An observable collection simply allows an object to know when the collection has changed, possibly through an event. This puts the responsibility on the collection to notify all of its listeners for changes, rather than the listeners checking periodically if the collection has changed. If you were to hand-roll your own, you might implement IList<T> on your own, and have your implementation raise an event on itself when Add is called.

Starting in the .NET Framework 4, these have been provided to you in a fairly straight-forward fashion. A new class called ObservableCollection<T> in the namespace System.Collections.ObjectModel does the heavy lifting for you. It contains an event called CollectionChanged that is raised when something changes on your collection. Its delegate contains two items. First is “sender”, which is the collection itself. The second is the “args”, which has a lot of interest to us. It is of type NotifyCollectionChangedEventArgs. The args parameter tells us what happened: how it changed, and what exactly changed. Let’s take a peek at some code to see it.

static void Main(string[] args)
        {
            var collection = new ObservableCollection<string>();
            collection.CollectionChanged += collection_CollectionChanged;
            collection.Add("Hello World!");
            collection.Add("Goodbye World!");
            collection.Remove("Goodbye World!");
        }

        static void collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    Console.WriteLine("\"{0}\" was added.", e.NewItems[0]);
                    break;
                case NotifyCollectionChangedAction.Remove:
                    Console.WriteLine("\"{0}\" was removed.", e.OldItems[0]);
                    break;
            }
        }

The Action on the NotifyCollectionChangedEventArgs tells us if something was removed, added, or moved. NewItems is an array of items that were added, and OldItems contains items that were removed. In the ObservableCollection class, the event will be raised once per-item and the NewItems and OldItems will contain a single item. Other classes that implement the observable pattern in the framework (or possibly your own) may in raise the event once for several items, possibly for both an Add and Remove. One occurrence of this is the observable pattern that is in the AJAX 4.0 Framework when making web service callbacks.

Observable collections are useful when a collection is handed to several actors on the collection, and a single place must know what is occurring on the collection, for example: displaying feedback to the user on a GUI.

Kevin Jones is a Team Lead at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product. On Twitter? Follow Kevin

Digging into the Parallel Framework

April 22, 2010 3 comments

Digging into the Parallel Framework

April 22nd | 2010

Digging into the Parallel Framework

A new feature I really like in the .NET Framework 4 is the parallel framework. Virtually every home computer has at least two physical cores, but client applications don’t utilize multiple cores as effectively as they could; probably because parallel programming is difficult.

As I’ve never been required to make heavy use of parallel programming, I haven’t invested time in existing frameworks. Typically, I’ve used a messy combination of a semaphore and threads. This lead to some complex design patterns and the occasional bug. Nor was this method as effective as the parallel framework.

To get started, most of the parallel related APIs exist in the System.Threading.Tasks.Parallel namespace. The class we will discuss here is fittingly named Parallel.

Example task, and how to solve it.

Take 20 popular websites, download their HTML and save it to disk.

We’ll use Alexa as our source for the websites, and keep it simple by hard-coding the URLs for now.

Using no parallel tasks at all, the code looks like this:

Sub Main()
    Dim topUrls() As Uri = {
        New Uri("http://www.google.com")
    } 'Remainder ommited for clarity
    Dim watch As Stopwatch = Stopwatch.StartNew()
    For Each url As Uri In topUrls
        Dim wc As New WebClient()
        Dim data() As Byte = wc.DownloadData(url)
        File.WriteAllBytes(".\" &
            url.GetComponents(UriComponents.SchemeAndServer Xor
            UriComponents.Scheme, UriFormat.SafeUnescaped) &
            ".html", data
        )
    Next
    watch.Stop()
    Console.WriteLine("Elapsed Time: " & watch.Elapsed.ToString())
    Console.ReadKey(True)
End Sub

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, “Courier New”, Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Given that network performance is subjective, our mileage will vary. There was negligible latency except for sina.com.cn. All in all, the resulting performance is 00:01:11.068 over an average of 10 runs. The performance of sina.com.cn was pretty bad, but that’s expected given that it’s Chinese based – and their site is a whopping 466kb at the time of writing.

This kind of work is perfect for parallel programming. Even though it’s not CPU intensive, it’s also not utilizing my network as well as it could. The Parallel class can help us, and it isn’t a big leap to get the basics going. There’s a static method on the Parallel class called ForEach, and it will act as our iterator rather than an actual ForEach loop. We then pass a lambda specifying what the work is. This is the simplest use of the Parallel class.

Here is the code I wrote using Parallel.

Sub Main()
    Dim topUrls() As Uri = {
        New Uri("http://www.google.com")
    } 'Remainder omitted for clarity
    Dim watch As Stopwatch = Stopwatch.StartNew()
    Parallel.ForEach(topUrls, Sub(url)
                Dim wc As New WebClient()
                Dim data() As Byte = wc.DownloadData(url)
                File.WriteAllBytes(".\" &
                url.GetComponents(UriComponents.SchemeAndServer Xor
                UriComponents.Scheme, UriFormat.SafeUnescaped) &
                ".html", data
                )
            End Sub
        )
    watch.Stop()
    Console.WriteLine("Elapsed Time: " & watch.Elapsed.ToString())
    Console.ReadKey(True)
End Sub

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, “Courier New”, Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

It doesn’t look too much different, does it? Behind the scenes, there is a lot going on. It’s a complex bit of multithreading and distribution. It’s dependent on your environment as well. The number of threads used depends on how many cores you have, and many other factors.

As a result, using the Parallel worker shaved an average of 10 seconds off every run.

Parallel.For is very similar in behavior, but rather than using a collection it will use a start integer and end integer.

Now let’s say we need to accumulate results. For example, let’s extract the title from the HTML. Again, the single threaded example looks like this:

Sub Main()
    Dim topUrls() As Uri = {
        New Uri("http://www.google.com")
    } 'Remainder omitted for clarity
    Dim watch As Stopwatch = Stopwatch.StartNew()
    Dim pageTitles = New List(Of String)()
    For Each url As Uri In topUrls
        Dim wc As New WebClient()
        Dim data As String = wc.DownloadString(url)
        Dim title As String = Regex.Match(data,
    "<title[^>]*>(?<title>[\W\w\r\n]*)</title>").Groups("title").Valu
        title = Regex.Replace(title, "[/\\:\?\*""<>|.\r\n\W]", "")
        pageTitles.Add(title)
        File.WriteAllText(".\" & title & ".html", data)
    Next
    watch.Stop()
    Console.WriteLine("Elapsed Time: " & watch.Elapsed.ToString())
    For Each title As String In pageTitles
        Console.WriteLine(title)
    Next
    Console.ReadKey(True)
End Sub

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, “Courier New”, Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

My regular expression handiwork isn’t the best, but it gets the job done. To put this in Parallel, we might be tempted to wrap the whole thing with a Parallel.ForEach again, but we can be more efficient than that.

The Parallel framework does not execute one item per-thread and ditch the thread. That would be inefficient. What we can do is specify local storage for each thread, then an additional lambda containing the accumulated data from the thread will be invoked. In addition to our body, we will now have an initializer that returns an initial state of an object that we define. This gets passed into the body, and the body will accumulate to that thread’s local storage. And finally, when the thread is finished, we’ll merge it into a master collection.

It sounds complex, but really, it isn’t. Let’s look at the code:

Sub Main()
    Dim topUrls() As Uri = {
        New Uri("http://www.google.com")
    } 'Remainder omitted for clarity
    Dim watch As Stopwatch = Stopwatch.StartNew()
    'Use a thread safe collection!
    Dim pageTitles = New ConcurrentBag(Of String)()
    Parallel.ForEach(topUrls, Function() New List(Of String)(),
      Function(url, loopState, threadData)
          Dim wc As New WebClient()
          Dim data As String = wc.DownloadString(url)
          Dim title As String = Regex.Match(data,
    "<title[^>]*>(?<title>[\W\w\r\n]*)</title>").Groups("title").Value
          title = Regex.Replace(title, "[/\\:\?\*""<>|.\r\n\W]", "")
          threadData.Add(title)
          File.WriteAllText(".\" & title & ".html", data)
          Return threadData
      End Function,
     Sub(threadData)
         For Each title In threadData
             pageTitles.Add(title)
         Next
     End Sub)
    watch.Stop()
    Console.WriteLine("Elapsed Time: " & watch.Elapsed.ToString())
    For Each title As String In pageTitles
        Console.WriteLine(title)
    Next
    Console.ReadKey(True)
End Sub

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, “Courier New”, Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

This isn’t entirely different from the previous result, there’s just a little extra. Looking at the call to ForEach, we are still handing in the topUrls as before, but now we have an init function before the body. This is run once before each thread starts, and it is unique to that thread. In this case, it’s a plain list. Since the list is unique to each thread, we don’t have to use a ConcurrentBag or worry about thread safety.

Next: the body. Previously it was a Sub/Void that took in each URL as the collection iterated. Now it includes the loopState, which can provide information about your current loop. We aren’t going to use that as of now. It also has the object that we initialized in the beginning. We will add our title to this collection, and return the collection. Lastly, we have the finally piece. Throughout the body we were adding the titles to different Lists, which now won’t do us any good. The finally is a lambda that says, OK. This thread is done. Here is all the data this thread accumulated. Do with it as you see fit. In this case, we copy it to a ConcurrencyBag back on our main thread. Note that whatever you call in the finally piece must be thread safe.

That’s the basics of the Parallel task. And there’s still a lot to explore in the in the Parallel Framework including Tasks, Cancelling, and Progress updates.

Kevin Jones is a Team Lead at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product. On Twitter? Follow Kevin

Categories: .NET

Quirks in .NET – Part 2 Marshalling Booleans

February 25, 2010 3 comments

.NET Quirks

February 25th | 2010

Quirks in .NET Part -2  Marshalling Booleans

In Quirks in .NET – Part 1, we talked about the quirky call vs. callvirt instructions.

This week Platform Invoke is under my microscope. It isn’t necessarily a quirk, but it can be a hair-pulling experience when you get it wrong. More specifically, getting the marshaling done properly. Even more challenging, getting it right on both 32-bit and 64-bit environments.

Marshaling Booleans

Marshaling is done primarily for two reasons: you need functionality that is not built into the .NET Framework, but is found in existing Win32 APIs; and you have existing functionality in an unmanaged library you want to reuse.

Let’s start with the basics: What managed types map to unmanaged types? Some are common sense and some are not. Let’s talk about the System.Boolean type for a second.

Most people assume it takes exactly 1 bit in managed world, but in memory it’s a byte. We can prove it with this code:

int sizeBoolean = sizeof (bool);
int sizeInt = sizeof (int);
Console.Out.WriteLine("size of boolean = {0}", sizeBoolean);
Console.Out.WriteLine("size of int = {0}", sizeInt);

The size of operator returns the value in bytes, and a bool is 1. Here’s where things get tricky. In unmanaged world, there are two types—three really—BOOL, BOOLEAN, and COM’s VARIANT_BOOL.

The .NET Framework will try guess which one you mean when marshaling, but it won’t always get it right. When a managed bool is marshaled in a Platform Invoke method, it will marshal it automatically as a 4 byte type which is the more common marshaling case. However, if you cause an interface to get marshaled to COM and that interface has a Boolean as a return or method parameter, it will marshal it automatically as a 2-byte VARIANT_BOOL. Since COM interfaces tend to mix and match Booleans of different sizes, it’s always best to be explicit about which Boolean to marshal it to using the MarshalAsAttribute attribute.

You’ll have to look at the IDL or header file of the unmanaged code to determine which you need to use. Sometimes it requires a bit of playing around too.

Unmanaged Type Size (in bytes) MarshalAs Enum
BOOL (Win32) 4 UnmanagedType.Bool
BOOLEAN (Win32) 1 UnmanagedType.I1
VARIANT_BOOL (COM) 2 UnmanagedType.VariantBool

In Quirks in .NET – Part 3 we’ll look at numbers and binary

Kevin Jones is a Team Lead at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product. On Twitter? Follow Kevin

Categories: .NET

Quirks in .NET – Part 1 Call vs Callvirt

February 18, 2010 3 comments

Debugger Friend or Foe?

February 18th | 2010

Quirks in .NET Part -1 Call vs Callvirt

There are a lot of cool yet strange things in the .NET Framework. A lot of them make you go, “Huh. I didn’t know that.” Let’s take a look at a few of them.

call vs. callvirt

This one is especially interesting to me. In the Microsoft IL specification there are two instructions to call a method, put simply—call and callvirt. call will simply call a method, while callvirt is used to a late-bound method call, meaning that the method which is going to be called is chosen at runtime and not compile time as the simple call would. This is needed for virtual methods when a VTable lookup is required.

Interestingly, all calls to instance classes are compiled as callvirt, even if the method being called is not virtual (thus at compile time we know exactly which method will be called.) I thought it was a bug at first, but really it was a fix for another bug. Consider this code:

class Program
{
    static void Main()
    {
        HelloWorld instance = null;
        instance.SayHello();
    }
}

public class HelloWorld
{
    public void SayHello()
    {
        System.Console.WriteLine("Hello World!");
    }
}

Looks like it would immediately blow up, right? We should get a NullReferenceException. And in fact, if we compile this with the C# compiler, we do! Let’s look at the IL for the Main() method.

  L_0000: nop
  L_0001: ldnull
  L_0002: stloc.0
  L_0003: ldloc.0
  L_0004: callvirt instance void HelloWorld::SayHello()
  L_0009: nop
  L_000a: ret

Sure enough, it uses a callvirt, but here’s the interesting part: If the compiler used a call, rather than callvirt, this code would actually run. In fact, we can do that, too. Let’s change the callvirt to a call, recompile the IL using ilasm, and see what happens.

netquirks

Yeah, it appears that our program, which should be causing a NullReferenceException, isn’t. This is why the .NET compilers don’t use call, even though using call is a perfectly acceptable in this case. For that matter, the only time a regular call will be used is when you call a method on a struct, or a static method on a class.

If you use the Reflection.Emit namespace frequently, or are looking into it, this is something to watch out for.

In Quirks in .Net  Part 2. we’ll look at Marshalling Booleans.

Kevin Jones is a Team Lead at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product. On Twitter? Follow Kevin

Categories: .NET