Creating Compact Framework Applications in RAD Studio 2007

By: David Clegg

Abstract: This article describes how to create Delphi for .NET applications targeting the .NET Compact Framework using RAD Studio 2007

    Introduction

In Borland Developer Studio 2006, and Delphi 2005 (with the help of the Compact Framework compiler preview), it has been possible with a little work to create Delphi for .NET applications which target the Compact Framework. This article will outline the steps required to get up and running with creating Compact Framework applications using RAD Studio 2007.

    Setting up the Development Environment

    Installing the Compact Framework Assemblies

Unlike previous versions of Delphi, RAD Studio 2007 does not include .dcpil files for the Compact Framework assemblies, nor does it include the .dcuil files for the CodeGear assemblies and units compiled to target the Compact Framework.

With the .NET Framework 2.0 SDK, Microsoft has bundled an installer for the Compact Framework SDK, so obtaining these assemblies is a lot easier than in the past. The installer for this is called netcfsetupv2.msi, and should be located under the \Program Files\Microsoft.NET\SDK\v2.0\CompactFramework directory.

By default, the installer will place the SDK under a \Program Files\Microsoft.NET\SDK\CompactFramework\v2.0 directory (note the difference from the path where the installer resides). The assemblies we need to reference reside in the WindowsCE sub-directory.

    Creating Borland.Delphi.System.dcuil

As mentioned above, RAD Studio 2007 does not include the .dcuil files for the CodeGear assemblies and units compiled to target the Compact Framework.

In the case of the Compact Framework assemblies we can get around this problem by linking against the assemblies directly, but in order to create Delphi for .NET applications we will need to create a .dcuil file by compiling the Borland.Delphi.System.pas unit.

As all Delphi for .NET applications will need to link this file, we will create a CF subdirectory under the existing RAD Studio lib directory and place the .dcuil file in there. This can be done by invoking the dccil compiler using the following command-line. You will probably need to change the RAD Studio rtl and lib directory paths so that they point to the valid locations for your RAD Studio installation.

dccil --clrversion=v2.0.50727 -DCF -m -q -nsBorland.VclCF 
-nsBorland.Vcl -ln"D:\CodeGear\RAD Studio\5.0\lib\CF" 
-W-UNIT_PLATFORM -W-SYMBOL_PLATFORM 
-lu"D:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\mscorlib.dll" 
-lu"D:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\System.dll" 
-lu"D:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\System.Xml.dll" 
-lu"D:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\System.Drawing.dll" 
-lu"D:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\System.Windows.Forms.dll" 
-lu"D:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\System.Data.dll" 
-lu"D:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\System.Windows.Forms.DataGrid.dll" 
-lu"D:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\System.Web.Services.dll" 
-lu"D:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\System.Net.IrDA.dll" 
-N"D:\CodeGear\RAD Studio\5.0\lib\CF" 
-U"D:\CodeGear\RAD Studio\5.0\lib\CF" 
-LN"D:\CodeGear\RAD Studio\5.0\lib\CF" -z --no-config -Y 
- "D:\CodeGear\RAD Studio\5.0\Source\DotNet\rtl\borland.delphi.system.pas"

NOTE: In order to preserve the formatting in this article, many of the parameters in the above command line have had CRLFs appended to them. These will need to be stripped out manually before invoking the command.

Once this command has been invoked, we should have a Borland.Delphi.System.dcuil file created which we can then reference in all our subsequent Compact Framework applications. We should also have .dcpil files for all the referenced Compact Framework assemblies, which can be referenced instead of referencing the assemblies directly.

    Configuring the Emulator

For those developers not lucky enough to own a Compact Framework compatible device, an emulator will be required in order to test the programs created with the compiler. While the Compact Framework SDK is now included with the .NET Framework 2.0 SDK, it doesn’t include any emulator images.

This article will use an emulator from the Windows Mobile 6.1 Professional emulator images made freely available on the Microsoft Download Center. I would recommend using these images rather than the Windows Mobile 6.1 Standard emulator images, as the standard ones do not have touch screen support, so the operating system and applications must be manipulated by using the emulator buttons.

Once the emulator images have been installed, standalone emulators can be be invoked from the Start Menu under All Programs->Windows Mobile 6->Standalone Emulator Images.

Hide image
Click to see full-sized image

In order to copy our Delphi for .NET applications to the emulator, we can configure a shared directory. This will be exposed on the emulator as a storage card which can be navigated to by using File Explorer. This is done in the Emulator Properties dialog, which can be accessed by selecting File->Configure… from the Emulator main menu.

Hide image

    Creating a Compilation Batch File

We will set up a batch file to assist us in compiling Compact Framework applications. The batch file we will use looks like this:

@echo off
rem **************************************************************
rem ** Batch file to be used from the RAD Studio 2007 IDE Tools 
rem ** menu to invoke the Delphi for .NET compiler to compile 
rem ** Compact Framework applications. It takes two parameters 
rem ** as follows:
rem **  %1 - Name of project .dpr file
rem **  %2 - Path to project directory
rem **************************************************************

set _DCCILPATH_="D:\CodeGear\RAD Studio\5.0\Bin\dccil.exe"
set _CFUNITS_="D:\CodeGear\RAD Studio\5.0\lib\CF"
set _PROJECTNAME_=%1
set _PROJECTDIR_=%2
set _OUTPUTDIR_="D:\Source\Delphi.NET\CF\Shared"

rem *************************************************************
rem ** Ensure the RAD Studio 2007 Bin directory is in the path. 
rem ** It may have been removed by a User path override.
rem *************************************************************
set _BDSDIR_=D:\CodeGear\RAD Studio\5.0\Bin
path %_BDSDIR_%;%PATH%

rem *************************************************************
rem ** Cater for the fact that paths with spaces can't be passed
rem ** in from the Tools menu
rem *************************************************************
SHIFT
SHIFT
:LOOP
IF "%1" == "" GOTO END
set _PROJECTDIR_=%_PROJECTDIR_% %1
SHIFT
GOTO LOOP
:END

rem *************************************************************
rem ** Change to the project drive and directory
rem *************************************************************
FOR /F "tokens=1 delims=\ " %%A IN ('echo %_PROJECTDIR_%') DO SET _DRIVE_=%%~dA
%_DRIVE_%
cd %_PROJECTDIR_%
del *.dc?il

%_DCCILPATH_% -DCF --no-config "%_PROJECTDIR_%%_PROJECTNAME_%" -u%_CFUNITS_% -luSystem.Windows.Forms -luSystem.Data -E%_OUTPUTDIR_%
pause

The batch file takes two command line parameters, with the first one being the file name of the project to compile, and the second one being the path where the project resides. For the purpose of this article, only the System.Windows.Forms.dll assembly needs to be referenced, but additional assemblies can be referenced by adding extra -lu parameters.

It caters for the fact that the RAD Studio directory may not be in the PATH variable accessible to the batch file when run within the context of the IDE (this can be overridden). It also handles project paths that may contain spaces, as this information cannot currently be passed in as one parameter from the built-in macros available when configuring an item on the IDE tools menu.

It also uses the –-no-config parameter to ensure the compiler doesn’t use the contents of the dccil.cfg file. Failing to do this may result in the compiler linking to the full version of the .NET framework DLLs, which will prevent any binaries from executing on the Compact Framework.

Once this batch file has been created, we can configure the IDE so it can be invoked for the current project from the Tools menu. To do this, select the Configure Tools... option from the Tools menu, click the Add... button, and enter the following information:

Title: Delphi.NET CF Compiler
Program: <Fully qualified path to batch file>
Parameters: $SAVEALL $NAMEONLY($PROJECT).dpr $PATH($PROJECT)

Hide image

Click the OK button, and you should now have a Delphi.NET CF Compiler option at the end of the RAD Studio Tools menu. When invoked, it will prompt for all changed files to be saved, and will call our batch file, passing in the Project .dpr file name and full path information.

    Developing and Running a Delphi for .NET Application

    Creating the Project

We are now ready to create our first Delphi for .NET Compact Framework application. Start up RAD Studio 2007, and select File->New->Other… from the IDE main menu. Select the Delphi for .NET Projects node and double-click on the Console Application icon. Save the project as HelloWorld.dproj.

    The Project Source

Add a new unit to the project, and save it as MainForm.pas. Paste the following code into the Code Editor:

unit MainForm;

interface

uses
  System.Drawing, System.Collections, System.ComponentModel,
  System.Windows.Forms;

type
  TMainForm = class(System.Windows.Forms.Form)
  strict private
    Components: System.ComponentModel.Container;
    Label1: System.Windows.Forms.Label;
    Button1: System.Windows.Forms.Button;
    Label2: System.Windows.Forms.Label;
    procedure InitializeComponent;
    procedure Button1_Click(sender: System.Object; e: System.EventArgs);
  strict protected
    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;

implementation

{$AUTOBOX ON}

procedure TMainForm.InitializeComponent;
begin
  Self.Label1 := System.Windows.Forms.Label.Create;
  Self.Button1 := System.Windows.Forms.Button.Create;
  Self.Label2 := System.Windows.Forms.Label.Create;
  //
  // Label1
  //
  Self.Label1.Location := System.Drawing.Point.Create(16, 16);
  Self.Label1.Text := 'Hello World!';
  //
  // Button1
  //
  Self.Button1.Location := System.Drawing.Point.Create(16, 40);
  Self.Button1.Text := 'Click Me!';
  Include(Self.Button1.Click, Self.Button1_Click);
  //
  // Label2
  //
  Self.Label2.Location := System.Drawing.Point.Create(16, 72);
  Self.Label2.Size := System.Drawing.Size.Create(152, 23);
  //
  // TMainForm
  //
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Controls.Add(Self.Label2);
  Self.Controls.Add(Self.Button1);
  Self.Controls.Add(Self.Label1);
  Self.MinimizeBox := False;
end;

procedure TMainForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;

constructor TMainForm.Create;
begin
  inherited Create;
  InitializeComponent;
end;

procedure TMainForm.Button1_Click(sender: System.Object; e: System.EventArgs);
begin
  Label2.Text := 'Delphi is in the building!';
end;

end.

Select Project->View Source from the IDE main menu, and replace the contents of the .dpr with the following code:

program HelloWorld;

uses
  System.Windows.Forms,
  MainForm in 'MainForm.pas';

begin
  Application.Run(TMainForm.Create);
end.

    Compiling the Project

We are now ready to compile our Compact Framework application. Select the Delphi.NET CF Compiler option from the Tools menu:

Hide image

This will invoke the batch file we created earlier, after prompting for any changes to be saved. The output is shown below:

Hide image
Click to see full-sized image

    Running the Application

We are now ready to run our application on the Windows Mobile emulator. It should have been compiled to, and be visible from, a storage card on the emulator. To navigate to this, click the Start button on the emulator, select Programs, and click on the File Explorer icon. By default, this will place us in the My Documents folder. In order to get to the storage card, click the drop down arrow next to the My Documents text at the top of the screen, and select Storage Card

Hide image
Click to see full-sized image

You should be shown the contents of the storage card (our shared folder), and see an entry called HelloWorld. Click on this entry and the application should run. Clicking the Click Me! button should yield the following output :-

Hide image
Click to see full-sized image

    Summary

While not officially supported, it is still possible to create Delphi for .NET Compact Framework applications using RAD Studio 2007. This article has shown how you can get started with doing this.

    Acknowledgements

A big thank you to Seppy Bloom for help with some of the technical aspects of this article. And thanks to Rossen Assenov for providing the incentive to write it in the first place.

Server Response from: ETNASC01