ctypes

ctypes/C++ segfault accessing member variables

牧云@^-^@ 提交于 2021-02-08 01:49:07
问题 I am no stranger to the python ctypes module, but this is my first attempt at combining C++, C and Python all in one code. My problem seems to be very similar to Seg fault when using ctypes with Python and C++, however I could not seem to solve the problem in the same way. I have a simple C++ file called Header.cpp : #include <iostream> class Foo{ public: int nbits; Foo(int nb){nbits = nb;} void bar(){ std::cout << nbits << std::endl; } }; extern "C" { Foo *Foo_new(int nbits){ return new Foo

Python: Similar functionality in struct and array vs ctypes

蓝咒 提交于 2021-02-07 22:17:37
问题 Python provides the following three modules that deal with C types and how to handle them: struct for C structs array for arrays such as those in C ctypes for C functions, which necessarily entails dealing with C’s type system While ctypes seems more general and flexible (its main task being “a foreign function library for Python”) than struct and array , there seems to be significant overlap in functionality between these three modules when the task is to read binary data structures. For

cannot load dll in python with ctypes

孤人 提交于 2021-02-07 18:31:06
问题 I am trying to load a dll form python code with ctypes and it raised an error. my python code: import ctypes from ctypes import * hllDll = ctypes.WinDLL ("c:\\Users\\saar\\Desktop\\pythonTest\\check.dll") and this raised error: Traceback (most recent call last): File "C:\AI\PythonProject\check.py", line 5, in <module> hllDll = ctypes.WinDLL("c:\\Users\\saar\\Desktop\\pythonTest\\check.dll") File "C:\Python27\lib\ctypes\__init__.py", line 365, in __init__ self._handle = _dlopen(self._name,

Python ctypes addressof CFuncType

萝らか妹 提交于 2021-02-07 14:25:32
问题 Related to my other question How can I get the address (acctual function pointer) to a CFuncType object? addressof() does not report the correct address. C code: extern "C" _declspec(dllexport) int addr(int (*func)()) { int r = (int)func; return r; } Python code: def test(): return 42 t = CFUNCTYPE(c_int) f = t(test) print addressof(f) print dll.addr(f) Output: 7030864 3411932 trying to call *(7030864) from C causes a crash, but calling *(3411932) works as expected. What's wrong with

Python ctypes addressof CFuncType

别等时光非礼了梦想. 提交于 2021-02-07 14:23:50
问题 Related to my other question How can I get the address (acctual function pointer) to a CFuncType object? addressof() does not report the correct address. C code: extern "C" _declspec(dllexport) int addr(int (*func)()) { int r = (int)func; return r; } Python code: def test(): return 42 t = CFUNCTYPE(c_int) f = t(test) print addressof(f) print dll.addr(f) Output: 7030864 3411932 trying to call *(7030864) from C causes a crash, but calling *(3411932) works as expected. What's wrong with

How to work with UTF-16 in python ctypes?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-07 10:09:43
问题 I have a foreign C library which uses utf-16 in API: as function arguments, return values and structure members. On Windows its OK with ctypes.c_wchar_p, but under OSX ctypes uses UCS-32 in c_wchar and I could not find the way to support utf-16. Here is my research: Use _SimpleCData subclassing to redefine _check_retval_. it allows a transparent conversion of utf-16 to Python string. can be placed as C structure member But it doesn't allow to handle strings as arguments, its from_param()

How to work with UTF-16 in python ctypes?

萝らか妹 提交于 2021-02-07 10:09:34
问题 I have a foreign C library which uses utf-16 in API: as function arguments, return values and structure members. On Windows its OK with ctypes.c_wchar_p, but under OSX ctypes uses UCS-32 in c_wchar and I could not find the way to support utf-16. Here is my research: Use _SimpleCData subclassing to redefine _check_retval_. it allows a transparent conversion of utf-16 to Python string. can be placed as C structure member But it doesn't allow to handle strings as arguments, its from_param()

How to work with UTF-16 in python ctypes?

喜欢而已 提交于 2021-02-07 10:07:23
问题 I have a foreign C library which uses utf-16 in API: as function arguments, return values and structure members. On Windows its OK with ctypes.c_wchar_p, but under OSX ctypes uses UCS-32 in c_wchar and I could not find the way to support utf-16. Here is my research: Use _SimpleCData subclassing to redefine _check_retval_. it allows a transparent conversion of utf-16 to Python string. can be placed as C structure member But it doesn't allow to handle strings as arguments, its from_param()

user pointer in python

做~自己de王妃 提交于 2021-02-07 04:19:26
问题 *I am trying to display preview from webcam captured using v4l. Here is an idea of how the code looks like: from ctypes import * from v4l2 import * from Image import fromstring from Tkinter import Tk, Label from ImageTk import PhotoImage from ctypes.util import find_library libc = CDLL(find_library('c')) posix_memalign = libc.posix_memalign getpagesize = libc.getpagesize device_name = '/dev/video0' ps = preview_settings = { 'width': 320, 'height': 240, 'pixformat': 'RGB', } PIX_FMT = V4L2_PIX

user pointer in python

痴心易碎 提交于 2021-02-07 04:18:02
问题 *I am trying to display preview from webcam captured using v4l. Here is an idea of how the code looks like: from ctypes import * from v4l2 import * from Image import fromstring from Tkinter import Tk, Label from ImageTk import PhotoImage from ctypes.util import find_library libc = CDLL(find_library('c')) posix_memalign = libc.posix_memalign getpagesize = libc.getpagesize device_name = '/dev/video0' ps = preview_settings = { 'width': 320, 'height': 240, 'pixformat': 'RGB', } PIX_FMT = V4L2_PIX