Thursday, July 19, 2007

Javascript "Empty Statement"

Spot anything wrong in the Javascript below?
var a=[];
//initialize array
for(int i = 0; i < 10; i++);
a[i] = 100;

Values in array a will be:
[undefined, undefined, 100]

The semicolon at the end of the for loop forms an "Empty Statement". So a[i]=100; is actually outside of the for loop!

Wednesday, July 18, 2007

Reload Magellan MobileMapper CE OS Firmware

Where to download MobileMapper CE OS Firmware Image

Some Magellan official documentations have this out-dated information about location of MobileMapper CE OS firmware download location:
"The current version of MobileMapper CE Operating System (OS) firmware can be downloaded from in the /Mobile Mapping/MM CE/Firmware/OS Firmware/ folder."
This turned out to be out-dated information. The above directory does not exist on the FTP server. After some digging, I found the OS firmware in this zip file:
Notice: the above FTP location is accurate as 7/18/2007, may be changed by Magellan.

What Happened

I saw low battery warning on the device. Naturally, I select "Full Shut Down" to allow the system do a proper backup. This turned out to be a mistake. Apparently, battery was out before the device can fully shut-down, and the OS firmware image is corrupted. After this, upon each reboot, I am asked to calibrate stylus, then there are 2 dialogs:
Dialog 1: dialog title: "Warning", Text: "This is a Rescue Image! The
Main Image failed to start. Press "Cancel" to Retry Main Image. Press
"OK" to continue with Rescue Image."
After I press "OK" which is the only button on the dialog, I am lead to
the second dialog:
Dialog 2: dialog title "OS Loader", Text: "Please choose a new image
file. The image file cannot be in the MyDevice directory."
After I press "OK", an "Open File" dialog appears with option to load a
file type: Image Files (*.nbx).

Following the instruction in "Getting Started Guide", page 61 "Updating MobileMapper CE OS Firmware", I was able to load the OS image I downloaded from Magellan. The system seems to be back to proper working order afterwards.

Lesson Learned

When you have low battery warning, rather than to risk corrupt the OS firmware image, it is probably better to wait for the battery to die out or, if possible, recharge the battery immediately.

Friday, July 13, 2007

Invoke CTRL-ALT-DELETE on Remote Desktop

When you are connected to other Windows computer using Remote Desktop Connection, the combination CTRL-ALT-DELETE will invoke the local computer's logon screen or task manager. Use CTRL-ALT-END to invoke it on the remote computer.

Thursday, July 12, 2007

Delay Load DLL and __HrLoadAllImportsForDll

Here are a few tips for delay loaded DLLs based on my experience with Visual Studio 2003 (Visual C++ 7.1)

  1. Delay load can be easily setup using the Visual Studio IDE by the following steps:
    • Open project property page: Linker > Input
    • In Additional Dependencies, add library: delayimp.lib;
    • In Delay Loaded DLLs, add all the DLLs you want to delay load.
      TIP #1: if you have more than one DLLs, the file names must be separated by ";", e.g.: rapi.dll;ceutil.dll. If you forget this, and try something like: rapi.dll ceutil.dll, then the linker will treat the whole string as the file name of a single file and fail to find a file with that name. There will be a subtle warning during build: LINK : warning LNK4199: /DELAYLOAD:rapi.dll ceutil.dll ignored; no imports found from rapi.dll ceutil.dll, which most likely will be ignored.
  2. Detect if the delay loaded DLLs exists on the host computer:
    The whole purpose of delay loaded DLLs is to allow our program to be more flexible. It can be loaded on computers that do not have certain DLLs, and it can choose to use those DLLs when they are present. So, there is always a need to know if we can load the delay loaded DLLs on the host computer. __HrLoadAllImportsForDll is the recommended function by MSDN documentation to do the job.
    TIP #2:Do not use __HrLoadAllImportsForDll because it use memcmp to compare DLL file names byte by byte which is too strict. File names in Windows system is not case sensitive, so the file names can be the same even if memcmp fails. This function will return module not found error code even when the DLL files are installed on host computer. I end up lift the implementation from delayhlp.cpp, and replace memcmp with _tcsicoll which is case insensitive.

Friday, July 06, 2007

Regular Expression Designer

Writing regular expression is always tough for me, not to mention trying to read regular expressions written by other programmers. I found this excellent FREE tool: Regular Expression Designer provided by a Sydney based company.

As you can see from the screen-shot below, it makes it very easy to experiment and visually inspect output of regular expression applied against various text input. You can try MATCH, SEARCH or REPLACE with one click. Match results are displayed in a tree format, so that you can instantly see all the matches and captured groups. An online help of regular expression language elements is provided as a quick reference.

This application only runs on Windows computer with .NET Framework 1.1 or 2.0.

Thursday, July 05, 2007

Boost.Regex with MFC/ATL in Visual Studio 2003 or Above

Searching, parsing, and validating text input is always a big part of contemporary programming. Regular expression is a powerful tool that is available to C#, Java, Perl, ... Unfortunately, it does not come with C++ or MFC library. One of the best regular expression library in C++ is Boost.Regex, which is accepted as part of C++ standard library in the next release of C++ standard. Boost.Regex library provides direct support for MFC/ATL string types, so that integration with MFC/ATL project is very straightforward. However, integrating BOOST library into Visual Studio 6.0 or older is very hard, due to their lack of standard C++ support.

To use Boost.Regex in a MFC/ATL project in Visual Studio 2003 or above is very easy. Just follow these steps:
  1. Download installation package created by Boost Consulting, following the links: Products > Free Downloads, you will find installer for latest BOOST library distribution. Just run the installer, it will automatically download the selected BOOST library source code from a and pre-compiled binaries from a mirror site;
  2. Setup VC++ include and library directories for BOOST libraries, detailed instructions can be found here: BOOST--Getting Started on Windows;
  3. If you are like me, getting very comfortable using MFC/ATL string types like CString, CStringW, TCHAR, _T, ... Please notice BOOST libraries normally only support standard string type. Please read this before you start to write code: Boost.Regex--Working with MFC/ATL String Types. Boost.Regex does provide a set of overloaded functions that use MFC/ATL types, you will need to include a header file: <boost\regex\mfc.hpp> which defines all the overloaded Boost.Regex APIs as shown in the document;
A sample code:
   1://boost regular expression library
2:#include <boost/regex.hpp>
3://support for MFC string types in boost Regular Expression library
4:#include <boost/regex/mfc.hpp>
6://extract string value from XML by given key
7://Example: given the string below, and key="LogFile"
8://<add key="LogFile" value="c:\temp\error.txt" />
9://return will be: c:\temp\error.txt
10:CString GetXMLValue(LPCTSTR xmlContent, LPCTSTR key)
12: CString strRegEx;
13: strRegEx.Format("<add\\s+key\\s*=\\s*\"%s\"\\s+value\\s*=\\s*\"([^\"]*)\"\\s*/>", key);
14: tregex re(strRegEx);
15: tmatch matches;
17: //we are expecting no more than 1 match in the file
18: if(regex_search(xmlContent, matches, re))
19: {
20: //extract the value from sub-expression
21: return CString(matches[1].first, matches[1].length());
22: }
23: else
24: {
25: return "";
26: }

Static link to Boost.Regex library for the above sample code, adds about 80K to the compiled binary output.
Official documentation of Boost.Regex library:

Tuesday, July 03, 2007

CeCopyFile (RAPI) Requires Both Files on Remote Device

There is no mention of this restriction in MSDN documentation, but be aware that CeCopyFile requires both lpExistingFileName and lpNewFileName refer to files on remote device. If you want to copy files from PC to remote device or from remote device to PC, you can refer to samples: Pget, and Pput provided in Windows Mobile SDK (in Mobile 5.0 SDK, the samples are under: Samples\CPP\Win32\Rapi).

Definition of CeCopyFile:
BOOL CeCopyFile(
LPCWSTR lpExistingFileName,
LPCWSTR lpNewFileName,
BOOL bFailIfExists

Monday, July 02, 2007

C++ Primer (Recommended Book)

Title: C++ Primer (I own the 3rd Edition)
Authors: Stanley B. Lippman, Josée Lajoie

Bottom Line: A keeper. Highly recommended. With its accurate and complete coverage, concise text and short code samples, this book serves as a good C++ reference book for intermediate or advanced C++ programmers. But definitely not a book for beginners.

This is the 1st and the only C++ book I have. The books has been on my shelf for 6 years. I was already familiar with C++ when I bought this book, and I find this book very useful through out the years. It covers EVERY fine details of standard C++, and the information is ACCURATE and CLEAR.

At first, I spent a few weeks browse through it. I found it has tremendous amount of new information for me -- an self-taught intermediate level C++ programmer.

But, if you are beginner trying to learn C++ and Object-Oriented programmer, I would recommend you to find another book.For one thing, in many places, there are terms referencing concepts in future chapter that make you having to read the book in two passes to understand it. Then there are too much fine details about C++, and too little practical guides. Even if you manage to read the book, it could easily take you a full year to finish this book and emerge in all the intricacy about C++, then come out not able to write a single program. There is NO full-blown sample program, nor step by step guide to setup development environment.

Due to its sheer volume and broad coverage, I didn't read the book cover to cover. Instead, I kept this book as a C++ standard reference. Whenever I have doubt, I lookup the index and read the related chapters. The books is written in concise and clear language, and has coverage of many minor details that cannot be easily find elsewhere. Having to juggle between C, C++, C#, Javascript, and SQL, I constantly need to refresh my memory about subtle C++ issues. To be honest, even if I work with C++ every day, I do not think I can master the full-depth of C++ language. C++ is a powerful language that provides many opportunities for programmers to express their ideas. This books is the perfect reference tool for me, it provides all the details about a language feature with enough details (but not too long to read) to remind me of the key points very quickly and precisely.

If this book is not a keeper that I feel deeply indebted to, I would not have the urge to write about it. :)