1、ctypes 变量类型
from ctypes import *
import ctypes
i = c_int(45)
print i.value
2、定义一个可变字符串变量,长度为 10
from ctypes import *
p = create_string_buffer(10)
p.value = 'student'
print p.raw
# 只有前三个字符被修改
p.value = '123'
print p.raw
3、C 语言指针
from ctypes import *
c = c_int(234)
# 定义指针指向c
p = pointer(c)
print p.contents.value
c.value = 45
print p.contents.value
p.contents.value = 100
print c.value
4、使用 C 语言数组和结构体
from ctypes import *
# 定义一个结构,内含两个成员变量 x,y,均为 int 型
class POINT(Structure):
_fields_ = [("x", c_int),
("y", c_int)]
point = POINT(2,5)
print point.x, point.y
# 重新定义一个POINT,x取默认值
point = POINT(y=5)
print point.x, point.y
# 定义 POINT_ARRAY 为 POINT 的数组类型
POINT_ARRAY = POINT * 3
pa = POINT_ARRAY(POINT(7, 7), POINT(8, 8), POINT(9, 9))
for p in pa:
print p.x, p.y
5、mac平台掉用c库函数
1)编写C代码
#include <stdio.h>
int add(int x, int y)
{
return (x+y);
}
char *reverse(char *s) {
register char t;
char *p = s;
char *q = (s + (strlen(s) - 1));
while (p < q) {
t = *p;
*p++ = *q;
*q-- = t;
}
return s;
}
typedef struct{
int a;
int b;
}mystruct;
mystruct * create(){
mystruct * s = (mystruct *)calloc(1, sizeof(mystruct));
s->a = 100;
s->b = 200;
return s;
}
void destroy(mystruct * s){
free(s);
}
2) 编译动态链接库
gcc add.c -fPIC -shared -o libAdd.so
or
gcc -fPIC -shared -o libAdd.so add.c
3)phthon导入动态链接库,调用C函数,涉及到参数类型以及函数返回类型
from ctypes import cdll
def callc():
# load the some.dll
so = cdll.LoadLibrary('/Users/xxxx/Desktop/python/libAdd.so')
# add函数很简单
print so. add(2,3)
if __name__== '__main__':
callc()
对于参数返回类型,返回地址不一致,从c代码应该是一样的
from ctypes import *
so = cdll.LoadLibrary('E:/nuli/scikit-learn/Scikit-learn/tqlq/dlltest.dll')
p = create_string_buffer(8)
p.value = 'student'
so.reverse.restype = c_char_p
lr = so.reverse(p)
print '%x'%id(lr), '%x'%id(p.value),p,lr,p.value
2606378 3c9daa8 <ctypes.c_char_Array_8 object at 0x0000000003D10248> tneduts tneduts
调整参数类型,返回类型设为类指针,得到一致性
from ctypes import *
so = cdll.LoadLibrary('E:/nuli/scikit-learn/Scikit-learn/tqlq/dlltest.dll')
p = create_string_buffer(8)
p.value = 'student'
so.reverse.restype = POINTER(type(p))
lr = so.reverse(p)
print '%x'%id(lr.contents.value), '%x'%id(p.value),lr.contents.value,p.value
3ac2080 3ac2080 tneduts tneduts
对于结构体类型处理
from ctypes import *
so = cdll.LoadLibrary('E:/nuli/scikit-learn/Scikit-learn/tqlq/dlltest.dll')
class mystruct(Structure):
_fields_ = [('a', c_int),
('b', c_int)]
so.create.restype = POINTER(mystruct)
p = so.create()
print p.contents.a, p.contents.b
so.destroy(p)
print p.contents.a, p.contents.b
100 200
释放内存后
64078736 0
6 PyObject,vs编译为xxx.dll,将xxx.dll改为xxx.pyd,然后import xxx即可
int add(int a,int b)
{
return a+b;
}
int sub(int a,int b)
{
return a -b;
}
int mul(int a,int b)
{
return a*b;
}
int div1(int a,int b)
{
if(0 == b)
{
return b;
}
return a/b;
}
PyObject* wrap_add(PyObject* self, PyObject* args)
{
int n1,n2, result;
if (! PyArg_ParseTuple(args, "i|i", &n1,&n2))
return NULL;
result = add(n1,n2);
return Py_BuildValue("i", result);
}
PyObject* wrap_sub(PyObject* self, PyObject* args)
{
int n1,n2, result;
if (! PyArg_ParseTuple(args, "i|i", &n1,&n2))
return NULL;
result = sub(n1,n2);
return Py_BuildValue("i", result);
}
PyObject* wrap_mul(PyObject* self, PyObject* args)
{
int n1,n2, result;
if (! PyArg_ParseTuple(args, "i|i", &n1,&n2))
return NULL;
result = mul(n1,n2);
return Py_BuildValue("i", result);
}
PyObject* wrap_div1(PyObject* self, PyObject* args)
{
int n1,n2, result;
if (! PyArg_ParseTuple(args, "i|i", &n1,&n2))
return NULL;
result = div1(n1,n2);
return Py_BuildValue("i", result);
}
static PyMethodDef exampleMethods[] =
{
{"add", wrap_add, METH_VARARGS, "Caculate 1!"},
{"sub", wrap_sub, METH_VARARGS, "Caculate 2!"},
{"mul", wrap_mul, METH_VARARGS, "Caculate 3!"},
{"div1", wrap_div1, METH_VARARGS, "Caculate 4!"},
{NULL, NULL,0,NULL}
};
void initexample()
{
PyObject* m;
m = Py_InitModule("example", exampleMethods);
}
网友评论