Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:20:19+00:00 2026-06-15T10:20:19+00:00

So I am trying to use a C library in C# with SWIG on

  • 0

So I am trying to use a C library in C# with SWIG on Linux(MonoDevelop). Everything compiles and runs just fine, but when I call the MsgBus_UseString the console-out shows gibberish instead of “HappyHappyJoyJoy”.

Here’s my Main in 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);
        }

Here is what the function MsgBus_UseString looks like in the C lib:

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

The MsgBus_setString function in the C lib:

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;
}

This is what my SWIG interface looks like:

/* 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"

Here’s the typedef in the msgbus_client.h file:

typedef struct msgbus msgbus_t;

I’ve been struggling with this for quite some time. The printf in the MsgBus_setString function returns “HappyHappyJoyJoy” but for some reason this data is lost when calling the MsgBus_UseString function. As you can see I call them right after each other and the “set” function returns 0(no error) while the “use” function returns -1(error). This makes sense since the printf is showing that TheString is a bunch of random/unreadable characters. The msgbus struct is quite large so I’d like to avoid wrapping the entire thing, but I’ll do it if there is no other way.

Not sure if it’s needed but just in case, here is the create function in the C lib:

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

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

        pObj->TheString = NULL;

    }

    return pObj;
}

In case it wasn’t obvious I’m a SWIG newb, but based off the documentation this should work. I’m clearly missing something, but I can’t seem to figure out what it is. Any help is greatly appreciated.

Edit:
Thanks for the quick reply.

Here is the definition of msgbus struct:

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

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

The definition of the msgbus struct is in msgbus_client.c and is in the msgbus_client.h with the following typedef:

typedef struct msgbus msgbus_t;

Which was mentioned above. Here is the script that does the 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!"

So I copied a lot of the g++ calls from a make log so there are switches in there that I don’t fully understand. Please let me know if I need to display anything else. Thanks again for all your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:20:20+00:00Added an answer on June 15, 2026 at 10:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to use SWIG to wrap around a simple library that uses
I'm trying to use this library (which looks very nice) but I'm having difficulty
I have a problem. I'm trying to use urllib library in python. but, I
I'm trying to use curses.h library for the first time, but no matter which
I'm trying to use the library jmathai/twitter-async to send tweets, but I get an
I'm trying to use the boost library in my code but get the following
So I'm trying to use the TagLib library with Databinding but I'm having trouble
I'm trying to use the ndimage library from scipy, but its apparently missing. I
I'm trying to use Xapian library in my Qt-project. I've just added header: #include
I am trying to use Dynamic Linq library in my code, but it gives

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.