Getting Started with C# and the Compact Framework by Marc Rohloff

By: Marc Rohloff

Abstract: This article will show you the steps to edit, compile and test applications for the .NET Compact Framework using Borland Delphi 2005 and C# and explain what you can and cannot do.

Getting Started with C# and the Compact Framework

 

 

Many people on the Borland newsgroups have asked how to develop applications for compact devices using the Borland tools. This article attempts to explain what you can and cannot do, and to show you the steps required to edit, compile and test applications on devices that use the .NET Compact Framework. I will also show you how to set up the Windows CE Emulator and explain how Delphi 2005 can help you develop applications and what its limitations are.

 

Although it is possible to do a lot, the process is not currently very simple. In addition, since the Delphi compiler does not currently support the Compact Framework you are limited to using the C# Personality.

 

Note: I used Borland Delphi 2005 for my examples but the ideas and techniques should work equally well with C# Builder.

 

 

Using the Windows CE Emulator

 

If you are like me, you do not actually have a compact device, but are just curious enough to want to know what you could do if you did have one. Fortunately, Microsoft has made a Windows CE device emulator available on a trial basis. At the time of writing version 5.0 was available on Microsoft's website.

Before running the installation, make sure that you have an active internet connection. If you have a slow connection and do not yet have MSXML 4.0 sp 2, installed it also helps to download and install this first.

Once you have the emulator installed, make a copy of the menu shortcut and modify it. You will want to add some command line options. Mine looks something like this:

 

Emulator_500.exe nk.cem /vmid {6F5AE65B-98FF-46A9-A0E1-29B82C9779D4} /vmname "CE 5.0 (CF 1.0)" /sharedfolder c:\dev\netcf /video 240x320x16

 

The emulator uses the GUID specified by the vmid option to save the state of the device between sessions, while the sharedfolder option makes your c:\dev\netcf folder appear as a storage card on the emulated device. This makes it much easier to share and copy files from your desktop.

Windows CE Emulator in action

If you try it now you should see something like this:

Play around with it and make sure you know how it works. While you are at it, go and show your friends how cool you are.


Emulator Tips and Tricks

     If you change the command line parameters then you will need to delete the saved files from your My Documents\My Virtual Machines folder for the new parameters to take effect.

     Occasionally the emulator does not find the storage card when you start up. You can fix this by performing a soft reset.

     I have been unsuccessful in getting ActiveSync to work on the emulator, but if you can make it work, I would like to hear how you did it.


Installing the Compact Framework

 

Many devices, including the CE Emulator, come with version 1.0 of the Compact Framework installed. You can check the version on your device by opening the Run dialog and typing cgacutil. If you have latest service pack (sp 2) installed the version should be 1.0.3316.0.

 

If you do not have the framework installed then you can download the latest version from Microsoft's mobility website. The end-user version comes with an automatic installer but requires an ActiveSync connection.

If you do not have ActiveSync then you will need to download the developer version of the CF Framework. Unpack it, copy the appropriate CAB file to your device and open it from there to install it.

 

 

Copying the CF Assemblies

 

To be able to compile for the compact framework you need to have the assemblies available on your desktop.

 

You can copy them from your compact device or emulator. Open the \My Device\Windows folder, and make sure that all the hidden files are visible. Find all the dlls that that have names starting with "GAC_", there should be 11 of them, and then copy them onto your PC via the Storage Card. I copied mine to a folder called c:\dev\netcf\assemblies.

 

Lastly, rename the files as shown in the table below.

 

Filename on Compact Device

Required Filename

GAC_mscorlib_v1_0_5000_0_cneutral_1

mscorlib

GAC_System_v1_0_5000_0_cneutral_1

System

GAC_System.Data_v1_0_5000_0_cneutral_1

System.Data

GAC_System.Drawing_v1_0_5000_0_cneutral_1

System.Drawing

GAC_System.Net.IrDA_v1_0_5000_0_cneutral_1

System.Net.IrDA

GAC_System.Web.Services_v1_0_5000_0_cneutral_1

System.Web.Services

GAC_System.Windows.Forms.DataGrid_v1_0_5000_0_cneutral_1

System.Windows.Forms.DataGrid

GAC_System.Windows.Forms_v1_0_5000_0_cneutral_1

System.Windows.Forms

GAC_System.Xml_v1_0_5000_0_cneutral_1

System.Xml

GAC_Microsoft.WindowsCE.Forms_v1_0_5000_0_cneutral_1

Microsoft.WindowsCE.Forms

GAC_Microsoft.VisualBasic_v7_0_5000_0_cneutral_1

Microsoft.VisualBasic

 

 

Creating Your First Application

 

There is a lot to cover, so I will start with something simple to show you the basic concepts; however, developing applications that are more complex is more or less the same.

 

Start up the Delphi 2005 IDE and from the File New menu choose Windows Forms Application  C# Builder. I named my project FirstCFApp and set the location to c:\dev\netcf\FirstCFApp.

FirstCFApp Form

 

Shrink the form that the IDE created for you and add a label, setting its Text property to Hello World! (I told you it was going to be simple). Mine looks like the form on the left.

 


Environment Options settings We are almost ready to compile our CF application, but, before we do, let us look at how the Delphi IDE compiles the code.

 

Under the Tools menu choose Options, select the Environment Options node in the options tree, and make sure that you have enabled the Show Command Line.


Now if you build the project the compiler output should look something like this:

 

c:\windows\microsoft.net\framework\v1.1.4322\csc.exe

/noconfig

@C:\...\FirstCFApp\FirstCFApp.rsp

Response File Contents

/target:winexe

/debug+

/optimize-

/checked-

/unsafe-

/warnaserror-

/warn:4

/incremental+

/define:BDS;TRACE;DEBUG

/baseaddress:285212672

/utf8output

/out:C:\...\FirstCFApp\bin\Debug\FirstCFApp.exe

/res:C:\...\FirstCFApp.WinForm.resources

/r:c:\...\System.dll

/r:c:\...\System.Data.dll

/r:c:\...\System.Drawing.dll

/r:c:\...\System.Windows.Forms.dll

/r:c:\...\System.XML.dll

C:\...\FirstCFAppWinForm.cs

C:\...\FirstCFApp\AssemblyInfo.cs

 

To compile for CF.NET we need to execute the same command, but make sure that we reference the CF assemblies instead of the standard assemblies. For simplicity, I will leave out some parameters and not use a response file. Make sure you have saved all the project files and open a command prompt. (You can leave the IDE open for now).

 

First, make sure that you are in the directory with your project files:

 

cd c:\dev\netcf\FirstCFApp

 

I also create several environment variables to make the command line slightly shorter:

 

set fw=%windir%\microsoft.net\framework\v1.1.4322

set ass=c:\dev\netcf\assemblies

 

The first variable is the path to the .NET framework and the C# compiler (csc.exe) and the second variable is the path to the compact framework versions of the standard assemblies.

 

Now we can compile the project. The following is the command you need to enter:

 

%fw%\csc.exe

/noconfig

/nostdlib

/debug+

/optimize-

/define:BDS;TRACE;DEBUG

/out:FirstCFApp.exe

/res:FirstCFApp.WinForm.resources

/r:%ass%\mscorlib.dll

/r:%ass%\System.dll

/r:%ass%\System.Data.dll

/r:%ass%\System.Drawing.dll

/r:%ass%\System.Windows.Forms.dll

WinForm.cs

AssemblyInfo.cs

 

Before you run the command, let us look at some of the changes I made. The first thing you will notice is the /nostdlib directive. This directive, together with the /noconfig directive, tells the compiler not to use the standard assemblies or core library (mscorlib.dll). Because of this, we need to add a reference to mscorlib.dll manually. Also, notice that I have supplied the path to the compact framework versions of the .NET assemblies.

 

Type the command in and run it. Do not forget that it all has to be on a single line. You will notice that the compiler produces an error:

 

WinForm.cs(79,4): error CS0122: 'System.-STAThread-Attribute' is inaccessible due to its protection level

 

This may not look so good, but you always need to remember that many classes and methods are not available in the compact framework; this is just one of them. Go back to the IDE, comment out the line containing [STAThread] and save the file.

 

Now try compiling the application again. This time the compiler complains about several missing definitions. Again, you need to comment out the following six lines in the InitializeComponent method:

 

this.SuspendLayout();

this.label1.Name = "label1";

this.label1.TabIndex = 0;

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.Name = "WinForm";

this.ResumeLayout(false);

Completed App

Now if you save the file and compile again everything should be OK.

However, how do we know that everything worked? Well there is only one way to find out

 

Start up the emulator or copy the executable to your device and execute it from there.

Now are you convinced?

 

By now, you are probably thinking that this is far too complicated. It is true that you can simplify things by creating batch files, but let us see if we can get the IDE to help us some more.



The IDE Form Designer and Compact Framework Code

 

You will notice that you can still switch to the designer in the IDE and continue to make changes to the form. This may sound too good to be true. Well it is. If you go back to the code window, you will notice that all those properties that you carefully commented out have all mysteriously reappeared.

 

You will find that after you have got your screen layout stable it is easier to make your changes in the code window and not use the designer.

 

 

Compiling in the IDE

 

It turns out that with a little bit of work we can get the IDE to compile for us. What we need to do is to make the same changes we did when we used the command line compiler, so lets close the command prompt and go back to the Delphi 2005 IDE.

 

Firstly, we will make our project reference the correct assemblies:

Note: Once you have changed the references, you will no longer be able to view your form in the form designer.

 

Remove all of the references in your project. Then choose Add Reference from the project menu but instead of selecting from the list of assemblies choose the Browse button and go to the directory with your compact framework assemblies (c:\dev\netcf\assemblies).

You can control-click here to select all the assemblies you need. In our case, we need System.dll, System.Data.dll, System.Drawing.dll and System.-Windows.-Forms.-dll. Most importantly, you will also need to include mscorlib.dll manually just as we did when using the command line to compile.

Condition Defines Project Option Setting Next, open the Project Options and modify the Conditional Defines to include a space and then the value /nostdlib. This will trick the compiler into not including the standard mscorlib library.

 

Now compile your project from the IDE and you will see that it compiles just like a normal C# application except that you will get the compiler errors for missing CF classes and methods and the executable will be able to run on a compact device or emulator.

 

 

Debugging

 

Making this last set of changes gives you almost complete integration into the IDE. Apart from running your application on a compact device, you can also run your application from within the IDE and debug it as if it where a normal C# application and without making any other changes.

 

You cannot however debug code while running it on a compact device or emulator. In addition, if the code uses components that will only run on your device then it will not be possible to run it in the IDEs debugger.

 

 

One Last Thing

 

One last thing you may want to do is remove the label and its field to create an empty application. Once you have compiled it successfully, choose Add to Repository from the Project menu. Create a new category under C# Projects called Compact Framework and add your blank application there.

You can now create a new Compact Framework project simply by selecting it from the File New menu.

 

What about all those missing classes

 

You will soon discover that the number of classes and methods missing in the Compact Framework is quite large. Unfortunately, the IDE does not provide much help as the code insight features still show classes and methods that are not available to you.

 

The Microsoft Help files are your friend here. The documentation for each class and method has a requirements section at the bottom listing the platforms on which the class is available.

 

What to do if a class or method is not available

 

Many of the classes and methods, such as those we have commented out, are simply not necessary because of the simplicity of Compact Devices, for example, none of the code-access security classes and attributes is available.

 

In several cases, classes and methods may not be available but there are alternatives that you can use instead. For example :

Array.Copy(sourceArray, destinationArray, length)

Can easily be replaced with a call to

Array.Copy(sourceArray, sourceIndex,

destinationArray, destinationIndex, length)

 

Lastly, Microsoft has not implemented many classes or methods such as serialization and remoting. For these you will simply have to find workarounds or write your own.

There is still hope

 

There are several resources on the internet with replacements. One good site is www.OpenNETCF.org which has a large set of the missing classes in their Smart Device Framework. In addition, when Microsoft releases version 2.0 of the .NET framework it will support many more features.

 

Some good news is that the compact framework also comes with several new classes to try out, like the InputPanel and MessageWindow, as well as Infrared support.

 

Summary

 

Using the C# personality, you can create initial form designs, edit code and get help from most of the IDEs features.

 

Although not yet at the level of Visual Studio integration it is still possible to develop, compile, test and debug applications easily using the Borland Delphi 2005 IDE.

 

 

About the Author

 

Marc Rohloff occupies himself doing pretty much anything related to IT. He currently specializes in .NET technologies and is a member of TeamB. He has worked in many odd corners of the world, the current one being in Columbus, Ohio where he is on contract as a Delphi Mentor. You can find out more about him at www.marcrohloff.com.

 



Server Response from: ETNASC02