Perhaps since your string class is custom, you will need to write a whole set of typemaps to support it. And you may need typemaps not just for MyString itself but also MyString&, const MyString&, MyString*, and const MyString* (of course, in some cases one typemap can cover several of these or all of these).


C-side typemaps: ctype, in, out, argout

C#-side typemaps: imtype, cstype, csin, csout, csvarin, csvarout


As a shortcut, you could simply make a copy of Lib\csharp\std_wstring.i and replace all instances of wstring with MyString (you can’t do this with std_string.i since some instances of “string” refer to System.String, not std::string), then tweak it until it works properly for you. Also change SWIG_csharp_wstring_callback to SWIG_csharp_string_callback since your strings are not wide.


From: Anders Backman []
Sent: Friday, March 30, 2012 1:53 AM
Subject: [Swig-user] Using System.String instead own string implementation


Hi all.

I have been struggling with a problem for a while now.


I have been looking through documentation, existing examples, surfing the web etc. but the answer escapes me on this one.


Now assume I have my own string class (its an example, I'm not avoiding std::string, its just that we have a special string class and this example illustrates what we need, but its not the actual implementation).

Ok assume I have MyString.h:



#include <string.h>

#include <iostream>


namespace N


  class MyString



    MyString() {}

    MyString(const char *str) { m_string = str; }

    MyString(const std::string& str) { m_string = str; }

    MyString(const MyString& other) { m_string = other.m_string; }


    ~MyString() {}

    const char *c_str() const { return m_string.c_str(); }

    char operator[] (size_t index) const { return m_string[index]; }


    double m_buf[128]; // just some extra data

    std::string m_string;

    double m_buf2[128];



  class Stuff {


    Stuff() { std::cerr << "Stuff::Stuff" << std::endl; }

    ~Stuff() { std::cerr << "Stuff::~Stuff" << std::endl; }

    void setName( const MyString& name) {m_name = name;  std::cerr << "setName: " << m_name.c_str() << std::endl; }

    const MyString& getName( ) const { return m_name; }


    MyString m_name;





It contains a class Mystring and Mystuff.

In c# I really want to avoid using MyString, I want to use System.String all over the place (to comply with C#).


      N.Stuff stuff = new agx.Stuff();


      System.String ss = stuff.getName();

      Console.WriteLine("Name: {0}", ss);


My swigfile:


%module N


%include "std_string.i"

%include "typemaps.i"



 #include "MyString.h"



%include "MyString.h"


Now this obviously is not enough, but I have spent considerable time on typemap's with csin, csout etc, looking at std_string.i etc, but with no luck.

I thought about trying to tell SWIG that MyString and std::string is the same thing, but as you can see I have added m_buf, m_buf2 to this, illustrating that returning by reference, static cast etc does (of course) not work.


So anyone that can give me a hint on this? 



Anders Backman, HPC2N
90187 Umeň University, Sweden
Cell: +46-70-392 64 67