Python calling C DLL function output issues and restype
<p>I was having this issue with my C++ DLL function.  My C++ DLL function description goes like this: </p>

<pre><code>// 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);
</code></pre>

<p>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: </p>

<pre><code>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)
</code></pre>

<p>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?</p>

<p>You help is greatly appreciated.  Thank you!  </p>

<p>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.</p>

<p>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.</p>

python c++ c dll