我試圖使用ctypes的從蟒蛇一個dll互動,閱讀文檔的C++方法簽名下面的ctypes的從Python中調用C++方法:使用不工作
my_c_pp_function(user *param1[],const int_8 param2,const int_8 values3[],const int_8 length_param1)
本質上的C++函數需要用戶列表,整數,值列表和用戶數量,也是一個整數
注意:用戶是包含名稱,年齡和id的結構 。喜歡的東西:
typedef struct
{
char name[255];
int_16 age;
int_32 uid;
}user;
當我嘗試使用ctypes的Python調用代碼這個功能我做的:
def call_my_c_pp_function(list_of_users,int_param2,list_of_values,int_lenght_of_list):
myparam1=(ctypes.c_char_p * len(list_of_users))(*list_of_users)
myparam2=ctypes.c_int8(int_param2)
myparam3=(ctypes.c_int8 * len(list_of_values))(*list_of_values)
myparam4=ctypes.c_int8(int_lenght_of_list)
self.dll_object.my_c_pp_function.argtypes(ctypes.POINTER(ctypes.c_char_p),ctypes.c_int8,ctypes.POINTER(ctypes.c_int8),ctypes.c_int8)
ret_value=self.dll_object.my_c_pp_function(myparam1,myparam2,myparam3,myparam4)
現在每次我叫Python的功能我得到一個錯誤,基本上,如果函數成功返回值應該是0,任何非零數字表示某種問題。
我不斷收到一個大的非零數字作爲返回值。 我可能會做錯什麼?是我創建用戶數組的方式,而數組的值錯誤?
我不知道如何填充/利用C用戶結構++在我的Python代碼,使我的名單不只是一個字符串列表,但我使用Python2.7用戶
列表在Windows上
Regards
如果第一個參數是真的* *一個'STD :: STRI因爲'char *'是**而不是'std :: string' – Petesh
你不能在函數的公共接口中使用'std :: string'因爲它是一個C++類型)。 'ctypes'只提供對C兼容數據類型的支持。 – foo
要調用它,你必須知道如何構建或以其他方式獲得'users',但是你沒有向我們展示任何有關這種類型的東西。 – Flexo