忧郁者 发表于 2017-5-4 08:41:24

python 调用C程序的结构体和函数

  C代码如下:
  #include <stdio.h>  
  typedef struct TestDLL_  
  {  
  int a;  
  char *b;  
  } testdll;  
  testdll test(testdll t)  
  {  
  t.a=t.a+t.a;  
  printf("%d\n%s\n",t.a,t.b);  
  return t;  
  }
  python代码如下:
  from ctypes import *  
  #绝对路径 
  dllpath='test.dll'  
  dll=CDLL(dllpath)  
  #python内部参数赋值
  a=c_int(125)  
  b=c_char_p('Hello world,Hello Chengdu')  
  #定义结构体
  class testdll(Structure):  
  _fields_=[('a',c_int),  
  ('b',c_char_p)]  
  #实例化并赋值
  t=testdll()  
  t.a=a  
  t.b=b  
  #设置返回值类型
  dll.test.restype=testdll  
  #测试
  t=dll.test(t)  
  print t.a  
  print t.b  
  x=raw_input('any key to continue')
页: [1]
查看完整版本: python 调用C程序的结构体和函数