python 向C介面傳遞結構體陣列 結構體

2021-10-05 12:55:08 字數 3560 閱讀 3171

c原始碼 1.c

#include #include #include "1.h"

int add(int a, int b)

#if 0

int add_stus(student *data, int count)

return 1;

}int add_stu(student *data)

int add_data(student data)

#endif

int add_stus(student *data, int count)

return 1;

}int add_stu(student *data)

int add_data(student data)

#if 0

int main()

#endif

標頭檔案1.h

#ifndef _1_h_

#define _1_h_

#include #if 0

typedef struct _tag_ststudent;

#endif

typedef enumalarm_type_e;

typedef struct tag_threat_rawdatastudent;

int add(int a, int b);

int add_stu(student *data);

int add_stus(student *data, int count);

int add_data(student data);

#endif

生成動態庫

$ gcc 1.c -fpic -shared -o libmystu.so
注意:一定要用gcc編譯,python呼叫c/c++介面只支援gcc生成的動態庫

python 原始碼

#!/usr/bin/python3.4

# -*- coding:utf-8 -*-

from ctypes import *

import time

import sys

import os

dll = cdll.loadlibrary("./libmystu.so") #載入動態庫

lk_alarm_type_ip=1

lk_alarm_type_domain=2

lk_alarm_type_url=3

lk_alarm_type_fhash=4

#定義結構體

class threatclass(structure):

_fields_ = [

("rtype", c_int),

("alarmtime", c_long),

("msgdata", c_char * 128),

("srcip", c_char * 48),

("dstip", c_char * 48),

("trans_proto", c_char * 8),

("sport", c_short),

("dport", c_short),

("flag", c_int)

]def add_threat(array, count):

try:

if count <= 0:

return

entrylist =

i = 0

for data in array:

threat = threatclass()

threat.rtype = data["rtype"]

threat.alarmtime = data["alarmtime"]

threat.msgdata = bytes(data["msgdata"].encode())

threat.srcip = bytes(data["srcip"].encode())

threat.dstip = bytes(data["dstip"].encode())

threat.trans_proto = bytes(data["trans_proto"].encode())

threat.sport = data["sport"]

threat.dport = data["dport"]

threat.flag = 0

#dll.add_stu.argtype = pointer(threatclass)

#dll.add_stu.restype = c_int

#ret = dll.add_stu(byref(threat))

i = i+1

#定義結構體陣列,批量插入

threatarray = (threatclass * i)(*entrylist)

dll.add_stus.argtypes = (pointer(threatclass), c_int) #指定c介面的引數型別,list格式

dll.add_stus.restype = c_int #指定c介面的返回值型別

ret = dll.add_stus(threatarray, i)

print(ret)

except exception as err:

print(err)

if __name__ == '__main__':

arrary =

data = {}

data["alarmtime"] = int(time.time())

data["msgdata"] = "1.1.1.56"

data["srcip"] = "172.16.110.10"

data["dstip"] = "172.16.110.11"

data["trans_proto"] = "tcp"

data["sport"] = 1204

data["dport"] = 1205

data["rtype"] = lk_alarm_type_ip

da = {}

da["alarmtime"] = int(time.time())

da["msgdata"] = "9.9.9.9"

da["srcip"] = "172.16.110.10"

da["dstip"] = "172.16.110.11"

da["trans_proto"] = "tcp"

da["sport"] = 1204

da["dport"] = 1205

da["rtype"] = lk_alarm_type_ip

add_threat(arrary, 2)

注意:1. python 定義的結構體順序一定要與c定義的結構體順序一致

2. python 定義的結構體中的陣列元素的長度要與c結構體中的陣列長度一致,保持對齊

3. 字元陣列要如此處理,bytes(data["srcip"].encode())再傳遞

4. 結構體陣列的定義要 threatarray = (threatclass * i)(*entrylist), entrylist是列表,儲存物件

WPF向系統傳送訊息 並傳遞結構體

場景 需要開發乙個通訊元件 流程為 介面 開啟接收服務 通過傳送元件傳送資訊到 其他客戶端和服務端 接受服務接收其他客戶端傳送的訊息 需要傳遞給對應元件或者介面 因此會出現類庫重複引用問題。因為採用訊息佇列,和資料庫中轉來傳遞訊息需要每個元件知道太多其他元件的業務,並且耗損效能和時間更多一下因此都被...

C 關於結構體變數和結構體指標變數函式傳遞值得問題

2014年5月29日10 13 35 關於結構體變數和結構體指標變數函式的值傳遞 include includestruct stduent void inputstduent struct stduent pst 輸入函式 void onputstduent struct stduent stu ...

Python 向函式傳遞列表

在實際使用中你會發現,向函式傳遞列表是比較實用的,這種列表可能包含名字 數字 可能更複雜的物件 字典 假設向乙個函式傳遞一堆水果,我們說出我們喜歡所有的水果 def obj fruits for fruit in fruits msg i like fruit print msg l orange ...