Showing posts with label Tools. Show all posts
Showing posts with label Tools. Show all posts

Friday, June 17, 2011

Helpful tools for the weary programmer Part 2

Hi All,


This is the second part of my review of the tools I use in my daily work. If you likes the first part you will surely like the second.


Code decompilation or disassembly
The third category of useful tools are disassembly tools. If I were to make this post couple of months ago it would probably contain only Red-Gate Reflector but recently Red-Gate decided to charge money for even the basic version of Reflector so I will survey two alternative tools which are currently in the beta stage. 

The first tool comes from the guys at #Develop and is called ILSpy. ILSpy can take a compiled .NET assembly and regenerate the original code (or at least something close to the original). In the current beta it can show you the code in both C# and IL and it is quite accurate in the generated code. You can save the generated code to a code file easily from the menu. ILSpy supports search and navigation directly from the code editor and browsing of dependent dlls. One of the big tests of a decompiler is the ability to decompile itself which ILSpy does perfectly.

Another contender to the crown comes from the guys at JetBrains (the ones who make ReSharper) in the form of dotPeek. The UI of dotPeek looks similar to ILSpy (and to Reflector) but offers richer menus. I found it somewhat difficult to navigate through the code editor and had to search through the context menu items. At times the navigation didn't work at all. The generated code was quite accurate and at some places clearer than the code generated by ILSpy. It had no difficulty in decompiling itself 


The third tool comes from Telerik and is called justDecompile. Currently it is in the Beta stage and free. The UI is again similar although somewhat slick. I didn't manage to get any useful data with this tool as it failed to decompile even the simplest of codes. It is clearly not ready for real user but definitively worth to keep in mind.


This list cannot be complete without ILDasm (the tool that comes with .NET, search ildasm in your Everything). I like this tool because you are able to view the manifest of a dll in a convenient way. You can also use it to check the dependencies of a specific dll but only for one level. The biggest disadvantages of this tool is that the code is decompiled into IL and there is no easy navigation between assemblies or even within the same assembly.


ILSpy decompilation of the Object class

dotPeek decompilation of the Object class


justDecompile decompilation of the Object class


Snooping/Spying a running application
You probably know the following scenario: you add some control to your application and see it well in the designer but when the application is run it is nowhere to be found,or a scrollbar appears out of nowhere and you have no idea which control shows it. It is simple to see the dynamic structure of your running application while it runs using one of the following tools:
Spy++ for Win32 based applications (anything which is GDI based and not drawn by DirectX). This tool is installed with Visual Studio and is easy to use. When you run it, it shows all the window handles of your top level windows in a tree and you can drill down to the smallest of windows (in GDI everything is a window). You can highlight a certain window by using its handle or find a certain window by pointing at it with the mouse (using the find target tool). When the window is found you can track all the messages sent to that window.


If you try to use Spy on a WPF application you will have a surprise on your hands. Any WPF Window contains only one GDI window because the rendering is done internally using DirectX. In order to inspect a WPF application I use a different tool called Snoop. This tool is essentially like Spy (but somewhat closer to Firebug). Pointing Snoop on a WPF application will give you the full Visual Tree of this application. For each item in this tree you will be able to see and sometimes edit its properties. Snoop will also tell you who set each property so you will finally be able to see which animation stuck the height of your button.


Snooping justDecompile which decompiles the Object class




These are the main tools I use in my daily work. I very much recommend that you try each of the mentioned tools at least once to see if it is useful for you.


Thank you for reading
Boris.

Friday, June 10, 2011

Helpful tools for the weary programmer Part 1

Hi All,

[beginner level]
Today I am going to talk about some tools which make my life as a programmer somewhat easier. I know there are tons of articles such as this but I want to give my personal view on the way I work. A nice lecture about useful tools was given in the 2010 TechEd in Eilat and can be found here. The tools I am going to talk about are either free or come with the installation of Visual Studio and they are ordered by their usefulness to me and grouped by functionality.

Locating your files
If you are still using windows search function then this tool is for you. The tool is called Everything. (This tool was shown to me by David Elentok couple years back, you can find his blog with very interesting posts on my favorite blogs section on the right side of the page.)
After installing, the tool will "index" your hard drive. I write "index" in double quotes because it doesn't really read  your entire hard drive like some other search tools, it simply reads the NTFS table and maps it to an internal database (which is about 4Mb in size, depends on the size and content of your hard drive). This process runs the first time you start Everything and takes around 1-2 minutes. Every time you start Everything again it will update the local database within several seconds (the default behaviour is to let it start with windows and let it run in the background, then you don't even have to wait for it to start every time)  After the "indexing" is done each search takes... well... it searches as fast as you can type. The search results are updated as soon as you make changes to the files on your hard drive which can be very useful. One problem I found with this tool is that it sometimes doesn't show correct file metadata, this is a minor issue which should be taken into account.
I think this tool is a must for anyone working on a computer.

Who store my referenced dll (or who accessed my file)?
How many times did you get this exception message:
Could not load file or assembly <SomeDllName> implementation version=1.0.0.0, culture=neutral, publickeystoken = 90ba9c70f846762e or one of its dependencies. the system cannot find the specified files.
What do you do then? You open Everything (see previous paragraph) type in <SomeDllName> and of course find this file in your bin directory. This leaves you with one of its dependencies... doh!

As an example I created a console application called LoadExample which is unable to load a dll called MissingDll (because I deleted it... :))

The easiest thing to do is to run "Fusion". Note that it needs to be run as administrator and you need permissions to edit the registry! Just run the tool (find it using Everything on your drive) as administrator (right-click on the executable file and select Run As Administrator) then in the settings set it to log failures and select some (existing) custom path to be able to delete the cache. Run the problematic application and you will see the missing dll.

This is what you see when you run LoadExample with Fusion

There is a missing file but it is not an assembly? You can use the Sysinternals Process Monitor which is downloadable as part of the Sysinternals Suite. Start procmon.exe and set the filter path to either the missing file name or if you don't know the missing file name then set the process name to your application process name (use the "contains" condition so that you don't have to write exact names and paths). Now run the application and check the Results column. You are looking for anything that might be a hint for a problem and usually contains the words "NOT FOUND". Note that the Process Monitors monitors five types of operations as denoted by small icons on the right side of the main toolbar. If you are looking for a missing file you might want to leave only the file system activity turned on.

I found the missing dll

Missing dll in native code (or CLI)? There is a simple solution in the form of Dependency Walker. Once you load a dll in Dependency Walker, it will show all the dlls it depends on in a hierarchical tree. If there are missing dlls they will he highlighted using a special icon. This is especially useful when you fail to register a dll using regsvr32 because it will show all the dlls which are needed (and missing) for the registration.

You can get more (or less) information on this topic by reading this blog post.

Next week I will publish the second part of this post with some more useful tools.
Thanks for reading,
Boris