[ctypes] return type is different with what I setted up

Hello! while I use .dll file made from c++ for python, I got a problem.

I setted ctypes function restype as such as c_bool and c_char_p, but what I got was random integer such as 363303008 and -829978160.

Of course in c++, I used extern “C” and __declspec(dllexport) as well.
I checked return type of original function too.

What did I miss?

We can’t possibly diagnose problems in code that you describe to us - only in code that we actually see.

If there are sensitive business details etc., try to make a separate example first that demonstrates the problem. This is good practice anyway:

1 Like

I’ve been building dlls and calling them from Python recently too. When I saw values similar to those you did:

363303008 and -829978160.

it was because I’d messed up something to do with a pointer, e.g. forgot to dereference it.

All I can say based on the description of your problem is that if you’re getting an integer return value, such as -829978160, then the function pointer’s restype is not set to either c_char_p or c_bool. The getfunc of c_char_p converts a C char * pointer value into a Python bytes object, except that Python None is used for a C NULL pointer. The getfunc of c_bool casts a C _Bool to C long and converts that to a Python bool object, with value True or False. On the other hand, the default restype for a function pointer is c_int, which converts a signed C int value into a signed Python int object.

2 Likes

Thank you!
the problem was that I setted : restypes
after fixing it to this, it worked : restype

1 Like