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

c代码如下:

 

#include  

  

typedef struct TestDLL_  

立即学习“Python免费学习笔记(深入)”;

{  

    int a;  

    char *b;  

} testdll;  

  

testdll test(testdll t)  

{  

    t.a=t.a+t.a;  

    printf(“%d%s”,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’)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2281915.html

(0)
上一篇 2025年2月27日 19:02:47
下一篇 2025年2月27日 17:05:48

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论