Bo Peng | 30 Aug 12:00 2014
Picon

Symbol collision between two swig wrapped python modules?

Dear SWIG users,

I have two Python modules wrapped from the same C++ code base with differences in compile time macro definitions. The modules, which consist of

_module_A.so
module_A.py
_module_B.so
module_B.py

have identical Python level names pointing to slightly different C/C++ objects. These modules work fine if only one of them is used. However, if I do

import module_A as mA
obj = mA.someObj()
# del obj would work ok here
  
import module_B as mB
del obj  # crash!

python would crash, pointing to some memory access problem of std::deque used in someObj. It appears to me that the type objects in module_A are somehow replaced by  the type objects defined in module_B so python has a wrong understanding of the type of obj. I am quite confused by this because I thought that these symbols are defined in different modules and should not collide, which is at least the case with pure-python modules. Is there anything with how Python interacts with C/C++ module or how swig wraps the C/C++ code that could cause this problem? Is there a way to get around of this?

Many thanks in advance,
Bo
 
------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Olivier Voyer | 27 Aug 20:07 2014
Picon

Initialize a sub-module within a package with SWIG and Python 3

Hi all,

I'm trying to port my SWIG/Python code from Python 2.7 to Python 3.4 (to support UNICODE), but I can't find a way to import my sub-modules in embedded mode, i.e "import my_package.my_module" won't work when called from C++.

I've created a stackoverflow post, I was wondering if somebody would like to help me with this? We are on a tight schedule and I'm really stuck here...


Thanks,

--
Olivier
------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
hadass yaari | 25 Aug 20:49 2014
Picon

swig and callbacks

Hello, this is my first question in this mailing list.
I have a problem:
I want to call Java callback from my cpp application.
I created callback in cpp and using director and swig the relevant class was created in Java automatically.
This callback is called twice: 
1. When the context is java thread context - this works great.
2. When the context is the cpp context.- the application collapses with segmentation fault.

I debugged this and found out that in the wrapper_cxx file, in the relevant function, it collapses when swig_get_self(jenv) is executed.
After reading a lot on the internet, I understand that the problem is that the right java thread is not attached. I also understand what I have to do manually.

I would like to know what solution does the swig give me automatically.
I would be greatful for your responses
Hadas
------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
marcus h | 25 Aug 10:21 2014
Picon

Bug in R generated code with pointers

When generating R wrapper code from Swig, it doesn't handle pointers to pointers correct (or if I don't use Swig/R correct). It passes a pointer to a pointer even if it shouldn't (in the second function call, fnRDllTestCopy)

This must be a bug when generating R code (or I use it incorrectly).

(Full project to reproduce the problem https://umetrics.sharefile.com/d/s4aa392c862a49adb)

This how it's created and the result

Swig command to create the wrapper:
swig.exe -r -c++ -o rfile.cpp RDllTest.i

The exported code looks like
typedef struct tagTestHandle
{
void* pReserved;
} TestHandle;
RDLLTEST_API int RCDECL fnRDllTestPointer(TestHandle pHandle);
RDLLTEST_API int RCDECL fnRDllTestCopy(TestHandle pHandle);

and from R I call

dyn.load ("C:\Projects\RDllTest\Debug\RTest.dll")
source("C:\Projects\RDllTest\RTest.R")
handle <- tagTestHandle()
fnRDllTestPointer(handle)
fnRDllTestCopy(handle) // Returns -1 which indicates an error

The C code looks like

class TestClass
{
public:
TestClass() : miTest(1234) {}
int miTest;
};

// This is an example of an exported function.
RDLLTEST_API int fnRDllTestPointer(TestHandle* pHandle)
{
*pHandle = reinterpret_cast(new TestClass());
return 42;
}

RDLLTEST_API int fnRDllTestCopy(TestHandle pHandle)
{
TestClass* pClass = reinterpret_cast(pHandle);
if (pClass->miTest != 1234) // Should be 1234 or there is an error
return -1;
return 42;
}

Generated R code

fnRDllTestPointer = function(pHandle, .copy = FALSE)
{
if (inherits(pHandle, "ExternalReference")) pHandle = slot(pHandle,"ref") 
;.Call('R_swig_fnRDllTestPointer', pHandle, as.logical(.copy), PACKAGE='RTest');
}

attr(fnRDllTestPointer, 'returnType') = 'integer'
attr(fnRDllTestPointer, "inputTypes") = c('_p_p_tagTestHandle')
class(fnRDllTestPointer) = c("SWIGFunction", class('fnRDllTestPointer'))

Start of fnRDllTestCopy

fnRDllTestCopy = function(pHandle, .copy = FALSE)
{
if (inherits(pHandle, "ExternalReference")) pHandle = slot(pHandle,"ref") 
;.Call('R_swig_fnRDllTestCopy', pHandle, as.logical(.copy), PACKAGE='RTest'); 
}
attr(fnRDllTestCopy, 'returnType') = 'integer'
attr(fnRDllTestCopy, "inputTypes") = c('_p_tagTestHandle')
class(fnRDllTestCopy) = c("SWIGFunction", class('fnRDllTestCopy'))

Generated C++ code

SWIGEXPORT SEXP
R_swig_fnRDllTestPointer ( SEXP pHandle, SEXP s_swig_copy)
{
int result;
TestHandle *arg1 = (TestHandle *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
unsigned int r_nprotect = 0;
SEXP r_ans = R_NilValue ;
VMAXTYPE r_vmax = vmaxget() ;

res1 = SWIG_R_ConvertPtr(pHandle, &argp1, SWIGTYPE_p_p_tagTestHandle, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "fnRDllTestPointer" "', argument " "1"" of type '" "TestHandle ""'"); 
}
arg1 = reinterpret_cast< TestHandle * >(argp1);
{
try {
result = (int)fnRDllTestPointer(arg1);

} catch(std::string& stringReason) {
const char sData = (char*)stringReason.c_str();
SWIG_exception(SWIG_RuntimeError, sData);
} catch(...) {
SWIG_exception(SWIG_RuntimeError, "Unknown exception");
}
}
r_ans = Rf_ScalarInteger(result);

vmaxset(r_vmax);
if(r_nprotect) Rf_unprotect(r_nprotect);

return r_ans;
}

SWIGEXPORT SEXP
R_swig_fnRDllTestCopy ( SEXP pHandle, SEXP s_swig_copy)
{
int result;
TestHandle arg1 = (TestHandle) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
unsigned int r_nprotect = 0;
SEXP r_ans = R_NilValue ;
VMAXTYPE r_vmax = vmaxget() ;

res1 = SWIG_R_ConvertPtr(pHandle, &argp1, SWIGTYPE_p_tagTestHandle, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "fnRDllTestCopy" "', argument " "1"" of type '" "TestHandle""'"); 
}
arg1 = reinterpret_cast< TestHandle >(argp1);
{
try {
result = (int)fnRDllTestCopy(arg1);

} catch(std::string& stringReason) {
const char* sData = (char*)stringReason.c_str();
SWIG_exception(SWIG_RuntimeError, sData);
} catch(...) {
SWIG_exception(SWIG_RuntimeError, "Unknown exception");
}
}
r_ans = Rf_ScalarInteger(result);

vmaxset(r_vmax);
if(r_nprotect) Rf_unprotect(r_nprotect);

return r_ans;
}

------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Yuval Kashtan | 25 Aug 10:01 2014
Picon

Re: Can anyone tell me how to do this simple thing in Swig ?

Hi Mike,
I was working on your problem a bit yesterday (finally got some time)

first of all you cant call the module and the struct by the same name.
second - the %apply is using an exact string match, so if you use char * in the c definition., you must use it in the apply statement as well
and last, your apply and %pointer collide.

I also had to do some minor fixes to the code (can send you it later on today, when I'm back near the home computer)
after fixing all that - I got a compilable code.


but more importantly - on top of all of that, you cannot use SWIG (or java and c) this way. the code crash
take a look at the generated c code (that's how the struct set the char *):
SWIGEXPORT void JNICALL Java_ExampleJNI_MY_1STRUCT_1szBuffer_1set(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jbyteArray jarg2) {
  MY_STRUCT *arg1 = (MY_STRUCT *) 0 ;
  char *arg2 = (char *) 0 ;

  (void)jenv;
  (void)jcls;
  (void)jarg1_;
  arg1 = *(MY_STRUCT **)&jarg1;
  {
    if (!jarg2) {
      SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array null");
      return ;
    }
    if ((*jenv)->GetArrayLength(jenv, jarg2) == 0) {
      SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element");
      return ;
    }
    arg2 = (char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0);
  }
  {
    free(arg1->szBuffer);
    if (arg2) {
      arg1->szBuffer = (char *) malloc(strlen((const char *)arg2)+1);
      strcpy((char *)arg1->szBuffer, (const char *)arg2);
    } else {
      arg1->szBuffer = 0;
    }
  }
  {
    (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *)arg2, 0);
  }

}

you can see it is copying the CONTENT of the java byte array
it will let you then manipulate it
and when you call the get function - copy it back to a java string.

this is due to the way java handle memory management - you can read more about it here:
http://kashyoo.blogspot.co.il/2014/07/swig-callbacks-and-java-memory.html


If that's suite your needs - great, but please write your code accordingly.


Sincerely,
Yuval Kashtan


On Mon, Aug 18, 2014 at 1:18 PM, Mike Davies <miked <at> mobbu.com> wrote:
Hi,

I made a mistake in my .i file the line

%apply byte * INOUT {char*};

gives an error from Swig "Message.i:22: Warning 453: Can't apply (byte *INOUT). No typemaps are defined."

the line should read

 %apply signed char * INOUT {char*};

Mike


On 18 August 2014 09:47, Mike Davies <miked <at> mobbu.com> wrote:
Hi Yuval,

My Java code is as follows, it's from an Android application so please ignore the first and last 2 lines of the function which are specific to Android, also any small cut-and-paste errors that may appear :

import java.util.Arrays;
import com.example.MY_STRUCT.*;

    <at> Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
    /*  typedef struct
        {
                int* i1;

                char* c1;
                int len;
        }MY_STRUCT;
    */
        MY_STRUCT p_my_struct = new MY_STRUCT();
      
        int i1 = 1;
        SWIGTYPE_p_int i1p = MY_STRUCT.new_intp();
        MY_STRUCT.intp_assign(i1p, i1);

        int len = 128;
        byte[] c1 = new byte[len];
       
        p_my_struct.setI1(i1p);
        p_my_struct.setC1(c1);
        p_my_struct.setLen(len);
       
        MY_STRUCT.myfunc(p_my_struct);
        Integer i1_ret = MY_STRUCT.intp_value(i1p);
       
        byte[] ret_c1 = p_my_struct.getC1();
       
        String display = new String();
    // !!        display = Arrays.toString(ret_c1); // <<<< Gives Exception !!
        display += i1_ret;

        final EditText eText = (EditText) findViewById(R.id.editText1);
        eText.setText(display);
    }

Thanks,

Mike


On 18 August 2014 09:34, Yuval Kashtan <yuvalkashtan <at> gmail.com> wrote:
as long as you are doing so WITHIN the function boundaries (vs utilizing a callback or so), this should be pretty easy.
can you send your java code piece as well?

Sincerely,
Yuval Kashtan


On Mon, Aug 18, 2014 at 11:31 AM, Mike Davies <miked <at> mobbu.com> wrote:
Hi Yuval,

Thanks very much for your reply.  If you are able to  show me a working .i file and corresponding java code that can set and use the MY_STRUCT pointer members I will be eternally grateful :-).  I have been trying to get this to work for more than a week now…

Thanks again,

Mike



On 18 August 2014 09:27, Yuval Kashtan <yuvalkashtan <at> gmail.com> wrote:
I can probably help you. will try this out later, hopefully today.
:)

Sincerely,
Yuval Kashtan


On Mon, Aug 18, 2014 at 11:22 AM, Mike Davies <miked <at> mobbu.com> wrote:
Hi,

I posted what was going wrong for me with Swig but got no reply.  My post was rather long and what I really want to know is given the following code in C :

     typdef struct
    {
        int * i1;
        char* c1;
        int len;
    } MY_STRUCT;
   
    bool myfunc (MY_STRUCT* pms)
    {
        strncpy (pms->c1, "Hello from the world of Swig", pms->len);
        *pms->i1 = strlen(pms->c1);
    }

What do I need to put in a .i file and what java code in a .java file in order to be able to have read and write access to java data in my C code ? (I.e. read and write through pointers ?).

My .i file is effectively the same as what follows below and if anyone can tell me why I get flakey problems when I write to the i1 and c1 elements of MY_STRUCT thenm I would be grateful.  I am using Swig Version 3.0.2 on Mac OS X Mavericks.

Many thanks,

Mike


/* File : MY_STRUCT.i */
%module MY_STRUCT

// Enable the JNI class to load the required native library.
%pragma(java) jniclasscode=%{
 static {
 try {
 java.lang.System.loadLibrary("MY_STRUCT");
 } catch (UnsatisfiedLinkError e) {
 java.lang.System.err.println("native code library failed to load.\n" + e);
 java.lang.System.exit(1);
 }
 }
%}

%include <typemaps.i>
%apply byte * INOUT {char*};
%apply int * INOUT {int*};

typdef struct
{
    int * i1;
    char* c1;
    int len;
} MY_STRUCT;

%include cpointer.i

bool myfunc (MY_STRUCT* pms)
{
    strncpy (pms->c1, "Hello from the world of Swig", pms->len);
    *pms->i1 = strlen(pms->c1);
}

%pointer_functions(int, intp);


------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user







------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
brainstorm | 18 Aug 18:49 2014
Picon

SWIG python segfault on OSX, fine on Linux

Sorry for the cross-posting, but I wonder if any of you have a good solution for this problem I'm experiencing with SWIG and Python on OSX:

http://stackoverflow.com/questions/25349828/barebones-swig-python-c-interface-segfaulting-on-osxclang-not-in-linuxgcc

Thanks in advance!
Roman
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Mike Davies | 18 Aug 10:22 2014

Can anyone tell me how to do this simple thing in Swig ?

Hi,

I posted what was going wrong for me with Swig but got no reply.  My post was rather long and what I really want to know is given the following code in C :

     typdef struct
    {
        int * i1;
        char* c1;
        int len;
    } MY_STRUCT;
   
    bool myfunc (MY_STRUCT* pms)
    {
        strncpy (pms->c1, "Hello from the world of Swig", pms->len);
        *pms->i1 = strlen(pms->c1);
    }

What do I need to put in a .i file and what java code in a .java file in order to be able to have read and write access to java data in my C code ? (I.e. read and write through pointers ?).

My .i file is effectively the same as what follows below and if anyone can tell me why I get flakey problems when I write to the i1 and c1 elements of MY_STRUCT thenm I would be grateful.  I am using Swig Version 3.0.2 on Mac OS X Mavericks.

Many thanks,

Mike


/* File : MY_STRUCT.i */
%module MY_STRUCT

// Enable the JNI class to load the required native library.
%pragma(java) jniclasscode=%{
 static {
 try {
 java.lang.System.loadLibrary("MY_STRUCT");
 } catch (UnsatisfiedLinkError e) {
 java.lang.System.err.println("native code library failed to load.\n" + e);
 java.lang.System.exit(1);
 }
 }
%}

%include <typemaps.i>
%apply byte * INOUT {char*};
%apply int * INOUT {int*};

typdef struct
{
    int * i1;
    char* c1;
    int len;
} MY_STRUCT;

%include cpointer.i

bool myfunc (MY_STRUCT* pms)
{
    strncpy (pms->c1, "Hello from the world of Swig", pms->len);
    *pms->i1 = strlen(pms->c1);
}

%pointer_functions(int, intp);

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Mike Davies | 14 Aug 12:53 2014

Java, Android, Swig structure with pointers problem.

Hi,

I made a development but I don't really know what it means.  If I change the line

    %apply signed char * INOUT {char*};
    
to

    %apply byte * INOUT {char*};

my Swig invocation gives the following error :
    
    Warning 453: Can't apply (byte *INOUT). No typemaps are defined.

However the generated code compiles and runs OK with the proviso that I need to change my java application code to use a String instead of a byte[] for its version of c1.

My new java code is :

    <at> Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    /*  typedef struct
        {
                int* i1;
                char* c1;
                int len;
        }*P_MYSTRUCT, MYSTRUCT;*/
        S_MESSAGE_STRUCT p_my_struct = new S_MESSAGE_STRUCT();
       
        int i1 = 1;
        SWIGTYPE_p_int i1p = Message.new_intp();
        Message.intp_assign(i1p, i1);

        int len = 128;
        byte[] c1 = new byte[len];
        
        String s1 = new String(c1);
        p_my_struct.setI1(i1p);
        p_my_struct.setC1(s1);
        p_my_struct.setLen(len);
        
        Message.t_func(p_my_struct);
        
        SWIGTYPE_p_int pi1_ret = p_my_struct.getI1();
        Integer i1_ret = Message.intp_value(pi1_ret);
        String display = p_my_struct.getC1();

        display += i1_ret;

        final EditText eText = (EditText) findViewById(R.id.editText1);

        eText.setText(display);
    }

Does this shed any light on what I may be doing wrong ?

Any ideas gratefully received,

Mike
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Mike Davies | 13 Aug 15:43 2014

Java, Android, Swig structure with pointers problem.

Hi,

Apologies in advance for such a long first missive but I am trying to use Swig to generate wrappers for some in-house C-code so we can reuse it for a new Android java project and am having problems.  I am new to both Java and Swig so please be gentle with me in terms of the technical content of any replies.

I am trying to wrap a C-structure called S_MESSAGE_STRUCT which contains pointers in such a way that its elements can be accessed (set up, written, read etc) from java and also by legacy C code.  My C header file defining the struct is as follows, the code is exemplary and simplified from my real code but it displays the same problems :

    #ifndef MESSAGE_H_
    #define MESSAGE_H_
   
    #include <stdbool.h>

    typedef struct
    {
        int*    i1;
        char*   c1;
        int        len;
    } *P_S_MESSAGE_STRUCT, S_MESSAGE_STRUCT;
   
    bool t_func(P_S_MESSAGE_STRUCT p_s_mystruct);
   
    #endif /* ndef MESSAGE_H_ */

My C file contains a single function which tests the int* i1 element of the S_MESSAGE_STRUCT and then writes to the c1 and i1 elements as follows :

    #include "message.h"
   
    bool t_func(P_S_MESSAGE_STRUCT p_s_mystruct)
    {
        if (*p_s_mystruct->i1 == 1)
        {
            strcpy(p_s_mystruct->c1, "Hello from swig");
            p_s_mystruct->c1[p_s_mystruct->len-1] = '\0';
            *p_s_mystruct->i1 = strlen(p_s_mystruct->c1);
        }
       
        return true;
    }

I am using a .i file as follows :

    /* File : Message.i */
    %module Message
    %{
    /* Includes the header in the wrapper code */
    #include "../../../common/message/message.h"
    %}
   
    // Enable the JNI class to load the required native library.
    %pragma(java) jniclasscode=%{
     static {
     try {
     java.lang.System.loadLibrary("Message");
     } catch (UnsatisfiedLinkError e) {
     java.lang.System.err.println("native code library failed to load.\n" + e);
     java.lang.System.exit(1);
     }
     }
    %}

    %include <typemaps.i>
    %apply signed char * INOUT {char*};
   
    typedef struct
    {
        int*      i1;
        char*   c1;
        int       len;
    } *P_S_MESSAGE_STRUCT, S_MESSAGE_STRUCT;
   
    %include cpointer.i
   
    bool t_func(P_S_MESSAGE_STRUCT p_s_mystruct);
   
    %pointer_functions(int, intp);

My java source function is as follows (edited from a default Android Activity) :

    <at> Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
    /*  typedef struct
        {
                int* i1;
                char* c1;
                int len;
        }*P_MYSTRUCT, MYSTRUCT;
    */
        S_MESSAGE_STRUCT p_my_struct = new S_MESSAGE_STRUCT();
      
        int i1 = 1;
        SWIGTYPE_p_int i1p = Message.new_intp();
        Message.intp_assign(i1p, i1);

        int len = 128;
        byte[] c1 = new byte[len];
       
        p_my_struct.setI1(i1p);
        p_my_struct.setC1(c1);
        p_my_struct.setLen(len);
       
        Message.t_func(p_my_struct);
        Integer i1_ret = Message.intp_value(i1p);
       
        byte[] ret_c1 = p_my_struct.getC1();
       
        String display = new String();
    // !!        display = Arrays.toString(ret_c1); // <<<< Gives Exception !!
        display += i1_ret;

        final EditText eText = (EditText) findViewById(R.id.editText1);

        eText.setText(display);
    }

Everything compiles and builds OK and there are no errors from the swig invocation of :
      
    swig -java -package com.mobbu.Message -outdir ../../src/com/mobbu/Message/ -verbose Message.i
   
but I know that I am doing something wrong because I keep getting flakey problems after I call the t_func() function from java.  The problems are various and include the program hanging after the call to t_func() completes but sometimes the program runs and displays an output OK.  When output is displayed the contribution due to c1 is null (it just gives output "15" with no signs of "Hello from swig".

I have determined that the program flakiness stems from the writes to the pointer elements i1 and c1 in the P_MYSTRUCT struct, because there are no problems either without those elements or without the writes (but leaving the c1,i1 elements in the struct).

I would be very grateful for help on how to achieve my goal of being able to use pointer elements of C structs and for any ideas of what I am doing wrong in my Message.i file ?  I am using SWIG Version 3.0.2.
Thanks,

Mike
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
Taylor Centers | 13 Aug 16:31 2014
Picon

Just starting SWIG question

I have a c++ static library file (.lib) that I would like to generate an interface for to c#, I've been able to get the imports and the function definitions out of it and have done my best at making the interface file for the library, and have run "swig -c++ -csharp interface.i" and it generates a lot of files for me, a lot of .cs and one .cxx.

I guess my questions are: have I done these first steps correctly? and, Where do I go from here?
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user
natarajan manikandan | 11 Aug 15:13 2014
Picon

Could not able to write the proper interface file for Csharp


Hi Swig users,
 
I could not able write the proper interface file when my object factory returns via boost:: shared_ptr as shown below
//MyPackFactory.h
namespace MyPack{
class  ATPACK_API MyPackServiceFactory
{
public:    boost::shared_ptr<IMyPackService> getPackService();
};
}
//MyPackServices.h
namespace MyPack{
//Represents information 
class MyPackService : public IMyPackService
{
public:
MyPackService(){}
    virtual std::vector<std::string> getPacksList();
};}
//MyPackInterfaceServices.h
namespace MyPack{
class IMyPackService 
{
public:
    virtual std::vector<std::string> getPacksList() = 0; };
}

My usage would be in cpp file is like this:
//Main.cpp
 MyPack::MyPackServiceFactory MyPackFactory;
    boost::shared_ptr<MyPackPack::IMyPackService> service = MyPackFactory.getPackService();
    std::vector<std::string> packlist = service->getPacksList();


to consume the same in C# project by creating wrapper using swig, what I should do?I have tried my attempt like this below:
\\mypack.i
%module MyPackWrap
%{
#include "MyPackServiceFactory.h"
#include "MyPackInterfaceService.h"
#include "boost\shared_ptr.hpp"
#include "MyPackService.h"
%}
// Some required SWIG headers
%include <windows.i>
%include <std_vector.i>
%include <std_string.i>
%include "MyPackInterfaceService.h"
%include "MyPackService.h"
%include "MyPackServiceFactory.h"
%include <boost_shared_ptr.i>
%template(vectorofstring) std::vector<std::string>;
%shared_ptr(atPackService)
%shared_ptr(IAtPackService)
%shared_ptr(AtPackServiceFactory)

Could anyone help me?
Best Regards,
 
Manikandan N
9980007590
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/swig-user

Gmane