Programming in C#, Java, and god knows what not

Progress Indicator in Windows 7 Taskbar

Illustration

One of little things that mean a lot in Windows 7 is (at least for me) ability to show progress in taskbar. There is no longer need to Alt-Tab every minute or so into long running task (most often file copy). Progress of any long-running operation can (and should) be visible in taskbar button itself.

It is surprisingly easy to use this feature in our programs. Everything that we need is contained withing two functions: SetProgressState and SetProgressValue. Setting percentage is as easy as

var res = _taskbarList.SetProgressValue(owner.Handle, (ulong)newProgressPercentage, 100);

Although that minimal code will do it’s work, there is need for some additional preparation around it. We need to initialize feature itself, we need to check whether function is even available (XP is pretty much alive) and some error checking would be nice touch.

Because of this I made sample class in C# that should enable your program to have all those goodies. And, as always, sample is available for download.

64-Bit Registry View

If you are creating application that works with registry (e.g. your own registry editor) you are probably aware that 64-bit and 32-bit applications do not necessarily share registry keys. If you have 32-bit application your view of registry will not match one that 64-bit application gets.

.NET Framework 4.0 helps here a little. There is RegistryView enumeration available so code can specify which “bit-ness” of registry is desired. It works both ways: 32-bit application can specify 64-bit view and 64-bit application can specify 32-bit view.

As always, you cannot get what is not there. If you have 32-bit Windows, specifying 64-bit view will still give you 32-bit one.

ClientLogin

Google offers quite rich API for users of their services. Most of it needs some form of authentication. Without much ado here is my C# implementation. Only limitation is lack of captcha support, everything else should be there.

P.S. If you are interested in Google Data API, you might want to check .NET client library. It is open source project and it covers pretty much whole API suite.

NTP Server

Few days ago I stumbled upon problem. I had three Windows 7 computers without Internet access and I needed time synchronization between them. It wasn’t really important that time is correct - it was important that time is same. Solution was simple - just make one of computers NTP server and let others synchronize to it (using standard Windows time synchronization). While Windows 7 synchronizes time perfectly over Internet and/or when you are member of domain, it does not include NTP server.

As I tried to install some other software it became clear that I need to stop and DISABLE “Windows Time” service. NTP servers can only work if they can use UDP port 123. As long as “Windows Time” service is running that port is taken by it. For my scenario this service does nothing so disabling it caused no problems.

As I tried few NTP server solutions I got more and more desperate - either they weren’t free (yes, I am cheap bastard) or they would not work as service under Windows 7. After some time I just decided to make my own program. How hard can it be?

While I consider most of RFCs technical descriptions pure beauty, somebody made a mess of this one. All information was there, how to synchronize, which algorithms to use, probably even genealogy of authors - only thing not there was pure and simple ASCII format of UDP packet. Fortunately authors of SNTP (which is mostly compatible with NTP) did much better work with it. That combined with old pal WireShark made this program possible.

As any program that is written in one afternoon, this one has fair share of things not done. First of all, this program was only tested with Windows XP and Windows 7 as clients. While other versions should work, I spent no time in actually testing it.

This server is also not fully NTP compliant. All fields are there and all clients will consider it valid time source but stratum, precision, root delay and root dispersion numbers are just hard-coded instead of calculated. This should present no trouble in local network and if precision in seconds is satisfactory, but it is definitely not time server you would use if every microsecond is important.

Program requires .NET 2.0 and there is no setup. When you extract it to desired folder first order of day is performing installation from administrator prompt (cmd with Run as Administrator is fine). There we just run program with /install as parameter (“tempora.exe /install”) and service will be installed and started (if you remembered to DISABLE “Windows Time” service first). As long as you don’t uninstall it (“tempora.exe /uninstall”) it will start at each startup and it will return current time to all it’s clients.

Download is here.

P.S. Do not forget to DISABLE “Windows Time” service. ;)

[2015-04-25: Program is available on GitHub]

IsNullOrWhiteSpace

When one deals with user input, it is quite useful to check whether user entered anything. Definition of “anything” is not necessarily same for developer and user.

Quite often user will consider one accidental space to be nothing at all. To implement proper check developer would need something like this:

if (string.IsNullOrEmpty(text) || (text.Trim().Length == 0)) {
    //Text is empty.
} else {
    //Text is not empty.
}

Code is not difficult and it is pretty obvious. Hard part (at least for me) is remembering to add this text.Trim().Length==0 condition. Some time I just forget.

.NET 4.0 brings little bit of syntactic sugar in this area:

if (string.IsNullOrWhiteSpace(text)) {

It is basically same code - just a little bit shorter and, thanks to IntelliSense, much easier to remember.

WebMatrix

Illustration

All those interested in ASP.NET might benefit from newest Microsoft toy. It is called WebMatrix and it is currently available as a beta. It brings to table everything lightweight web development needs: easy syntax (ASP.NET Razor), small database (well known SQL Server Compact Edition) and decent web server (IIS Developer Express). Those interested might as well check video presentation.

For me IIS Developer Express is probably best thing in this package. It is simple web server that does not need ninja to configure it properly. Works with any recent Windows system (yes, even XP is supported) and all that while having almost complete compatibility with full IIS Server. That is quite a feat since all IIS 7 modules are also supported alongside whole IIS 7 extensibility model.

All that and it needs only some space on hard drive and standard user privileges for both running and debugging.

Naming Issues

Illustration

I had to install SQL Server 2008 R2 on Windows Server 2003. As soon as install started, I was greeted with message that Windows Installer 3.1 and .NET Framework 3.5 are missing (yes, it was quite an old installation). Fortunately links were provided and I was ready to continue install.

After checking system requirements, SQL Server had another surprise for me - it needed PowerShell 2.0. Although it was annoying that I haven’t got this error before, I just went to big evil Internet to download it.

To my surprise I could not find direct link to PowerShell 2.0 download. There was a lot of talk about it, quite a few links to PowerShell 1.0 - everything but quick link toward PowerShell 2.0. After restricting search to Microsoft only, problem was found.

For some reason Microsoft decided to pack PowerShell 2.0 with some additional tools and call it a Windows Management Framework update. Following links on that page finally brought me to download containing PowerShell 2.0 (among other things).

We're Off to See the Market

Illustration

In order to put application on Android Market one of most important steps is signing it. There is pretty comprehensive guide available for that in developer documentation. I followed instructions wondering why everything needs to be done in command-line only to discover at the very end that those steps are not needed.

All you need is proper setup of your development environment. Once you have that, creating Market-ready application is just matter of right-clicking on project and selecting “Export Android Application”. From there wizard will guide you through key creation, compilation and all other necessary steps.

Retrieving Application Icon in WPF

As I started doing more and more work in WPF, I had to port my “About” dialog. In attempt to keep everything clean of Windows Forms that also meant that I need to find another way of loading icon (since Bitmap class is not part of WPF).

Code follows same logic as one for Windows Forms. Only important difference is use of ImageSource as our final destination.

var hLibrary = NativeMethods.LoadLibrary(Assembly.GetEntryAssembly().Location);
if (hLibrary != IntPtr.Zero) {
    var hIcon = NativeMethods.LoadIcon(hLibrary, "#32512");
    if (hIcon != IntPtr.Zero) {
        return Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
    }
}

Without further ado, here is full sample.

The Image Format Is Unrecognized

Illustration

As I started my new WPF application, I got greeted with FileFormatException - “The image format is unrecognized”. I was confused since application was so new that I haven’t added any images to it. After checking and double-checking I found which image exception was referring to - my application icon.

It seemed like normal icon to me. As I compared it to icons that I already used successfully with WPF there was only one difference. This icon had 256x256 size. As soon as I deleted it, my program started working again (ok, it was empty application so making it work was not that big of task).

Side effect of this solution was that I lost my big images. That felt dirty to me so I ended up with two icons in my application. First one was with all images (including 256x256) and I used it as application icon (Properties, Application, Icon). Second one was without 256x256 images and I just set it’s Build Action to “Resource”. Whenever I needed WPF window icon, I just pointed to second one.

I must confess that I am quite annoyed by this. WPF was all about making Windows look and feel better. Using brand new .NET 4 and restricting icons to sizes before Vista just does not seem right. And I better not start about how useful XAML exceptions are…

P.S. If you do not have icon editor available, I would recommend IcoFx. It is good and it is free.