How to return Windows data types like HBRUSH, HWND from cython

Hi all,
I am using python to create my windows gui program. In python, I am using ctypes to handle Windows special data types. Now I want to use cython to speed up certain areas of my program. So I want to take windows data types like HDC, RECT etc from python as arguments and return an HBRUSH to python. HDC & HBRUSH are actually void pointers. How to take these data types as parameters and how to return these types from cython ? Thanks in advance.

Some things are exposed via ctypes.wintypes: cpython/Lib/ctypes/wintypes.py at main · python/cpython · GitHub

If not, you would create a ctypes.Structure matching up with the C definition from the WinApi docs and/or the Windows SDK. Use wintypes.py as a set of examples for how to do that.

Edit: I misread. I thought you were asking about CPython. I’m not sure how it works in Cython. Sorry about that.

Python Cython Tutorial – Speeding up your Code 1000x

As far as I read you either keep your Python code or you cyphon ypur python code. Are you instead saying you want to have two different programs? One in Python and one in Cython?

So you mean, I can’t return pointer data types from my cython function, right ?

Are you instead saying you want to have two different programs? One in Python and one in Cython?

No. I want to mix cython and python in the same program. I am drawing a button with windows api functions. Ctypes is handling the ffi work between python and windows api. But I found that I can speed up the process if certain parts of this functions are moved to cython. I tried that with a dll created in D programming language and gained 2.5X performance. So I want to experiment with cython too. But Can’t receive pointer data types as parameters from python and can’t return pointer data types to python. That’s my problem.

No probs. At the first glance, I thought I got some directions to look in.

You can import ctypes from Cython code and pass around those objects at the borders between Cython and Python. This won’t be pretty and involves quite a few casts, but it should work.

Thank you @MegaIng