How to receive and return Windows data types in cython

179 views
Skip to first unread message

vinod kc

unread,
Jan 6, 2024, 8:32:55 AM1/6/24
to cython-users
Hi all,
I am writing a windows gui program in python. I am using ctypes to handle windows special data types and calling api functions. Recently I found that Cython will speedup certain areas of my program. So I am planning to move some work load of this program to a cython function where it takes windows data types like HDC, RECT etc and returns an HBRUSH. HDC & HBRUSH are void pointers. Ctypes will provide these types in python. But the problem is Cython can't return an HBRUSH from a function. How do I do it in Cython ? Thanks in advance.

da-woods

unread,
Jan 9, 2024, 3:23:42 AM1/9/24
to cython...@googlegroups.com
I'm slightly unclear what the exact problem is, but I guess your issue is returning these types to Python code, since Cython believes that functions returning pointers can only be used from Cython code.

If that's correct, I think you have two options:
1. Convert the handles to/from integers (by casting to uintptr_t before you return them). This isn't hugely type-safe since it lets you mix up different types of handle.
2. Create a cdef class that wraps each type of handle and return that.



On 06/01/2024 13:26, vinod kc wrote:
Hi all,
I am writing a windows gui program in python. I am using ctypes to handle windows special data types and calling api functions. Recently I found that Cython will speedup certain areas of my program. So I am planning to move some work load of this program to a cython function where it takes windows data types like HDC, RECT etc and returns an HBRUSH. HDC & HBRUSH are void pointers. Ctypes will provide these types in python. But the problem is Cython can't return an HBRUSH from a function. How do I do it in Cython ? Thanks in advance.
--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/47538c45-58b7-4eff-ab7e-36c34a1e911en%40googlegroups.com.


vinod kc

unread,
Jan 9, 2024, 8:54:32 AM1/9/24
to cython-users
@D Woods,
Thank you for the reply. You guessed it right. I want to return windows data types as well as I want to receive windows data types as function parameters. From your suggestions, I like the option 2. Even though I didn't know how to do it, but I hope I can find it out from the web. 

Reply all
Reply to author
Forward
0 new messages