python調用C程序的結構體和函數
來源:懂視網
責編:小采
時間:2020-11-27 14:27:41
python調用C程序的結構體和函數
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調用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

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')
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
python調用C程序的結構體和函數
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