Reputation: 53
I am setting up a Python-C SWIG interface right now. I've already gotten it to produce the shared object file through the following commands:
swig -python myFile.i
gcc -c myFile.c myFile2.c myFile3.c myFile_wrap.c -IC:\myincludedirectory\include
gcc -shared myFile.o myFile2.o myFile3.o myFile_wrap.o -Ic:\myincludedirectory\libs -lpython27 -o _myfile3.o
In myFile.i, I have:
/* myFile3 */
%module myFile3
%{
#include "myFile.h"
#include "defs.h"
#include "myFile2.h"
#include "myFile3.h"
%}
%include "defs.h"
%include "myFile.h"
%include "myFile2.h"
%include "myFile3.h"
defs.h contains the following lines (among others):
typedef unsigned int BOOL;
typedef unsigned short WORD; //Word is 2 bytes but int* is 4 bytes ??
typedef unsigned int DWORD;
typedef unsigned long long QWORD;
typedef unsigned char BYTE;
When I attempt to call my program, I do the following in command prompt:
python
import myFile3
myFile3.myFuncInit()
myFile3.myFuncWrite ("ABCD", "ABCDEFGHI", 8); // <= I get an error when I enter this line.
where myFuncWrite's parameters are (char *param1, const BYTE *param2, WORD param3)
I believe the issue lies in the fact that it's a const BYTE pointer. I am not sure if I need to do some simple helper function or a typemap to get it to pass properly. As it is, I keep getting stuck here and I've no idea what I should do to get swig to understand I'm simply passing a pointer of BYTE's typedef.
Upvotes: 2
Views: 1323
Reputation: 1297
After the %include "defs.h line, try adding this line:
%apply char* {BYTE*};
This will apply all of the char* typemaps (including type-checking) to BYTE*. For more on this, read the following section of the SWIG manual:
http://www.swig.org/Doc2.0/Typemaps.html#Typemaps_nn6
Upvotes: 3