free web page counters

Windows Mobile Pocket PC Smartphone Programming

==>Click here for the SiteMap<==. Original contents with decent amount of source codes.

Sunday, April 30, 2006

Trap of Copying Project Settings in Visual Studio 2005 (/Machine: ARM vs. Thumb, /subsystem: windowsce, 4.02 vs 5.01)

====>SiteMap of this Blog<===

Trap of Copying Project Settings in Visual Studio 2005 (/Machine:ARM vs. Thumb, /subsystem:windowsce,4.02 vs 5.01)

Visual Studio 2005 offers developers with a nice option when a new project platform needs to be created: to copy project settings for one platform to the new platform.

How Copying Project Setting works?

Typical use case is that you already have a project for Pocket PC 2003, and you want to create a new project targeting Pocket PC 5.0 device. You can bring up "Project Settings" dialog, and click "Configuration Manager" button to "New" the project for 5.0 platform, and opt to copy from the project setting for 2003 platform.

Visual Studio takes care quite a few changes automatically, for example, output directory, windows CE version, and PPC/Smartphone platform. The mechanism to cover the changes is using Macros extensively. Below are two typical macros:

  • Windows CE version difference: $(CEVer). 4.20 for 2003 platform, and 5.01 for 5.0 plaform.
  • Platform difference: $(CEPlatform) / $(CePlatform) / $(PLATFORMDEFINES). WIN32_PLATFORM_WFSP for Smartphone, and WIN32_PLATFORM_PSPC for Pocket PC.

Issue of Blindly Copying Project Settings from Newer Platform to Older Platform

It is worthy noting that VS2005 does not adjust two important options for its linker while copying project settings:

  • /SUBSYSTEM option, which tells the targeting OS how to run the .exe file, and affects the entry point symbol (or entry point function) that the linker will choose.
  • /MACHINE option, which specifies the target platform for the program, for example, X86, X64, IA64, MIPS, SH4, and etc.

If you create a new Smart Device project, possibly /MACHINE is not set explicitly, and /SUBSYSTEM is hard coded to be the number most suitable for the project. For example, if you create a project for Pocket PC 5.0 platform, /MACHINE is not set (so default to THUMB), and /SUBSYSTEM is set to 5.01.

If such settings are blindly copied to another project for older platform (in this case, Pocket PC 2003), the final binary won't be executed by PPC2003 device.

Walkthrough of the issue

1. A new solution is created, and a new Smart Device project targeting Pocket PC 5.0 platform is created.

  • "Project Setting"=>"Linker"=>"System"=>"SubSystem" is not set.
  • "Project Setting"=>"Linker"=>"Advanced"=>"Target Machine" is not set either.

Command line option will be like " /subsystem:windowsce,5.01"

Do a "dumpbin /all" on the executable generated by VS2005, you'll see something like this, meaning the binary is for Thumb machine and CE version should be 5.01 or higher.

FILE HEADER VALUES
1C2 machine (Thumb)
OPTIONAL HEADER VALUES
5.01 subsystem version

2. The executable for Pocket PC 5.0 runs great in a 5.0 device. Now you want propagate its success to Pocket PC 2003 devices, so a new project targeting PPC2003 platform is created using Configuration Manager. In its "Active solution platform" dropdown list, you select "New", and "Copy settings from" the Pocket PC 5.0 platform.

3. For the new project to build, you may need to add "secchk.lib" to lib dependency list if you have at least one function call, and add "ccrtrtti.lib" to lib dependency list if you have at least one C++ source file. However, if you run the executable, you'll get the infamous error dialog:

4. Do a "dumpbin /all" on the executable for PPC2003 platform, you'll see that the executable is for THUMB machine and CE OS must be 5.01 or higher, exactly the same as the one for PPC5.0 device. To fix it, you need to change the linker option to something like " /subsystem:windowsce,4.20 /machine:ARM".

Rebuild and you'll get a good executable with the following header values:

FILE HEADER VALUES
1C0 machine (ARM)
OPTIONAL HEADER VALUES
4.20 subsystem version

Notice you must change both /subsystem and /machine, otherwise it won't work!

Wrap Up

VS2005 seems to copy the project setting literally. It resolves the fundamental differences (OS, PPC or Smartphone, directories) by expanding Macros, for example, $(CEVer) and $(CEPlatform). However, /MACHINE and /SUBSYSTEM are either not specified, or hard-code to be a number, resulting in wrong values to be used in the newly-created project. Such mismatch probably is not a big issue if the new project is for newer platform (for example, copying PPC2003 settings to PPC5.0 project), but definitely causes headache is the new project is for an older platform (for example, copying PPC5.0 settings to PPC2003 project).

A Side Topic: The THUMB instruction set consists of 16-bit instructions that act as a compact shorthand for a subset of the 32-bit instructions of the standard ARM. The binary built for THUMB is typically with the size roughly 70-80% that of ARM. Unfortunately binary compiled for THUMB cannot be executed in Windows Mobile 2003 devices.

====>SiteMap of this Blog<===