Sunday, October 11, 2009

Where do you keep (serialize) application data?

Applications keep data everywhere. From plain text file, INI file, Windows registry, database to proprietary file format.

Take a browser as an example, it needs to serialize user options, bookmarks, cookies and history. The designer can choose to keep each kind of persistence in different files. For example, options in one file and bookmarks in one file. The designer can also choose to keep user options in Windows registry, while keeping the bookmarks in a file.

Can we keep everything (or almost everything) in a single place? Sure, database to the rescue! I am not talking about client/server database system like Oracle and SQL Server. I am talking about small but powerful database system such as SQLite and embedded Firebird. For a real world example, Firefox 3 uses SQLite to keep bookmarks, histories and cookies.

There are several apparent advantages of using database to keep data. It is easy to backup and restore if everything is kept in one database. You are also free from opening and closing various files. Retrieving data quickly is easily accomplished by using index. You can also avoid data inconsistency by using transactions.

Some people will complain that using database to keep small set of data is an overkill. You’ll have to judge that by yourself. Do you want to free yourself from headaches such as creating indexes and creating your own file formats? If so, start to serialize your data to database now. You can then focus on developing your software and release it quicker.

And yes, there are people who keep big list of bookmarks and histories.

Friday, October 9, 2009

Opera in XIII: The Conspiracy

Maybe I haven't watched too many movies, so this was the first time I saw Opera in a film. I was excited that I replayed to see the icon again :)

XIII was rescued by an old couple after being coma on a tree with a parachute. He woke up without remembering who the hell he was and what in the world he did, yet his could still show off his various skills.

He tried to use the old folk’s machine which had Opera installed (notice the dot dot dot (…) button?):
XIII uses Opera

Opera couldn’t connect:
2009-10-09 Opera2

Old man said the internet has not been functioning for more than a year, it must have been disconnected. XIII said that some ISPs are so lazy that they just disable users by blocking hardware ID. So he took off the modem cover (which was not screwed or glued together at all), adjusted a DIP switch,
2009-10-09 DIP

spoofed the MAC address,
2009-10-09 Spoof

and voilĂ , he’s online with HOT hit (#1 user-rated search engine)!
2009-10-09 Online

“You can't even remember your own name. How did you manage to do that?” the old man asked with a curious look.
2009-10-09 Old folk

“I have no idea,” XIII replied.
2009-10-09 No idea

Monday, October 5, 2009

Windows 7 Keyboard and Mouse Shortcuts

Windows 7 added some useful shortcut keys and mouse shortcuts.


The following keys affect the current window. They works only if Aero Snap is enabled which is enabled by default. If yours is somehow disabled, go to Ease of Access Center -> Make the mouse easier to use –> clear the “Prevent windows from being automatically arranged when moved to the edge of the screen" checkbox.

  • Win+Up: Maximize window (if it can be maximized).
  • Win+Down: Restore window if it is currently maximized, or minimize if it is not currently maximized.
  • Win+Shift+Up or Double-click the top border: Maximize window vertically.
  • Win+Shift+Left / Right: Move window to the another monitor.
  • Win+Left: Dock to the left of the screen, press this repeatedly to cycle the window through different monitors.
  • Win+Right: Dock to the right of the screen, press this repeatedly to cycle the window through different monitors.
  • Win+Spacebar = Take a peek at the desktop.
  • Win+Home = Minimize all other windows except the current window. Press again to undo.


In Windows Vista and Windows 7, you can press Win+T to focus the taskbar. You can do these when the taskbar has the keyboard focus:

  • Left or Right arrow: move between tasks.
  • Home: focus the first task button. Works only in Windows 7.
  • End: focus the last task button. Works only in Windows 7.
  • Press the first letter of a task title: That task button will be focused.
  • Up arrow: focus the Aero Peek window. When you are at a Aero Peek window, you can press left/right/home/end to move around. Press down arrow or Esc to focus back to the task button.
  • Enter or Spacebar: switch to the selected task.


Press Win+B to focus the tray. Then:
    • Left or Right arrow: move between icons.
    • Press the first letter of a icon tooltip: That icon will be focused. The tooltip is the text that appears when you hover a tray icon with the mouse.
    • Enter or Spacebar: this is the same as double-clicking the icon.
    • Drag a tray icon from the taskbar and drop it outside the taskbar: this will hide that tray icon from the taskbar, unless it has notifications.
    • Drag a icon from the hidden icon box to the taskbar: this will show that tray icon on the taskbar, making it always visible.

  • Taskbar Favorites

    Press Win+<Number> to open task quickly. For example: press Win+1 to open the first program on the taskbar. Press Win+0 to open the 10th program on the taskbar. These key combinations are called Taskbar Favorites, some models of Microsoft keyboard have actually included hotkeys just for these.

    Wednesday, August 19, 2009

    32-bit applications running under 64-bit Windows do not see the world as it really is

    64-bit desktop computing is becoming more common. Major computer vendors like Dell and HP have started to give users the choice of 64-bit OS (read: Windows) in many of their models. Some of you may have already owned a 12gb+ XPS from Dell.

    The specific Windows I am talking about here is Windows 7 64-bit, but the behaviors should apply to Vista 64-bit and/or XP 64-bit, as well as those 64-bit variants of Windows Servers.

    In those days of Windows 32-bit, we have those Windows API libraries such as user32.dll and gdi32.dll residing in %windir%\System32. It makes perfect sense that one would expect that 64-bit API libraries in 64-bit Windows would live in %windir%\System64.

    But in reality, 64-bit API libraries still live in %windir%\System32. One may say: Ok, it's alright to have both 32-bit and 64-bit libraries in the same directories, as the names of 64-bit libraries should be user64.dll, gdi64.dll etc.

    But again, in reality, 64-bit API libraries still use the same name -> user32.dll, gdi32.dll etc.

    So if 32-bit and 64-bit libraries share the same name, how do they both sit together in the same directory?

    The truth:

    1. 64-bit Windows API libraries live in %windir%\System32
    2. 32-bit Windows API libraries live in %windir%\SysWOW64
    3. 64-bit applications directly use 64-bit Windows API libraries in %windir%\System32
    4. 32-bit applications indirectly use 32-bit Windows API libraries in %windir%\SysWOW64

    The interesting part is point #4. Well, 32-bit applications cannot see the real %windir%\system32. Whenever a 32-bit application try to access %windir%\system32, the OS will translate the request to %windir%\SysWOW64, which is the place where 32-bit libraries live now.

    Need a proof? Let's do an experiement. We need Internet Explorer for this test. I hope you did not remove Internet Explorer 8 yet since Windows 7 indeed allow you to. If you have really removed it, just use any 32-bit applications that can open a file to test.

    1. Make sure your Windows is 64-bit. You cannot see this effect in 32-bit Windows.

    2. Open Windows Explorer (which is 64-bit), go to your %windir%\system32, create a folder named "Can you see me" inside it.

    3. 64-bit Windows come with both 32 and 64-bits of Internet Explorer. I need you to run the 32-bit Internet Explorer now.

    4. In Internet Explorer, press Ctrl+O to show the Open dialog. Then click Browse.

    5. Now go to your %windir%\system32. Can you see the folder we created in step 2? You can't. Why? Because this 32-bit Internet Explorer is actually viewing %windir%\SysWOW64 instead of the real %windir%\System32.

    6. Now run your 64-bit Internet Explorer. Press Ctrl+O. Click Browse. Go to %windir%\system32. Tada! We can see the folder now!

    Why in the world did Microsoft do this kind of confusing stuff? For backward compatibility reasons. Many software hardcoded itself to access %windir%\system32. If Microsoft simply created a system64, many software will need to be changed and recompiled (including those from Microsoft). And why are the names still user32.dll, gdi32.dll etc? Same reason, if the DLL name are changed, many source code will need to be changed before recompiling to 64-bit targets. By using the same directory and same naming, you do not need to change much in your software to retarget to 64-bit Windows.

    Finally, how do you know whether a running application is 32 or 64-bit? You can use Task Manager to find out. A 32-bit process has a "*32" behind it's name. Those without it are 64-bits.

    Wednesday, August 12, 2009

    Difference between Windows 7 RTM ISO from MSDN. Create a one-for-all Windows 7 RTM ISO (Install most editions with one DVD)

    MSDN provides subscribers various versions of Windows 7 RTM. According to Microsoft, all features are actually installed, no matter which edition you have in hand. For example if you have Windows 7 Home Basic, you can upgrade to Ultimate without getting another DVD. The extra features will be unlocked from your computer.

    Does that mean that all these ISOs are actually the same?

    For Vista, MSDN provided one DVD that included Vista Home Basic, Home Premium, Business and Ultimate. With this DVD, the Vista edition that will be installed depends on the product key you entered in Setup.

    But it is different this time. Installing from a Windows 7 Ultimate ISO, setup did not accept when I tried to put product keys from other editions.

    Out of curiosity, I did a binary compare on two Windows 7 ISO - Home Premium and Ultimate. The two ISO have the same size - 2,501,894,144 bytes. And the difference was only 75 bytes.

    I extracted the two ISO with WinRAR, both have 874 files and 200 folders. The total size was a little bit different. So I did comparison on all files with WinMerge. Turned out that the difference was only in one file - sources\ei.cfg. The contents of this file in Home Premium:


    And in Ultimate:


    So if you change only that file in the ISO, you do not need to download all editions. But the best solution is just delete ei.cfg, Setup will ask you which edition you want to install if it sees no such file.

    With tools like PowerISO, one can delete files in a ISO. The following shows the steps to create a One-For-All-Windows-7 DVD that you can use to install Windows 7 Starter, Home Basic, Home Premium, Professional and Ultimate. I don't know about other tools, but I used PowerISO for this.

    Step 0: Download one of the Windows 7 Edition (Starter, Home Basic, Home Premium, Professional or Ultimate). Note that Windows 7 Enterprise is not included in any of these ISOs.

    Step 1: Open the ISO with PowerISO (or any ISO tool of your choice) and delete sources\ei.cfg

    Step 2: Save the new ISO

    Step 3: Burn the ISO or mount it.

    Step 4: Try to install, you will see the following screen prompting "Select the operating system you want to install"

    Will Microsoft eventually release a one-for-all Windows 7 ISO as in Vista? I certainly hope so, it will save many DVDs, covers and storage space.

    On Testing Windows 7 in VMware Workstation

    If you are installing Windows 7 using VMware Workstation, you don't need to modify the ISO, Setup will prompt you for the edition. I tested on VMware Workstation version 6.5.2 and although it treats Windows 7 as Windows Vista, the installation went fine. I suspect that Windows 7 Setup changes its behavior whenever it finds itself living inside a Matrix world. Or maybe VMware Workstation has a way to "ask" Windows 7 Setup to let you choose the Edition ?

    Tuesday, August 11, 2009

    Upgrading from Windows Vista Ultimate 64-bit to Windows 7 Ultimate 64-bit

    Downloaded Windows 7 Ultimate 32-bit and 64-bit RTM from MSDN. Installed the 32-bit on a Core 2 Duo 2.66Ghz in a new partition. Took only less than half an hour. The boot and shutdown speed were apparently faster than Vista.

    But it spent at least 3 hours when I upgraded from Vista Ultimate x64 to Windows 7 Ultimate x64. The machine is a Core 2 Quad 2.66hz with 8gb RAM. Obviously it was no joke when Windows 7 setup said the upgrade may take several hours. At the time of the writing of this sentence, it was still being upgraded, forcing me to use another machine to blog.

    The final step of the upgrade - Tranferring files, settings, and programs - was the most time-consuming, took over 2 hours in my machine. And it showed that there were 1,003,453 items to be transferred. I wonder what constituted these 1 million items. One registry key = one item? Maybe I really have that many files.

    I ran the Windows 7 Setup from within Windows Vista. It warned me that SQL Server 2008 may not run properly if I upgrade without uninstalling it first. After some googling I decided to follow the advice and uninstalled it.

    Seems like most settings from Vista are left untouched after the upgrade. Unfortunately there is one big problem - all my pinned programs in Start menu were gone. I have to repin those programs. Anyone experienced this?