0

I was having this issue with my C++ DLL function. My C++ DLL function description goes like this:

// Combine the lower 32 bits and upper 8 bits of a 40 bit
// integer into a 64 bit double precision floating point number 
//
// Inputs:
// - Lower: lower 32 bits of the 40 bit timestamp
// - Upper: upper 8 bits of the 40 bit timestamp
//
// Outputs:
// - function return value is the 64 bit double precision floating point number
//
extern "C" double WINAPI Make64BitDoubleIntegerFromUpperLower(uint32_t Lower, uint8_t Upper);

When I make a simple C++ client test code, the function works and I was getting the results I expect. I'm trying to make a python client that will use this function (and others). I've set up everything I need to run the function in python (libraries, import locations etc.), and I put in the correct parameters, however when I ran a test code script for my python client I always got a value of 0 after the function. I found that the python function was returning an Int and not a c_double. I looked online and I found there is a way to fix this using the 'restype' function. I used it like so:

def pc_make_64_bit_double_integer_from_upper_lower(self, Lower, Upper):
    self.opxclient_dll.OPX_Make64BitDoubleIntegerFromUpperLower.restype = c_double
    return self.opxclient_dll.OPX_Make64BitDoubleIntegerFromUpperLower(Lower, Upper)

Now everything works and I'm getting the right output. This is great but I'm also confused as to how this is working. My concern is I have many dll functions I want to implement in my python client. Most of them that just fill in arrays that I pass in by reference, with c_long and c_int values which I will convert to python types. These functions then return a 0 (got data) or -1 (no data) which python easily can read as an int. When should I use the restype function? Is it necessary for all dll functions in order to get the right output values?

You help is greatly appreciated. Thank you!

2
  • Use restype for all functions that do not return a C int. See the Ctypes documentation. Save yourself some hassle and always specify all and correct argtypes and restype. Commented Aug 26, 2016 at 19:45
  • Ah thanks for the info! Commented Aug 26, 2016 at 21:20

1 Answer 1

1

Just like any C function, Python needs to know what the return type is, in order to convert it to it's own type. By default, python infers that the return type is an int - something that isn't always the case.

In all cases, when the return type is not int, you need to explicitly state the return type for the function so python will translate and convert it correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep I saw the documentation that cohlke posted! This is interesting! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.