C# 传递std::字符串&;和const std::string&;作为C中的参考#

C# 传递std::字符串&;和const std::string&;作为C中的参考#,c#,c++,string,reference,swig,C#,C++,String,Reference,Swig,我有以下问题。我有一个导演班,我有以下几位 方法: 虚拟布尔方法(常量字符串和参数1、int a、int b、字符串和参数2) 我想将std::string&和const std::string&作为c#中的ref传递。我 我们找到了以下解决方案 ,但这对我不起作用 在我的.i文件中,我调用: %include "std_string.i" %include "std_string_ref.i" %feature("director") DirectorClass; %include "

我有以下问题。我有一个导演班,我有以下几位 方法:

虚拟布尔方法(常量字符串和参数1、int a、int b、字符串和参数2)

我想将std::string&和const std::string&作为c#中的ref传递。我 我们找到了以下解决方案 ,但这对我不起作用

在我的.i文件中,我调用:

%include "std_string.i" 
%include "std_string_ref.i" 

%feature("director") DirectorClass; 
%include "DirectorClass.h"
下面是之前提供的链接示例(我改为ref):

/*
-----------------------------------------------------------------------------
*标准字符串参考i
*
*std::string和const std::string的类型映射&
*它们被映射到一个C#字符串,并通过引用传递
*
*
-----------------------------------------------------------------------------
*/
%{
#包括
%}
名称空间标准{
%自然弦;
类字符串;
//串&
%类型映射(ctype)标准::字符串和“字符**”
%typemap(imtype)std::string&“/*imtype*/out string”
%typemap(cstype)std::string&“/*cstype*/out string”
//C++
%typemap(in,canthrow=1)std::string&
%{//typemap in
std::字符串温度;
$1=&temp;
%}
//C++
%类型映射(argout)标准::字符串&
%{
/C++中的类型映射。
//这将将C++字符串转换为C字符串
*$input=SWIG_csharp_string_回调($1->c_str());
%}
%类型映射(argout)常量std::string&
%{
//常量std::string的argout类型映射&
%}
%类型映射(csin)标准::字符串和“out$csinput”
%typemap(抛出,canthrow=1)字符串&
%{SWIG_CSharpSetPendingException(SWIG_csharpsApplicationException,
$1.c_str());
返回$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;
    }
bool SwigDirector\u somedirector类::somemethod(std::string const
¶m1,int a,int b,std::string和param2){
bool c_result=SwigValueInit();
无符号整数jresult=0;
char*jparam1=0;
int ja;
int-jb;
字符**jparam2=0;
if(!swig_callbacksomemethod){
扔
Swig::DirectorPureVirtualException(“SomeDirectorClass::somemethod”);
}否则{
jparam1=SWIG_csharp_string_回调((¶m1)->c_str());
ja=设备ID;
jb=键;

JPARAM2=(STD:String *)和PARAM2;*C和C++?你尝试调用一个代码>虚拟< /Cord> C++方法(非常复杂的问题1),通过一个<代码> STD::String < /C>(非常复杂的问题2)。一般来说,这两个问题几乎都是无法解决的。C + C++不绑定。好的。正如另一个用户经常说的,使用C++/CLI作为粘合剂,或者像我经常说的那样,创建一些
extern“C”{}
方法作为粘合剂。我使用的是处理字符串和虚拟方法的工具Swig。但是我在按引用传递方面遇到了问题。我提供的示例可以工作,但不能与控制器(用于回调的Swig功能)一起工作。一些程序员认为SWIG可以解决他们的互操作问题,现在他们有两个问题。请编写一个C++/CLI包装程序。@HansPassant,那么您仍然会有两个问题,但至少有一个问题是Microsoft的问题:-)
  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;
    }