Calling C++ Method from Python using ctypes not working

user595985

I am trying to interact with a dll from python using ctypes, reading the documentation the C++ method signature is of the following:

my_c_pp_function(user *param1[],const int_8 param2,const int_8 values3[],const int_8 length_param1)

Essentially the c++ function requires a list of users, an integer,a list of values and the number of users, also an integer

Note: the users is a structure containing name,age, and id. Something like:

typedef struct
{
char name[255];
int_16 age;
int_32 uid;
}user;

When I try calling this function from python code using ctypes I do:

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)

Now every time I call the python function I get an error basically if the function succeeds the return value should be 0, any non zero number indicates some kind of problem.

I keep getting a large non-zero number as the return value. What could I possibly be doing wrong? is the way I'm creating the array of users, and the array of values wrong?

I am not sure how to populate/make use of the c++ user structure in my python code so that my list is not just a list of strings but a list of users

I'm using Python2.7 on Windows

Regards

foo

Assuming that you cannot change the signature of your library function.

I would recommend you to create a simple adapter for your c++ function, which just acts as an interface to your actual function. This function would just take all the values you need and then convert them to your needed types.

Here is a working example (tested with python27). Note that I added an additional parameter for the number of elements in the int array.

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()

export.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);

export.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;
} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Returning array when calling Fortran from Python using Ctypes

From Dev

Calling darknet API from python using ctypes (image as argument)

From Dev

unexpected integer returned from a C function by calling it using ctypes

From Dev

Calling call by reference c program in Python program using Ctypes

From Dev

How can I get the same object back (pass by reference) when calling C++ code from Python (using ctypes)?

From Dev

Calling Functions from within class using CTypes

From Dev

Calling CPP function from python ctypes

From Dev

Python ctypes: calling a function with custom types in c

From Dev

Returning a C array from a C function to Python using ctypes

From Dev

JNI: Calling a java method from C periodically is not working

From Dev

How to create a C struct from Python using ctypes?

From Dev

calling an object method from another method not working

From Dev

C Method returning string to Python via ctypes

From Dev

Calling DLLs using python's ctypes in OS X

From Dev

Calling a C library method from swift using pointers

From Dev

Calling scp from Python's subprocess not working with filelist \{a,b,c\}

From Dev

Calling a Go string function from Python ctypes results in segfault

From Dev

Calling a method from within itself is not working

From Dev

Calling a method from superclass not working Java

From Dev

Calling a method from ABL code not working

From Dev

Get image from a fingerprint using Python and Ctypes

From Dev

Debug C-library from Python (ctypes)

From Dev

Passing struct from C to Python with ctypes

From Dev

Passing audio data from Python to C with ctypes

From Dev

Calling one method from another method in Python

From Dev

POST method not working in .NET Core MVC while using Jquery , AJAX for calling api from View(html page)?

From Dev

imported some function from c++ dll to python using ctypes, but some functions doesn't work as expected

From Dev

How to get a string from C++ to python when using ctypes and wchar_t?

From Dev

How to update the value of a C output parameter from python using a ctypes callback?

Related Related

  1. 1

    Returning array when calling Fortran from Python using Ctypes

  2. 2

    Calling darknet API from python using ctypes (image as argument)

  3. 3

    unexpected integer returned from a C function by calling it using ctypes

  4. 4

    Calling call by reference c program in Python program using Ctypes

  5. 5

    How can I get the same object back (pass by reference) when calling C++ code from Python (using ctypes)?

  6. 6

    Calling Functions from within class using CTypes

  7. 7

    Calling CPP function from python ctypes

  8. 8

    Python ctypes: calling a function with custom types in c

  9. 9

    Returning a C array from a C function to Python using ctypes

  10. 10

    JNI: Calling a java method from C periodically is not working

  11. 11

    How to create a C struct from Python using ctypes?

  12. 12

    calling an object method from another method not working

  13. 13

    C Method returning string to Python via ctypes

  14. 14

    Calling DLLs using python's ctypes in OS X

  15. 15

    Calling a C library method from swift using pointers

  16. 16

    Calling scp from Python's subprocess not working with filelist \{a,b,c\}

  17. 17

    Calling a Go string function from Python ctypes results in segfault

  18. 18

    Calling a method from within itself is not working

  19. 19

    Calling a method from superclass not working Java

  20. 20

    Calling a method from ABL code not working

  21. 21

    Get image from a fingerprint using Python and Ctypes

  22. 22

    Debug C-library from Python (ctypes)

  23. 23

    Passing struct from C to Python with ctypes

  24. 24

    Passing audio data from Python to C with ctypes

  25. 25

    Calling one method from another method in Python

  26. 26

    POST method not working in .NET Core MVC while using Jquery , AJAX for calling api from View(html page)?

  27. 27

    imported some function from c++ dll to python using ctypes, but some functions doesn't work as expected

  28. 28

    How to get a string from C++ to python when using ctypes and wchar_t?

  29. 29

    How to update the value of a C output parameter from python using a ctypes callback?

HotTag

Archive