Skip to navigation
Real World Computing

Almost ADSL

Posted on 2 Jul 2002 at 17:04

Dave Jewell awaits his ADSL installation with both fear and excitement, and shows how to access the Shell32 COM objects from C#

if (item.IsFolder) FList.Items.Add ("-" + item.Path + "-');

else FList.Items.Add (item.Path);

}

}

}

As you can see, the code iterates through the various files in the folder, adding each one to the list box. If NumFiles happens to return zero (an empty folder), then the semantics of C# ensure that the for loop never executes and the list box remains empty.

You'll also notice that I use a property of the FolderItem object, IsFolder, to determine whether or not each folder item is itself a folder. If so, I enclose the folder pathname within hyphens which - because the list box is kept sorted - ensures that all the folder names are placed together, followed by the ordinary filenames (though, of course, this simple scheme will screw up if you happen to have any ordinary files whose names begin with a hyphen). If you want to do the job properly, use an owner-draw list box and apply distinctive formatting to the drawing of folder items. Likewise, if you wanted to display things like file size and modification date alongside each item, use a list view control operating in report mode rather than a simple list box. I should also point out that .Net contains its own set of rich classes and methods for manipulating files and directories - all I'm trying to get across is the simplicity of accessing COM objects from a .Net application.

Another interesting method of the Shell object is ControlPanelItem. Just pass the name of a Control Panel extension as an argument to this method and - hey presto - it will be started for you.

sh.ControlPanelItem ("desk.cpl");

In this example, the desk.cpl extension will start executing, showing the familiar Display Properties dialog. Note that if the specified extension is stored in the Windows or WindowsSystem directory, it isn't necessary to provide a full pathname, but the cpl file extension isn't optional.

The EjectPC method can be used to eject a laptop PC from its docking station, while the more useful Explore method will invoke Windows Explorer, set to a designated folder location:

sh.Explore ("c:Program Files");

The FileRun method can be used to invoke the Shell's Run dialog (equivalent to clicking 'Run...' on the Start menu), while the FindFiles method invokes the standard Find Files dialog, and so on. Many more interesting things can be done by referencing the Shell32 library from a .Net program and this is one of many COM objects that Microsoft makes available.

Exemplary decompiler

I pointed out a few months ago that the IL code used by Microsoft .Net is very easy to decompile. This is because this virtual machine code is stack-based, which means that any prospective decompiler doesn't have to worry about keeping track of machine registers. Likewise, because the IL codes are relatively high-level operations, the amount of work needed to produce an equivalent high-level language representation is correspondingly reduced. All the same arguments apply to Java byte code, which has been plagued by decompilation almost since its inception.

As I wrote about the possibility of a .Net decompiler, I didn't realise how quickly my predictions would be fulfilled. Let me introduce you to Exemplar 0.1, the world's first (as far as I know) full decompiler for C#. You can find Exemplar at www.saurik.com/net/exemplar and impressive it is too. Exemplar is freeware, released under a GPL licence and comes with full source code written in C++. The program isn't perfect, as it crashes when it comes across a chunk of IL code that it doesn't understand, and it currently exists only as a command-line utility. But as a proof-of-concept application, it's amazing.

1 2 3 4
Subscribe to PC Pro magazine. We'll give you 3 issues for £1 plus a free gift - click here

From around the web

Be the first to comment this article

You need to Login or Register to comment.

(optional)

advertisement

Latest Real World Computing
Latest Blog Posts Subscribe to our RSS Feeds
Latest News Stories Subscribe to our RSS Feeds
Latest ReviewsSubscribe to our RSS Feeds

advertisement

Sponsored Links
 
SEARCH
SIGN UP

Your email:

Your password:

remember me

advertisement


Hitwise Top 10 Website 2010
 
 

PCPro-Computing in the Real World Printed from www.pcpro.co.uk

Register to receive our regular email newsletter at http://www.pcpro.co.uk/registration.

The newsletter contains links to our latest PC news, product reviews, features and how-to guides, plus special offers and competitions.