Get Application's Icon

When I was building “About” dialog for my applications, I wanted it to be as reusable as possible. That meant that all data should be retrieved while program is running. Only thing that I could not discover within .NET code was application’s icon.

Windows API came to rescue. In short we just load our own assembly for purpose of extracting one icon resource. That gives us “good old” pointer which can be used to create bitmap.

IntPtr hLibrary = NativeMethods.LoadLibrary(Assembly.GetEntryAssembly().Location);
if (!hLibrary.Equals(IntPtr.Zero)) {
    IntPtr hIcon = NativeMethods.LoadIcon(hLibrary, "#32512");
    if (!hIcon.Equals(IntPtr.Zero)) {
        Bitmap bitmap = Icon.FromHandle(hIcon).ToBitmap();
        if (bitmap != null) { return bitmap; }
    }
}

Probably most curious thing is “#32512” string. This is resource identifier that goes way back into non-CLR past (also-known-as IDI_APPLICATION constant) and it is not only one that can be used.

Here is full sample.

What Is Happening to Me?

Illustration

Few days ago I wrote instructions for setting-up Android development environment. Since I am mostly known as Windows developer this caused lot of questions in my neighborhood. Here are answers to most common ones and some observations of mine.

1. Yes, I am still C# developer at heart.

2. I still love Windows Mobile. This goes for 6.5. As far as Windows Mobile 7 is concerned, I have my reservations (this is drastic change after all) but it is really hard to comment anything at this time. Most of known limitations and features of that platform are subject to change by the time of launch. Anyhow most of things that bother me are just political decision (e.g. multitasking) and not a real limitation.

3. I do not consider my work in Android as betrayal of my love. While I love Visual Studio and what it does offer, I think that trying other environments can only lead to good things.

4. My phone ratio is 2:1 in favor of Windows Mobile (HP iPAQ 514, HTC S740 and HTC Desire).

5. Exchange ActiveSync is best protocol there is. I am not sure how good it was for Microsoft to allow Google and others to use it in their products but from customer side I must confess it is great decision. It allows all my phones to share same data regardless of their underlying platform and everything gets synchronized by “it-self”.

6. Both Windows Mobile and Android share same hate for letters Đ and đ (latin capital/small letter D with stroke). While I can synchronize existing contacts with this letter, neither of these platforms will allow me to enter this letter via keyboard. When letter is part of your alphabet it gets annoying fast.

7. Yes, I will probably make some comparison of development experience between these platforms in future.

Setting Android Environment

Illustration

Prerequisited

Before we start anything, we need two downloads:

Java SE Development Kit

This one is prerequisite for everything else. Just download it, run it and go wild on “Next”. Installation is as easy as it can be.

Installing Eclipse

Installing Eclipse is as easy as unpacking it. For purpose of this walk-through I will unpack it to root directory of drive D: (eclipse.exe will be located in directory D:\eclipse).

Installing Android SDK

Unpack archive to (D:\eclipse) and run “SDK setup.exe” (from “D:\eclipse\android-sdk-windows\SDK setup.exe”). Location is not set in stone but I like it in subdirectory of eclipse. If you desire some other location, just go for it.

As soon as you start it, it will attempt to download newest repository data. If you are behind proxy, this step will fail. In that case just go to “Settings” and fill proxy server and port fields. Often it is also necessary to check “Force https://… sources to be fetched using http://” for everything to download properly.

Once update is completed, you should get list of packages to install. I just leave it at default, check “Accept All”, and click on “Install”. Brace for lengthy download (around 800 MB for Android 2.2).

ADT Plugin for Eclipse

Once you start Eclipse go to “Help”, “Install new software”. Click “Add” once window opens. Now write “ADT Plugin for Eclipse” under site name (exact naming is not important) and “https://dl-ssl.google.com/android/eclipse/” under location. If your Internet access goes over proxy, you will need to use http location “http://dl.google.com/android/eclipse/”.

When packets are found (“Android DDMS” and “Android Development Tools”) I just select them both and click on “Next”/“I accept …”/“Finish” combo few times and download will start. After restart you need to go to “Window”, “Preferences”. There, under “Android” node, you need to select folder with unpacked Android SDK (in my case “D:\eclipse\android-sdk-windows”). All other things I left as default.

After one more restart (I am not really sure it is necessary) you will be able to create new project under “File”,“New”,“Project”:“Android\Android Project”.

Anything more

As you attempt to start project, you will get prompt to create emulator. If you would like audience as broad as possible, Android 1.6 is reasonable choice. If you want to target only newer devices, 2.1 is one that you should do your testing on. Those are only rule-of-thumb advices - do not take them too seriously.

If you intend to use command-line tools it would be good to add Android SDK tools to path (for me it is under “D:\eclipse\android-sdk-windows\tools”).

It is definitely not as easy as installing Windows Phone Developer Tools but it is not too bad.

Visual Basic 6.0 - Controls

I am preparing to clean my web site and I decided to remove all references to programming source samples. General idea is moving those samples to blog. Here I will start with my old VB 6 controls.

Download is here.

CmnDlg

Substitute for CommonDialog control.

Contain Containter for other controls.

Progress

Replacement for ProgressBar Common Control. Doesn’t use .ocx.

TextComplete

AutoComplete TextBox.

TrayIcon

Enables program icons in tray notification area.

P.S. Rest of old stuff can be found in this post.

Java Rfc2898DeriveBytes

I was creating Java port for one program of mine and I stumbled across little issue. Although C# and Java seem quite different, you can almost always rely on one-for-one feature compatibility. Of course I stumbled across one class that was not implemented in Java. That class was Rfc2898DeriveBytes (.NET PBKDF2 implementation).

To be totally correct, I did found quite a few classes that do implement RFC 2898 but they did not give same result as one I used in .NET. While those implementations were also correct ones, they did not ensure compatibility with my existing code.

.NET Reflector comes here as great debugging tool. Quick peek just discovered that core of .NET Rfc2898DeriveBytes class is HMAC SHA-1 algorithm. GetBytes method has some basic buffer management (data gets generated 20 bytes at time) and call to omnipotent Func method. It is in this method that real crypto-magic happens.

Fortunately, building blocks for this functionality is available to Java. Although syntax is somewhat different, general idea is same. Whole getBytes method needed only changes related to array copying. In .NET we would use Buffer.BlockCopy and in Java this translates perfectly to System.arraycopy. Really hard…

Crypto-core is hidden in function named “func”. Notable spot here is incrementing block counter. In C# this is unsigned int but in Java there is no such thing. That is reason for one extra check.

if (this._block == 2147483647) {
    this._block = -2147483648;
} else {
    this._block += 1;
}

With these few changes done our Java implementation of Rfc2898DeriveBytes was done. Source code can be downloaded here.