使用ctypes从Python调用C++方法不起作用

Calling C++ Method from Python using ctypes not working

本文关键字:C++ 方法 不起作用 调用 Python ctypes 使用      更新时间:2023-10-16

我正在尝试使用ctypes与python中的dll交互,阅读文档,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,任何非零的数字都表明存在某种问题。

我一直得到一个大的非零数字作为返回值。我可能做错了什么?我创建用户数组和值数组的方式是错误的吗?

我不知道如何在我的python代码中填充/使用c++用户结构,这样我的列表就不仅仅是字符串列表,而是用户列表

我在Windows 上使用Python2.7

问候

假设您不能更改库函数的签名。

我建议您为c++函数创建一个简单的适配器,它只是实际函数的接口。这个函数只需要获取您需要的所有值,然后将它们转换为您需要的类型。

下面是一个工作示例(用python27测试)。请注意,我为int数组中的元素数量添加了一个附加参数。

py_caller.py

import ctypes
import os

class user(ctypes.Structure):
    _fields_ = [("name", ctypes.c_char_p),
                ("age", ctypes.c_int),
                ("uid", ctypes.c_int)]

def create_users(count):
    users = (user * count)()
    for i in range(count):
        users[i].name = ctypes.c_char_p("user" + str(i))
        users[i].age = i
        users[i].uid = i * i
    return users

def create_values(count):
    values = (ctypes.c_int * count)()
    for i in range(count):
        values[i] = i ** 2
    return values

def main():
    os.environ['PATH'] += ";" + os.path.dirname(os.path.abspath(__file__))
    dll = ctypes.cdll.LoadLibrary('cppdll')
    count = 4
    users = create_users(count)
    n_users = ctypes.c_int(count)
    values = create_values(count)
    n_values = ctypes.c_int(count)
    some_int = ctypes.c_int(42)
    res = dll.func(users, n_users, some_int, values, n_values)
    print (res)

if __name__ == "__main__":
    main()

导出.h

#pragma once
typedef struct t_user
{
  char *name;
  int age;
  int uid;
};
extern "C" __declspec(dllexport) int func(t_user *users, int n_users, int val, int *values, int n_values);

导出.cpp

#include "export.h"
#include <iostream>
int func(t_user *users, int n_users, int val, int *values, int n_values)
{
  std::cout << "users:n";
  for (auto i = 0; i < n_users; ++i)
  {
    std::cout << users[i].name
      << ": " << users[i].age 
      << " - " << users[i].uid << "n";
  } 
  std::cout << "values:n";
  for (auto i = 0; i < n_values; ++i)
    std::cout << values[i] << "n";
  std::cout << "some int: " << val << "n";
  // do something else with the values
  // such as calling your actual library function
  return 1;
}