FAQ Database Discussion Community


Define array type in Python to be filled by C function

python,c,arrays,ctypes
In the C header "param1" is declared as an OUT parameter: long A_API test( ___OUT_ char param1 ) In the Python code I am trying to use array so that test function has space to fill this parameter.I have imported ctpyes and: test_array = bytearray(b'') importArray = (ctypes.c_char*40)(*test_array) def callTest():...

How can I setup a Structure in ctypes with pointer to itself?

python,ctypes
I have the following C declaration of a struct: struct vnode { char firstchar; uint8_t wordlength; bool is_red; struct vnode *left; struct vnode *right; struct textelem *texts; }; So pretty typical tree with some payload. I tried to rewrite this to the following ctypes declaration: class VNODE(Structure): _fields_ = [("firstchar",...

using c++ code from python in a shared-lib to control an LCD

python,c++,shared-libraries,ctypes,raspberry-pi2
My project is to control an LCD with ST7565 controler on my Raspberry B2 I'm trying to use this c++ code, that controls the lcd: https://github.com/stephanel/ST7565-RPi Unfortunately I'm not familiar with programming in C, because of this I want to call the c++ functions from python with ctypes. I wrote...

How to define C-Enumeration types in python

python,ctypes,memmove
I have an enumeration data type in C. How should I declare that in python-ctypes? I want this enum variable to be part of a structure and the assignment of the values to this structure would be done through memmove. After assigning, I want to display the values of each...

Passing a path to Labview DLL in Python

python,dll,ctypes,labview
I am starting here with my question, but realize it might need to be answered on the Labview forums. So, I have a DLL that was created in Labview, and I am accessing it via Python 3.3.3. I know that I am accessing it correctly, as I am able to...

Using Rust returned array in Python using ctypes

python,rust,ctypes
I have a Rust function that returns an array and I want to use this array in Python, it could be a list or numpy.array it does not really matter. My Rust function looks like this: #[no_mangle] pub extern fn make_array() -> [i32; 4] { let my_array: [i32; 4] =...

Get address from PyCObject?

python,ctypes,python-2.6
I find it really strange that I couldn't an answer to this question by searching, only ever the opposite question on how to create a PyCObject from a pointer (and in C, not Python). I might just have used the wrong search terms. I have a PyCObject in Python, and...

Interfacing cuSOLVER-sparse using PyCUDA

python,cuda,ctypes,pycuda,cusolver
I'm trying to interface the sparse cuSOLVER routine cusolverSpDcsrlsvqr() (>= CUDA 7.0) using PyCUDA and am facing some difficulties: I have tried wrapping the methods the same way the dense cuSolver routines are wrapped in scikits-cuda (https://github.com/lebedov/scikits.cuda/blob/master/scikits/cuda/cusolver.py). However, the code crashes with a segmentation fault when calling the cusolverSpDcsrlsvqr() function....

Portable ctypes.c_char_p for python 2.x and 3.x

python,ctypes
From the ctypes documentation of python 2.x, we have: >>> printf("String '%s', Int %d, Double %f\n", "Hi", 10, 2.2) And from the ctypes documentation of python 3.x, we have: >>> printf(b"String '%s', Int %d, Double %f\n", b"Hi", 10, 2.2) So in one case the argtypes for c_char_p requires a str...

Python ctypes Unable to pass memory array in a structure

python,windows,dll,ctypes
Python 2.7.8, Windows 7, Phyton USB programmer's DLL I am stuck with the Data element which should be a large memory array, I have tried using several different definitions, but I cannot understand what the errors are trying to tell me. Most of the errors I have encountered have been...

Python ctypes for HANDLE

python,dll,ctypes
I have a DLL function with prototype as follow: short Connect(HANDLE* pHandle, UINT* pTimerID, LPCTSTR strDestination, LPCTSTR strServiceName, int nProtocol, int nType) The function description as follow: pHandle: socket handle.[out] pTimerID: Timer ID using sending alive packet to a camera.[out] strDestination: camera network IP.[in] strServiceName: camera network port.[in] nProtocol: address...

More cttypes c_float() precision

python,floating-point,ctypes,floating-accuracy
I'm using Python's ctypes library to call my C code. My problem is that when I try to create a c_float, I seem to obtain a slightly different value to what I set. For example print(value) print(c_float(value)) 0.2 c_float(0.20000...298...) How can I avoid this?...

How do I cast to a constant in ctypes?

python,c++,ctypes
I have a C function that takes a const double pointer in the third input pos: PI_MOV(int ID, const char* szAxes, const double* pdValueArray) When I try to use a double pointer: pos = C.POINTER(C.c_double) pos.value = 2.0 GCS_api.PI_MOV(controller_number, szAxis1, pos) I get an argument type error for position 3:...

Memory Leak in Ctypes method

python,c,memory-leaks,ctypes
I have a project mostly written in Python. This project runs on my Raspberry Pi (Model B). With the use of the Pi Camera I record to a stream. Every second I pauze the recording to take the last frame from the stream and compare it with a older frame....

Converting .so file of linux to a .dll file of windows

c,dll,ctypes
I have some C code (having various header files and a make file) compiled as a .so file in Linux(Ubuntu) and a python program which calls the functions of this shared object using Ctypes. Now, I want to use this program on windows. In windows, Ctypes needs a .dll file...

ctypes error AttributeError symbol not found, OS X 10.7.5

python,c++,ctypes
I have a simple test function on C++: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <locale.h> #include <wchar.h> char fun() { printf( "%i", 12 ); return 'y'; } compiling: gcc -o test.so -shared -fPIC test.cpp and using it in python with ctypes: from ctypes import cdll from ctypes import c_char_p...

numpy ctypes “dynamic module does not define init function” error if not recompiled each time

python,c++,numpy,ctypes
sorry for yet an other question about dynamic module does not define init function. I did go through older questions but I didn't find one which adress my case specifically enought. I have a C++ library which should export several functions to python ( like ~5 functions defined in extern...

Pass Python list to embedded Rust function

python,rust,ctypes
I am learning how to embed Rust functions in Python, and everything works fine if my inputs are ints, but not list. If my lib.rs file is: #[no_mangle] pub extern fn my_func(x: i32, y: i32) -> i32 { return x + y; } I can use this as follows: In...

Using windll in ctypes on Windows 7 (64bit)

python,python-2.7,ctypes
I would like to ask if somebody has similar problem like me and if there is any solution for this. When I used Windows XP (32bit) before I have installed Windows 7 (64bit), I've used following program in Python 2.7.8 without any error: from ctypes import * kernel = windll.kernel32...

Define array length for c_char Python

python,c,compilation,header,ctypes
In C header file I have: long param_API test( ___OUT_ char Text[41] ) And after importing ctypes in Python code I am calling test: out_char = (ctypes.c_char)() def getRes(): result = lib.test(out_char) return result But I ma getting this error in the log files: output parameter is NULL I guess...

Get error message from ctypes windll

python,windows,python-2.7,ctypes
I'm trying to use a Python script to change the wallpaper on a windows 7 computer. If it matters, I'm invoking the script from a node-webkit application. The shortened script looks as follows: # ... result = ctypes.windll.user32.SystemParametersInfoA(20, 0, path, 0) Often, it works, but sometimes, seemingly randomly, it does...

does python ctypes supports size-0 array?

python-3.x,ctypes
i have a struct with array[0] inside it.i wonder how can i represent it with ctypes? or if ctypes does not supprt it, are there any other solutions? Any help will be appreciated.

ctypes call GetProcAddress() failed with error code 127,but win32api.GetProcAddress successed

python,ctypes
When I call GetProcAddress() by win32api, I get the handle successfully but ctypes didn't. The codes are: from ctypes import windll import win32api KERNEL32 = windll.kernel32 h_kernel32 = KERNEL32.GetModuleHandleW('kernel32.dll') print(hex(h_kernel32)) h_loadlib1=win32api.GetProcAddress(h_kernel32,'LoadLibraryW') print(hex(h_loadlib1)) if not h_loadlib1: print("NtCreateThreadEx Failed:",win32api.GetLastError()) h_loadlib2 = KERNEL32.GetProcAddress(h_kernel32,'LoadLibraryW') print(hex(h_loadlib2)) if not h_loadlib2: print("NtCreateThreadEx...

Unable to load C++ dll in python

python,dll,ctypes
I have a C++ dll which I'm trying to use it in Python, >>> from ctypes import * >>> mydll = cdll.LoadLibrary("C:\\TestDll.dll") until now there are no errors, system seem to be doing what I wanted, but when I try to access mydll, the Intellisence in the Python IDLE shows...

Saving XML as ctypes c_byte in variable gives TypeError: an integer is required

python,c,xml,ctypes
In C header file I have : long TEST_API test ( ___OUT_ char DisplayText[41], _IN____ const char XMLparams[2049] ); In python code I have imported ctypes and I am trying to call "Test". class A(Structure): _fields_ = [("DisplayText", c_byte*41), ("XMLparams",c_byte*2049) ] XMLparamsVal = (ctypes.c_byte*2049)(["<xml><MatchboxDataProviderValue>Openvez</MatchboxDataProviderValue><AlwaysPrintTwoTicketsFlag>FALSE</AlwaysPrintTwoTicketsFlag><DisplayWidthInCharacters>20</DisplayWidthInCharacters><!--...

Invalid input type for c_char ctypes in python

python,c,header,ctypes
In my C header file I have: long TEST_API test( _IN____ const char arg1, _INOUT_ char arg2[512] ); I have imported ctypes in my python code and I am passing "kcOpCode" and "szXMLAdditionalParameters" to eftUtility function as following: utilityXml_bytes = bytearray(b'<xml><test>20</test></xml>') utilityXMLparams = (ctypes.c_byte*512)(*utilityXml_bytes) def test(): eftUtilityRes = lib.test("s", utilityXMLparams)...

python-magic WindowsError: access violation writing 0x00000000

python-2.7,cygwin,ctypes,python-magic
I installed python-magic (0.4.6) on my Win 7 64bit using pip. I then installed cygwin 1.7.33-2 to provide the needed dlls and created a copy of cygmagic-1.dll named magic1.dll (see When I run the Python 2.7.6 32bit shell, the "import magic" works fine. However, a magic.from_file('c:\user\username\sample.txt') gives me a Traceback...

Setting _fields_ dynamically in ctypes.Structure

python,ctypes
Reading the documentation here, I see that you can use ctypes.Structure as follows :- >>> class Int(Structure): ... _fields_ = [("first_16", c_int, 16), ... ("second_16", c_int, 16)] ... >>> print Int Would it be possible for me to create such a class by using a list that I decide upon...

Dynamically create Ctypes in Python

python,dynamic,ctypes
I have a file that I read from which has definitions of ctypes that are used in a separate project. I can read the file and obtain all the necessary information to create a ctype that I want in Python like the name, fields, bitfields, ctype base class (Structure, Union,...

Why is my ctypes long size 4 and not size 8 using 64 bit python?

python,ctypes,anaconda
Downstream of this I am getting an error Python int too large to convert to C long, which led me to suspect that somehow ctypes long was too small - size 4 and not 8. I tested this and it was indeed the case. Yet, I am using Python 2.7...

Using C from Python

python,c,ctypes,network-interface
I want to know the names of the NetworkInterfaces from python, but it seems it's not possible from python so I'm using this C code: #include <Python.h> #include <windows.h> #include <iphlpapi.h> #include <stdio.h> #pragma comment(lib, "iphlpapi.lib") PyObject* GetInterfaces (PyObject* self){ ULONG buflen = sizeof(IP_ADAPTER_INFO); IP_ADAPTER_INFO *pAdapterInfo = (IP_ADAPTER_INFO *)malloc(buflen); if...