2

I am new in C# programming and trying to call wrap functions that is in C++.

In C++ I have a function of the following prototype

string* swap(string* ptr1, string*ptr2){
    //swap the array of string
    return ptr2;
}

How do I wrap this function into C# (ideally using SWIG, but not necessary)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
leon
  • 4,931
  • 7
  • 39
  • 37

1 Answers1

1

I had the same question for Java.

For Java, I did not find anything pre-packaged and trivial to use. I had to define my own typemaps.

In case you don't get any better answers for C#, you could start with the SWIG/C++ code described at SWIG: How to wrap std::string& (std::string passed by reference) .

You would need to replace references to Java types, JNI methods (those that include a reference to "jenv"), and "JavaThrowException."

These files from SWIG might also provide some relevant example code for C#:

  • swig/Lib/csharp/std_string.i
  • swig/Lib/csharp/typemaps.i
Community
  • 1
  • 1
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • String is relatively simple(?) however I do not know how the string[] in c# is structure in memory. They are just continuous buffer with \0 terminators? – leon Sep 30 '10 at 20:31
  • Since C# strings are reference types, I would guess that the array is an array of references to non-contiguous string objects. I don't know the easiest way to convert between C# and C++ strings in C++. – Andy Thomas Sep 30 '10 at 22:14