'Freiburg' porting an Tcl extension to C#

This paper describes the porting of the Tcl Freiburg client to C#.


1. What is C#


C# is a new programming language introduced by Microsoft as answer of the growing popularity of Java from Sun Systems

the C# Framework is available in a GNU edition called mono and a original edition called Microsoft .NET Framework SDK


2. Compare C# and Freiburg


C# as a very new programming languages introduces a couple of new technology's including a language independent abstraction layer available for C#, Visual Basic, JScript and C++ and other. Freiburg was born to provide a language independent interface to services usable for Unix, Windows and Mac-OS. There are at least a common strategic target between .NET and Freiburg but available with two totally independent technologies:

  • .NET : mapping all supported programming languages into a common language called Common Language Runtime or short CLR using a language independent byte-code
  • Freiburg: using a technology layer to link different services (called server) into different programming languages (called clients) using a data-bus

From the programmer point of view everything looks equal but Freiburg supports all available languages and C# only a limited range


3. link the libFreiburg with Tcl and C#


Tcl
Tcl has a C library and C-API providing all features needed to link a C library with Tcl. search this wiki for additional information. (is a link available?)

C#
the C# way to link a dynamic library with the language is totally different
  1. no C-API available
  2. every link between a C# Method and a library function is done 'on the fly' using Marshalling of native C data types with C# data types
  3. C# directly support common C language features likes struct and pointer using the unsafe keyword

everything sounds very good for C#, but the problems are in detail

  • If you are using SWING to create the C# interface something interesting happen. SWING creates a C interface library and a C# wrapper for this library. (bad)
  • To avoid using pointers in C# every pointer like operation have to be transformed into a procedure operation -> this will blow up your interface (slow and bad)
  • you end up with an interface using C# and a interface using C (double workload)
  • to avoid double interfaces you end up with unsafe code (non portable in C#)
  • to create a fast interface unsafe code is needed with 2 restriction's:
  1. private pointers are not type safe because only one C# type IntPtr (e.g. void*) is available (very bad)
  2. public pointer are type safe because the public C structs can be included into C# using C# syntax -> every public C struct have to be duplicated into C#, every change on one side have to be reproduced on the other side (very very very bad)