0

所以我试图在Linux(MonoDevelop)上使用带有SWIG的C#中的C库。一切编译和运行都很好,但是当我调用 MsgBus_UseString 时,控制台输出显示乱码而不是“HappyHappyJoyJoy”。

这是我在 C# 中的主要内容:

  public static void Main (string[] args)
        {               
            SWIGTYPE_p_msgbus msgBusObj = msgbus_client.MsgBus_Create();

            ret = msgbus_client.MsgBus_setString(msgBusObj, "HappyHappyJoyJoy");
            Console.WriteLine ("Set setString ret = "+ret);

            ret = msgbus_client.MsgBus_UseString(msgBusObj);
            Console.WriteLine ("UseString ret = "+ret);
        }

下面是函数 MsgBus_UseString 在 C 库中的样子:

static int MsgBus_UseString( msgbus_t * pObj )
{
    if ( pObj ){
        printf("The String = %s", pObj->TheString);
        return Connect(pObj->TheString);
    }
}

C 库中的 MsgBus_setString 函数:

int MsgBus_setString( msgbus_t * pObj, const char * theStringIn )
{
  int status = 2;  // invalid msgbus_t pointer

  if( pObj ) {
    pObj->TheString = (char*)theStringIn;
    status = 0;
  }

  printf(fp, "pObj->TheString = %s \n", pObj->TheString);

  return status;
}

这是我的 SWIG 界面的样子:

/* Creates wrappers so that msgbus_client can be used in C# */
%module msgbus_client
%{
#include "msgbus_client.h"
#include "msgbus_var.h"
%}

%inline %{
typedef signed char     int8_t;
typedef short int       int16_t;
typedef int         int32_t;
typedef long long int       int64_t;
typedef unsigned char       uint8_t;
typedef unsigned short int  uint16_t;
typedef unsigned int        uint32_t;
typedef unsigned long long int  uint64_t;
typedef long int _time_t;
%}

%include "msgbus_client.h"
%include "msgbus_var.h"

这是 msgbus_client.h 文件中的 typedef:

typedef struct msgbus msgbus_t;

我已经为此苦苦挣扎了一段时间。MsgBus_setString 函数中的 printf 返回“HappyHappyJoyJoy”,但由于某种原因,调用 MsgBus_UseString 函数时该数据丢失。正如你所看到的,我一个接一个地调用它们,“set”函数返回 0(无错误),而“use”函数返回 -1(错误)。这是有道理的,因为 printf 显示 TheString 是一堆随机/不可读的字符。msgbus 结构非常大,所以我想避免包装整个东西,但如果没有其他方法,我会这样做。

不确定是否需要,但以防万一,这里是 C lib 中的 create 函数:

msgbus_t * MsgBus_Create()
{
    struct msgbus * pObj = NULL;

    pObj = (struct msgbus*)calloc(1, sizeof(struct msgbus));
    if ( pObj ){

        pObj->TheString = NULL;

    }

    return pObj;
}

如果不是很明显我是 SWIG 新手,但根据文档,这应该可以工作。我显然错过了一些东西,但我似乎无法弄清楚它是什么。任何帮助是极大的赞赏。

编辑: 感谢您的快速回复。

下面是 msgbus 结构的定义:

struct msgbus{
    int             fd;             
    int             run;            
    pthread_t       thread;        
    //.           
    //.   More Stuff
    //.     
    tlv_buffer_t *  pSend;          
    tlv_buffer_t *  pResp;          

    char *          TheString;        /*!< The String */
};

msgbus 结构的定义在 msgbus_client.c 和 msgbus_client.h 中,typedef 如下:

typedef struct msgbus msgbus_t;

这是上面提到的。这是执行 SWIGing 的脚本:

    # Begin Swiggin! #
    echo "Starting to SWIG! ... " 
    swig -csharp -outdir msgbus_swig msgbus_client.i


    Switches="-c -I/home/includes "\
"-Wall -Wundef -Wstrict-prototypes -Wno-trigraphs  -g -Wall -D__linux__ "\


    # Build binaries #
    g++ $Switches -o "$msgbusBin/msgbus_client_wrap.o" "msgbus_client_wrap.c"

    # Generate Shared Lib #
    g++ -isystem /home/include \
-isystem /home/another/include  \
-D__packed__= -DIMAGE_APPS_PROC -DFEATURE_Q_SINGLE_LINK -DFEATURE_Q_NO_SELF_QPTR \
-DFEATURE_LINUX -DFEATURE_NATIVELINUX -DFEATURE_DSM_DUP_ITEMS -DTARGET_desktop \
-I/home/more/include -Wall -Wundef \
-Wstrict-prototypes -Wno-trigraphs  -g -Wall -D__linux__ -fpic -shared \
-L/home/lib \
-Wl,-soname,libmsgbus_client.so -o "libmsgbus_client.so" \
"$msgbusBin/msgbus_client.o" "$msgbusBin/msgbus_var.o" "$msgbusBin/msgbus_client_wrap.o" -lmificommon -lc -lrt -lpthread 

    echo "SWIG complete!"

所以我从 make 日志中复制了很多 g++ 调用,所以那里有一些我不完全理解的开关。如果我需要显示其他内容,请告诉我。再次感谢你的帮助。

4

1 回答 1

1

For your string issue the problem is that you try to marshal a C# string directly to a char *. But please note char * stores a pointer and not the string itself. What you need to do is to copy the string. For that you probably also need to allocate/reserve memory.

于 2012-12-04T02:25:13.230 回答