summaryrefslogtreecommitdiff
path: root/Doc/Manual/CSharp.html
blob: 67b3beed80a86d8fe419b0d2c330550a843e6f9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SWIG and C#</title>
</head>
<body bgcolor="#FFFFFF">
<a name="n1"></a><H1>15 SWIG and C#</H1>
<!-- INDEX -->
<ul>
</ul>
<!-- INDEX -->

The purpose of the C# module is to offer an automated way of accessing existing C/C++ code from .NET languages.
The wrapper code implementation uses the Platform Invoke (PINVOKE) interface to access natively compiled C/C++ code.
The PINVOKE interface has been chosen over Microsoft's Managed C++ interface as it is portable to both Microsoft Windows and non-Microsoft platforms.
PINVOKE is part of the ECMA/ISO C# specification.

<p>
The C# module is one of the more recently added language modules to SWIG and consequently is missing some functionality available in some other SWIG modules.
The C# module is very similar to the Java module, so until some documentation has been written,
please use the <a href="Java.html">Java documentation</a> as a guide to using SWIG with C#.
The rest of this chapter should be read in conjunction with the Java documentation as it lists the main differences.

<p>
Director support (virtual method callbacks into C#) has not yet been implemented and is the main missing feature compared to Java.
The other missing feature is the lack of typemaps for wrapping STL code as well as a few minor utility typemaps in the various.i library.

<p>

The most noteable differences to Java are the following:
<ul>

<li>
When invoking SWIG use the <tt>-csharp</tt> command line option instead of <tt>-java</tt>.
</li>

<li>
The <tt>-package</tt> command line option does not exist. The <tt>-namespace &lt;name&gt;</tt> commandline option will generate all code into the namespace specified by <tt>&lt;name&gt;</tt>.
</li>

<li>
C/C++ variables are wrapped with C# properties and not JavaBean style getters and setters.
</li>

<li>
Global constants are generated into the module class. There is no constants interface.
</li>

<li>
There is no implementation for type unsafe enums - not deemed necessary.
</li>

<li>
The default enum wrapping approach is proper C# enums, not typesafe enums.
<br/>
Note that %csconst(0) will be ignored when wrapping C/C++ enums with proper C# enums.
This is because C# enum items must be initialised from a compile time constant.
If an enum item has an initialiser and the initialiser doesn't compile as C# code,
then the %csconstvalue directive must be used as %csconst(0) will have no effect.
If it was used, it would generate an illegal runtime initialisation via a PINVOKE call.
</li>

<li>
C# doesn't support the notion of throws clauses.
Therefore there is no 'throws' typemap attribute support for adding exception classes to a throws clause.
Likewise there is no need for an equivalent to <tt>%javaexception</tt>.
</li>

<li>
Typemap equivalent names:

<blockquote><pre>
jni                         -> ctype
jtype                       -> imtype
jstype                      -> cstype
javain                      -> csin
javaout                     -> csout
javainterfaces              -> csinterfaces and csinterfaces_derived
javabase                    -> csbase
javaclassmodifiers          -> csclassmodifiers
javacode                    -> cscode
javaimports                 -> csimports
javabody                    -> csbody
javafinalize                -> csfinalize
javadestruct                -> csdestruct
javadestruct_derived        -> csdestruct_derived
</pre></blockquote>

</li>

<li>
Additional typemaps:

<blockquote><pre>
csvarin                     C# code property set typemap
csvarout                    C# code property get typemap
</pre></blockquote>

</li>

<li>
Feature equivalent names:
<blockquote><pre>
%javaconst                  -> %csconst
%javaconstvalue             -> %csconstvalue
%javamethodmodifiers        -> %csmethodmodifiers
</pre></blockquote>
</li>

<li>
Pragma equivalent names:
<blockquote><pre>
%pragma(java)               -> %pragma(csharp)
jniclassbase                -> imclassbase
jniclassclassmodifiers      -> imclassclassmodifiers
jniclasscode                -> imclasscode
jniclassimports             -> imclassimports
jniclassinterfaces          -> imclassinterfaces
</pre></blockquote>
</li>

<li>
Special variable equivalent names:
<blockquote><pre>
$javaclassname              -> $csclassname
$javainput                  -> $csinput
$jnicall                    -> $imcall
</pre></blockquote>
</li>

</ul>
The intermediary classname has <tt>PINVOKE</tt> appended after the module name instead of <tt>JNI</tt>, for example <tt>modulenamePINVOKE</tt>.

<p>
The directory <tt>Examples/csharp</tt> has a number of simple examples. 
Visual Studio .NET 2003 solution and project files are available for compiling with the Microsoft .NET C# compiler on Windows.
If your SWIG installation went well on a Unix environment and your C# compiler was detected, you should be able to type <tt>make</tt> in each example directory, 
then <tt>ilrun runme</tt> (Portable.NET C# compiler) or <tt>mono runme</tt> (Mono C# compiler) to run the examples.
Windows users can also get the examples working using a
<a href="http://www.cygwin.com">Cygwin</a> or <a href=http://www.mingw.org>MinGW</a> environment for automatic configuration of the example makefiles.
Any one of the three C# compilers (Portable.NET, Mono or Microsoft) can be detected from within a Cygwin or Mingw environment if installed in your path. 

</body>
</html>