Wednesday 25 June 2008

Viewing the source code of .NET CF applications with Reflector

The best way to learn is by example. You might have found a nice looking application that has a funky looking list box with icons and are interested in achieving the same look e.g. Live Search. Unfortunately not all CF projects are open source, but fortunately there is a way to view the source code. First thing you need to check is if the application you are interested in is in fact a .NET CF application. The first indicator is if takes a long time to launch, because .NET applications are much slower to load than native because the CLR needs to start first. If it is instant to start, like Google Maps then don't bother because it is native.

First you should check if the application's source code isn't published somewhere. If you found it on Codeplex, Google Code, or Source Forge then the source code is most likely available already, still if it isn't released for the version of the executable you have you can proceed with the steps below.

The next thing you need to consider is access to the EXE or DLL. If you have already installed the application on to your smart phone then you can just active sync, browse to \Program Files\TheApplication and copy the binaries back to your PC. If you don't have a phone handy chances are the application has been distributed as a CAB file and you need to extract the files. The fastest way to do this is to use WinCE CAB Manager, just open the CAB and drag out the EXE and DLLs. They have a trial you can use.

Next you need to install Reflector for .NET. It comes as a zip file so what I usually do is create the folder "C:\Program Files\Reflector" and drag the files into there. Then I right click the Reflector exe and do create shortcut, rename it to just "Reflector" and drag it into my start menu and quick launch if you use it a lot.

Now run it and drag your .NET CF exe into the window. Expand its tree icon a few times and hopefully you will see some class names. Right click on the class name and choose "Disassemble", then click the expand methods at the bottom of the right pane. Hopefully now you will see all of the code. If you just see gobbledygook for the class and method names unfortunately the developer has protected the code from our eyes (Obfuscated code). You can still proceed it just is going to be really hard to read, luckily you can click method and class names to navigate around the code. Once you find something interesting you can copy it from the code panel.

The final step is if you want to extract all of the files of the executable or class library. Reflector can actually generate a project file you can open in Visual Studio, make changes to and run and debug. First you need to install the File Disassembler Addin. Extract the DLL and copy it to the "C:\Program Files\Reflector". Then in reflector go to the Addin menu item and install Disassembler. Now activate it under the Tools menu. You will notice everything stays greyed out until you choose an assembly. This means you need to select a top level tree item. Next choose the project type and then Generate. If it moans about missing references make sure you browse to find the correct file. This is essential to give you a build-able project file. Once finished, you should have a folder of all of the source code. Unfortunately the project file generated is for full .NET and not .NET CF so you can either modify it to look like a CF project file, or much easier just make a new one and drag in all the code and set up the references. Of course the code isn't commented because comments are removed when the application is compiled, but C# is very readable so this isn't a huge problem. Sometimes it generates the loops and case statements in an un-friendly format, and you might have to tidy them up a bit.

So there you go, a fantastic way to examine the source code of non-open source CF applications and see just how other developers right their code, a great learning technique. For example Live Search, uses an MVC design pattern which is excellent to learn, but quite difficult to implement in Visual Studio because of the way the form designer works. I'll post about any other interesting .NET CF applications I find and you can try using Reflector on them if you like.

This guide is for educational use and if you work for a company you definitely shouldn't be stealing code like this, contact the author first and ask if they will let you in on their technique. Most .NET CF developers are a friendly bunch and willing to share. This guide is particularly useful to the moderate level programmer who has learned all the basics but seeking to learn how some professionally developed software is created, with the CF being so limited chances are you will come across something useful.

No comments: