传递std :: string&和const std :: string&作为C#中的ref

时间:2017-04-12 12:26:57

标签: c# c++ string reference swig

我有以下问题。我有一个导演课,我跟随 方法:

virtual bool somemethod(const string& param1,int a,int b,string& param2);

我想传递std :: string&和const std :: string&作为参考c#。一世 找到了以下解决方案 agg ,但它对我不起作用。

在我的.i文件中,我打电话:

    /*
-----------------------------------------------------------------------------
 * std_string_ref.i
 *
 * Typemaps for std::string& and const std::string&
 * These are mapped to a C# String and are passed around by reference
 *
 *
-----------------------------------------------------------------------------
*/

%{
#include <string>
%}

namespace std {

%naturalvar string;

class string;

// string &

%typemap(ctype) std::string & "char**"
%typemap(imtype) std::string & "/*imtype*/ out string"
%typemap(cstype) std::string & "/*cstype*/ out string"

//C++
%typemap(in, canthrow=1) std::string &
%{  //typemap in
    std::string temp;
    $1 = &temp;
 %}

//C++
%typemap(argout) std::string &
%{
    //Typemap argout in c++ file.
    //This will convert c++ string to c# string
    *$input = SWIG_csharp_string_callback($1->c_str());
%}

%typemap(argout) const std::string &
%{
    //argout typemap for const std::string&
%}

%typemap(csin) std::string & "out $csinput"

%typemap(throws, canthrow=1) string &
%{ SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException,
$1.c_str());
   return $null; %}

}

以下是之前提供的链接示例(我已更改为参考):

  bool SwigDirector_SomeDirectorClass::somemethod(std::string const 
  &param1,int a, int b, std::string &param2) {
      bool c_result = SwigValueInit< bool >() ;
      unsigned int jresult = 0 ;
      char * jparam1 = 0 ;
      int ja  ;
      int jb  ;
      char** jparam2 = 0 ;

      if (!swig_callbacksomemethod) {
        throw
    Swig::DirectorPureVirtualException("SomeDirectorClass::somemethod");
      } else {
        jparam1 = SWIG_csharp_string_callback((&param1)->c_str());
        ja = deviceId;
        jb = key;
        jparam2= (std::string *) &param2; *<--- here is problem cannot convert
    from string * to char ***
        jresult = (unsigned int) swig_callbackGetConfigItem(jparam, ja, jb,
    jparam2);
        c_result = jresult ? true : false;
      }
      return c_result;
    }

生成的代码,我有问题:

{{1}}

那么如何实现在导演方法中通过引用传递字符串?

最诚挚的问候, NIK

0 个答案:

没有答案