How to pass a 2d array from Python to C?

Listing [SciPy.Docs]: C-Types Foreign Function Interface (numpy.ctypeslib) (and [Python.Docs]: ctypes – A foreign function library for Python just in case). This is a exactly like [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati’s answer) (a duplicate), it just happens to also involve NumPy. In other words, you have Undefined Behavior, as … Read more

Can I access ImageMagick API with Python?

I would recommend using Wand (explanations follows). I was looking for proper binding to ImageMagick library, that would: work error/problem free be regularly maintained and up to date allow nice objective Python But indeed python API (binding) has too many different (mostly discontinued) versions. After reading a nice historical overview by Benjamin Schweizer it has … Read more

Maximum and minimum value of C types integers from Python

According to: [Python 3 docs]: Numeric Types – int, float, complex: Integers have unlimited precision. Translated to code: >>> i = 10 ** 100 >>> i 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 >>> len(str(i)) 101 >>> i.bit_length() 333 On the other hand, each C type has a fixed size (depending on platform / architecture), as clearly shown in [CPPReference]: Fundamental … Read more

Can’t import dll module in Python

Starting with Python 3.8, the .dll search mechanism has changed (Win specific). According to [Python.Docs]: os.add_dll_directory(path) (emphasis is mine): Add a path to the DLL search path. This search path is used when resolving dependencies for imported extension modules (the module itself is resolved through sys.path), and also by ctypes. … Availability: Windows. So, you … Read more

How do I set the desktop background in python? (windows)

The following works for me. I’m using Windows 10 64-bit and Python 3. import os import ctypes from ctypes import wintypes drive = “c:\\” folder = “test” image = “midi turmes.png” image_path = os.path.join(drive, folder, image) SPI_SETDESKWALLPAPER = 0x0014 SPIF_UPDATEINIFILE = 0x0001 SPIF_SENDWININICHANGE = 0x0002 user32 = ctypes.WinDLL(‘user32’) SystemParametersInfo = user32.SystemParametersInfoW SystemParametersInfo.argtypes = ctypes.c_uint,ctypes.c_uint,ctypes.c_void_p,ctypes.c_uint SystemParametersInfo.restype … Read more

Why won’t this ctypes code work with Python 3.3 but will work with Python 2.7?

SystemParametersInfoA requires a 8-bit ANSI encoded input string as a parameter, which is known as mbcs encoding in Python. You will have to use SystemParametersInfoW in python3. This is because SystemParametersInfoW takes in a UTF-16 wide string (which is wchar_t * in C) and the ctypes library automatically converts this passed unicode argument into c_wchar_p. … Read more

python ctype recursive structures

You almost certainly want to declare next_command as a pointer. Having a structure that contains itself isn’t possible (in any language). I think this is what you want: class EthercatDatagram(Structure): pass EthercatDatagram._fields_ = [ (“header”, EthercatDatagramHeader), (“packet_data_length”, c_int), (“packet_data”, c_char_p), (“work_count”, c_ushort), (“next_command”, POINTER(EthercatDatagram))]

How to use IFileOperation from ctypes

Sure, it’s located in pythoncom and shell for constants, for example: from win32com.shell import shell import pythoncom # create an instance of IFileOperation fo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation, None, pythoncom.CLSCTX_ALL, shell.IID_IFileOperation) # here you can use SetOperationFlags, progress Sinks, etc. # create an instance of IShellItem for the source item item1 = shell.SHCreateItemFromParsingName(“c:\\temp\\source.txt”, None, shell.IID_IShellItem) # create … Read more