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!
restypefor all functions that do not return a Cint. See the Ctypes documentation. Save yourself some hassle and always specify all and correctargtypesandrestype.