R
Rolf
ctypes works as I would expect with python2.7.3.
However, when I upgrade to python3.2.3 things don't seem to work right. Look below for details.
I am not sure where I am going wrong.
Shared Library
==============
#include <stdint.h>
#include <string.h>
extern "C"
{
int main();
uint32_t myfunction (char **);
}
uint32_t myfunction (char ** _mydata)
{
char mydata[16];
strcpy(mydata, "Hello Dude!");
*_mydata = mydata;
return 0;
}
int main()
{
return 0;
}
Python 2.7.3 which works as I would expect
==========================================
from __future__ import print_function
from __future__ import unicode_literals
from ctypes import *
lib = CDLL('libtest.so')
o_result = c_char_p()
lib.myfunction(pointer(o_result))
print(repr(o_result.value))
Python 3.2.3 return string gets mangled
=======================================
from ctypes import *
lib = CDLL('libtest.so')
o_result = c_char_p()
lib.myfunction(pointer(o_result))
print(repr(o_result.value))
Every time I run it, I get a different set of values.
However, when I upgrade to python3.2.3 things don't seem to work right. Look below for details.
I am not sure where I am going wrong.
Shared Library
==============
#include <stdint.h>
#include <string.h>
extern "C"
{
int main();
uint32_t myfunction (char **);
}
uint32_t myfunction (char ** _mydata)
{
char mydata[16];
strcpy(mydata, "Hello Dude!");
*_mydata = mydata;
return 0;
}
int main()
{
return 0;
}
Python 2.7.3 which works as I would expect
==========================================
Python 2.7.3python2.7 -V
#!/usr/bin/env python2.7cat py27.py
from __future__ import print_function
from __future__ import unicode_literals
from ctypes import *
lib = CDLL('libtest.so')
o_result = c_char_p()
lib.myfunction(pointer(o_result))
print(repr(o_result.value))
'Hello Dude!'./py27.py
Python 3.2.3 return string gets mangled
=======================================
Python 3.2.3python3 -V
#!/usr/bin/env python3cat py3.py
from ctypes import *
lib = CDLL('libtest.so')
o_result = c_char_p()
lib.myfunction(pointer(o_result))
print(repr(o_result.value))
b'\xd8\xb0y\to Dude!'./py3.py
Every time I run it, I get a different set of values.