Patrick.Fromberg.extern@... wrote:
> I just read the SWIG-Dokumentation and one of the nice example typemaps
> is following multi-arg one:
>
> %typemap(in) (int argc, char * arg[]) {
> . . .
>
> It suggests to mee, I could map
>
> int foo(double * p, int len) //c-code
> to
> int foo(double[]) //c# code
>
> As the c# double[] type also stores the length.
>
> I see examples about marshalling and wrapping through out the whole help
> text (good enough for Python) but nothing about mapping c# types to c
> types.
>
> I can find no way to specify, which type in the generated c#-code shall
> correspond to which type in the c-code.
>
> I have a C-interface with lots of struct FP{int rows, int cols, double
> [] array} pointer arguments (Excels matrix type) which I would like to
> treat like native double[,] matrices in the C# code.
>
> Can you give me a hint, in which chapters I missed something very
> essential?
> P.
>
There isn't any specific documentation for this in C#. Implementing this
in C# is a bit tricky as obtaining information about managed objects
from the unmanaged side is not so easy. I got a little trick though to
pass the required information into the unmanaged side by adding in an
additional parameter. Normally with multi-argument typemaps, one of the
arguments is removed. I've added the missing argument back in in the
intermediary layer, but kept it removed from the proxy layer. Example...
/* File : example.i */
%module example
%typemap(ctype) (char *bytes, int len) "int length, char *"
%typemap(imtype) (char *bytes, int len) "int length, string"
%typemap(cstype) (char *bytes, int len) "string"
%typemap(csin) (char *bytes, int len) "$csinput.Length, $csinput"
%typemap(in) (char *bytes, int len) {
$1 = (char *)$input;
$2 = length;
}
%inline %{
int count(char *bytes, int len, char c) {
int i;
int count = 0;
for (i = 0; i < len; i++) {
if (bytes[i] == c) count++;
}
return count;
}
%}
>From C# you can then use:
string myString = "oh my goodness!";
int count = example.count(myString, 'o');
Console.WriteLine("count: {0}", count);
and you get
count: 3
William
|