Python ctypes parameter with DLL - pointer to array of doubles


lightning

I'm a novice programmer using ctypes with Python and trying to use functions from a DLL written in C. I've found a lot of similar questions on SO to solve, but none of the answers to this kind of conundrum.

I have the DLL loaded just fine, but one of the functions I need to call requires a pointer to an array of 6 doubles to dump the values ​​into, but I can't figure out how to provide what that function needs via Python . (My failed attempt to do this in C is another story.)

I've tried various permutations of ctypes.POINTER(c_double), using byref(), POINTER(c_double*6), etc, and for all of them I get either a type error, or preferably an access violation.

From the DLL documentation:

int swe_calc_ut (double tjd_ut, int ipl, int iflag, double* xx, char* serr)

The function calculates using the time in Julian day to return the planetary body's longitude, latitude, etc. as double.

The closest I've come to passing the data type the DLL will accept is with this code, just trying to get any of the 6 doubles from swe_calc_ut:

dll = windll.LoadLibrary(# file path)

# retype the args for swe_calc_ut
py_swe_calc_ut = dll.swe_calc_ut
py_swe_calc_ut.argtypes = [c_double, c_int, c_int, c_double, c_char_p]
py_swe_calc_ut.restype = None

tjd = c_double(# some value from elsewhere)

returnarray = c_double()
errorstring = create_string_buffer(126)

py_swe_calc_ut(tjd, c_int(0), c_int(64*1024), returnarray, errorstring)

When I try to run as is, I get the error:

OSError: exception: access violation writing 0x0000000000000000

Using byref() gives me a type error, etc.

I'd be forever grateful if someone could point me in the right direction to get the doubles I need from the original DLL function; I'm so frustrated I can't figure this out.

Mark Tolonen

This (untested) should work. The fourth parameter is type POINTER(c_double). double returnarray[6]The type equivalent to C is c_double * 6, and an instance of that type is returnarray= (c_double * 6)(). Also, if you declare the parameter type, you don't need to wrap the input parameter, e.g. int(0);. It 0is possible to pass:

dll = windll.LoadLibrary(# file path)

# retype the args for swe_calc_ut
py_swe_calc_ut = dll.swe_calc_ut
py_swe_calc_ut.argtypes = [c_double, c_int, c_int, POINTER(c_double), c_char_p]
py_swe_calc_ut.restype = None

tdj = 1.5 # some value
returnarray = (c_double * 6)()
errorstring = create_string_buffer(126)

py_swe_calc_ut(tjd, 0, 64*1024, returnarray, errorstring)

Related


Python ctypes parameter with DLL - pointer to array of doubles

lightning I'm a novice programmer using ctypes with Python and trying to use functions from a DLL written in C. I've found a lot of similar questions on SO to solve, but none of the answers to this kind of conundrum. I have the DLL loaded just fine, but one of

Python ctypes parameter with DLL - pointer to array of doubles

lightning I'm a novice programmer using ctypes with Python and trying to use functions from a DLL written in C. I've found a lot of similar questions on SO to solve, but none of the answers to this kind of conundrum. I have the DLL loaded just fine, but one of

Python Numpy array Ctypes pointer

Ventura Franklin I have a GetImageData function in a DLL: int GetImageData ( const char * name , int ID , const char * strLibPath , int Lvl , int rbeg , int rend , int cbeg , int cend , int ZLayer , unsigned char * ImgBuffer ); In Python, I import this DLL a

How to pass char pointer as parameter in ctypes python

Naveen kumar Katta rathanaiah Please help me to convert the following line of C++ code to ctypes python: Ret = openFcn(&Handle, "C:\\Config.xml"); Here are the declarations for each: typedef uint16_t (* OpenDLLFcnP)(void **, const char *); OpenDLLFcnP openFcn

access pointer to array of doubles

Toon I have these structures: typedef struct { char *start; char *loops; char *tolerance; double *numbers; } configuration; typedef struct { bool help; bool debug; const char *configFile; const char *inputFile; const char *

access pointer to array of doubles

Toon I have these structures: typedef struct { char *start; char *loops; char *tolerance; double *numbers; } configuration; typedef struct { bool help; bool debug; const char *configFile; const char *inputFile; const char *

access pointer to array of doubles

Toon I have these structures: typedef struct { char *start; char *loops; char *tolerance; double *numbers; } configuration; typedef struct { bool help; bool debug; const char *configFile; const char *inputFile; const char *

access pointer to array of doubles

Toon I have these structures: typedef struct { char *start; char *loops; char *tolerance; double *numbers; } configuration; typedef struct { bool help; bool debug; const char *configFile; const char *inputFile; const char *

access pointer to array of doubles

Toon I have these structures: typedef struct { char *start; char *loops; char *tolerance; double *numbers; } configuration; typedef struct { bool help; bool debug; const char *configFile; const char *inputFile; const char *

Pointer to array of doubles or array of pointers?

Mershad I'm maintaining part of the code my friend wrote, this is the definition of a variable called d: double (*d)[3]; I tried to initialize variables using the code below, but there are errors in every part (runtime or compile). I am confused whether the v

Pointer to array of doubles or array of pointers?

Mershad I'm maintaining part of the code my friend wrote, this is the definition of a variable called d: double (*d)[3]; I tried to initialize variables using the code below, but there are errors in every part (runtime or compile). I am confused whether the v

Pointer to array of doubles or array of pointers?

Mershad I'm maintaining part of the code my friend wrote, this is the definition of a variable called d: double (*d)[3]; I tried to initialize variables using the code below, but there are errors in every part (runtime or compile). I am confused whether the v

Load dll with Python Ctypes

Kronost I have looked at the ctypes example given here - Beginner and followed the same steps with different C code. I have built a .dll and .lib using the C code provided here : http://wolfprojects.altervista.org/articles/dll-in-c-for-python/ //test.c __dec

Python and Ctypes - Amplify DLL

heal you My goal is to get the magnification.dll from Windows running in Python 3.6 with Ctypes. I can zoom the screen, but I can't do the transformation of the input. I hope someone knows how to fix this and can explain to me what I'm doing wrong. thanks. ( Z

Load dll with Python Ctypes

Kronost I have looked at the ctypes example given here - Beginner and followed the same steps with different C code. I have built a .dll and .lib using the C code provided here : http://wolfprojects.altervista.org/articles/dll-in-c-for-python/ //test.c __dec

Load dll with Python Ctypes

Kronost I have looked at the ctypes example given here - Beginner and followed the same steps with different C code. I have built a .dll and .lib using the C code provided here : http://wolfprojects.altervista.org/articles/dll-in-c-for-python/ //test.c __dec

Load dll with Python Ctypes

Kronost I have looked at the ctypes example given here - Beginner and followed the same steps with different C code. I have built a .dll and .lib using the C code provided here : http://wolfprojects.altervista.org/articles/dll-in-c-for-python/ //test.c __dec

Python and Ctypes - Amplify DLL

heal you My goal is to get the magnification.dll from Windows running in Python 3.6 with Ctypes. I can zoom the screen, but I can't do the transformation of the input. I hope someone knows how to fix this and can explain to me what I'm doing wrong. thanks. ( Z

Load dll with Python Ctypes

Kronost I have looked at the ctypes example given here - Beginner and followed the same steps with different C code. I have built a .dll and .lib using the C code provided here : http://wolfprojects.altervista.org/articles/dll-in-c-for-python/ //test.c __dec