Delphi and Compact Framework

The Compact Framework is a version of .NET for Windows CE. It runs on Pocket PC 2002 and higher, on Smartphones with Windows Mobile 2003 or higher and, in theory, allows you to create applications for these devices using any .NET language. Unfortunately that doesn’t include Delphi 8 or Delphi 2005, which is a shame since without it there is no way to run Delphi code on your PDA. The next version of Delphi, set for release late in 2005, rectifies this deficiency. In the meantime, Borland has issued a preview release for registered Delphi 2005 users wishing to escape the desktop. Using the preview is an arduous process, as the following description shows, but it is a good way to learn more about mobile development with Windows.
Once the preview is installed, all you get is a new CFPreview folder in your Delphi 2005 application directory. Within it, there is an updated DCCIL.EXE, which is the Delphi .NET compiler, along with some library files and a readme. The readme gives instructions on how to hack the code from a Delphi 2005 Windows Forms project in order to get it to compile for the Compact Framework. There’s also an example command line for the new compiler, including a cryptic note that says you must specify where the .NET Compact Framework is installed.
What this note means is the location of the CF versions of the standard .NET Framework assemblies such as System.dll. Here you’ll need to understand a little about how .NET compilation works. In order to call an assembly in your .NET application, it has to be specifically referenced by the compiler, although some compilers disguise this by referencing a number of key assemblies by default. When compiling for the Compact Framework it’s essential to reference the CF versions of all the assemblies you use, by specifying them on the command line. Therefore, in order to use Delphi for the Compact Framework, the compiler must be told where these are.
In Visual Studio .NET 2003, the CF assemblies are in a folder called CompactFrameworkSDK. The full path in an installation is:Program Files | Microsoft Visual Studio .NET 2003 | Compact Framework SDK | v1.0.5000 | Windows CE.
This path is inconveniently long, and no use if you don’t have Visual Studio. It may also be out of date. It’s better to copy the latest versions of the CF assemblies from the Pocket PC itself. To do this, first make sure you have installed the emulator and tools for Windows CE development (see ‘Essentials for the Compact Framework’ on the next page). Run the emulator, install the latest .NET Compact Framework, then copy the assemblies from the emulator to your PC. I used the Remote File Viewer in EVC 4.0 for this. The assemblies you need are those beginning ‘GAC’ in the Windows directory. I copied them to a new directory located at C:\CFNET. Then you have to rename them to the core assembly name. For example: ‘GAC_Microsoft.WindowsCE.Forms_v1_0_5000_0_cneutral_1.dll’ becomes ‘Microsoft.WindowsCEForms.dll’.
Your first Delphi CF application
Now you are ready to create an application. Run Delphi 2005 and start a new Windows Forms application. This example calculates the VAT element in a VAT-inclusive price. I was targeting SmartPhone 2003, so I set the form size to 182x235, which are the values used by Visual Studio. Then I added code to implement a simple application and tested it in Delphi. The code needs tweaking to compile it with the CF preview, so I saved it and closed the Delphi IDE. Backup the project, in case you break it or need to reopen it in Delphi.
The next step is to open a command prompt and switch to the project directory. I created a batch file to do the compilation, following the guide in the readme.txt. I then deleted the project.cfg. If the compilation gets a bunch of errors starting with ‘Undeclared identifier: ‘SuspendLayout’’ you are on the right lines. Then I opened the main .pas file in my favourite editor (Borland’s discontinued Codewright, as it happens) and made the deletions suggested in the readme file. I still got an error: E2364 which indicates ‘Cross-assembly protected reference.’ The solution was to delete the temporary files created by the compiler and retry. Compilation successful, I then copied this to the Smartphone emulator, ran it, and got ‘NotSupportedException’. The issue here is the button. Buttons are not supported in the Compact Framework for SmartPhone, though they’re supported on Pocket PC. The reason is that buttons are not useful when you are operating solely through the keypad.
I revised the application, replacing the button with a menu. I gave the menu two menu items, Exit and Calc. Exit quits the application, and Calc fires off the tax calculation routine. Then I recompiled. The revised version works perfectly; Delphi code running on a Smartphone at last.
Running an application on a Smartphone
It’s not obvious how to run an application on the Smartphone emulator. If you use Visual Studio, the executable is deployed and run automatically when you debug, but with the Delphi Compact Framework preview you have no such luxury. I used the Remote File Viewer in Embedded Visual C++ 4.0 to export the executable to the device, choosing Storage | Windows | Start menu | Accessories for the location. The result is that the executable then appears as an item in the Accessories part of the Start menu, so you can run it from there. I also tried the application on a real a Qtek 8100 Smartphone. This is a Windows Mobile 2003 device made by HTC, who also manufacture the HP iPaq and the Orange SPV series. There’s no need to mess around with the Start menu on this device, since it comes with a File Explorer which lets you execute any file you choose.
There are other snags with real devices. When I first ran the Delphi application, a dialog warned that the executable was of unknown origin and gave me the opportunity to cancel. This particular phone is fully unlocked, but if you have a Smartphone from a network provider such as Orange or T-Mobile, it’s likely custom applications may be restricted or won’t run unless you can obtain a developer unlock.
Not for the faint of heart
The Delphi Compact Framework technical preview is not for casual use. Let me enumerate the main problems:
1. Massive downloads are required. You can mitigate these downloads by applying for a mobile development kit directly from Microsoft.
2. Documentation is skimpy beyond belief.
3. You need to manually post-process code generated by the Delphi IDE, or not use the IDE at all.
4. It’s fiddly to deploy to the emulator or device, and there’s no debugger.
5. There is no warning if you write code that is not actually supported on the device.
Although the next version of Delphi (which will include full support for the Compact Framework) should eliminate most of these issues, the jump from Windows to Windows CE will always be considerable. Even though it’s still a Windows program, it takes a while to work out exactly how to design the program effectively for small screens with limited input options. It’s also going to take them a bit of time to work around the existing limitations of the cut-down API and the small available memory.
Another factor is that once you get beyond utilities or games, interesting mobile applications need to communicate either with desktop applications or over the network. One possibility to accomplish this is to use XML web services, in which case Delphi is well positioned on both the client and on the server. It‘s also worth investigating SQL Server CE, which allows you to synchronise data between a device and a full SQL Server database. Note that although the current SQL Server CE doesn’t run on Smartphones, but only on Pocket PC devices, the forthcoming SQL Server Mobile Edition will run on all Windows Mobile 5 devices, including SmartPhone.
Many more annoyances are fixed in the forthcoming Compact Framework 2, including support for COM interop, XML serialization, a web browser control, and DirectX classes. COM interop in particular will solve the current releases frustrating difficulty in interacting with the data in Pocket Outlook. Unfortunately, the initial release of Delphi with Compact Framework support will be for version one.
Mobile futures Having Delphi for the Compact Framework is worth the pain. Today’s devices are vastly superior to early Windows CE efforts, and Windows Mobile 5 promises further improvement. In addition, keeping these things connected through Wi-Fi or a telecom network is now practical, which makes them more interesting as a development platform. Furthermore, non-visual code will often port easily. One developer working with the preview successfully compiled 16,000 lines of existing Delphi code and ran it on Pocket PC. Some hacking was needed, but it was still much easier than rewriting in another language. More important, this code can now be shared with Windows and ASP.NET applications.
Setting up your PC for mobile development involves hefty downloads.
To use Delphi for the Compact Framework, you will first need to install the SDK for the device you want to target. Critically, this provides an emulator skin so you can develop and debug on your PC. However, the SDK itself has dependencies. One approach is to download and install as follows:
1. ActiveSync 3.8 or higher.2. Embedded Visual C++ 4.0. This is a free C++ programming tool which also installs some useful tools. You also need the latest service pack (SP4 at the time of writing).
3. The SDK for your device. This might be Pocket PC or Smartphone, in 2002 or 2003 editions. You can install more than one SDK, but see below for the Windows Mobile 5.0 devices.
4. .NET Compact Framework 1.0 SP3 Redistributable. This lets you install or update the Compact Framework on a device or emulator.
If you have Visual Studio .NET 2003, you are already set up for CF development. However, you still need the right SDK for your device, and Embedded VC++ can be useful. Embedded VC++ is not used for Windows Mobile 5, so Microsoft points you to Visual Studio 2005, still in beta, along with ActiveSync 4.0 and the requisite SDK. You can also download the Windows CE 5 emulator separately. At the time of writing, there are no Windows Mobile 5 devices available, but they are expected before the end of the year.


