glfwGetWindowUserPointer: Correct use between C and Python using ctypes

Using GLFW C api I have wrapped a simple function as a shared library

// myfunction.h
__declspec(dllexport) int glfwGetSkinSurface(GLFWwindow* window);

then the shared library is loaded in python using ctypes.

import ctypes as ct

clib = ct.CDLL("myfunction.dll")
glfwGetSkinSurface = clib.glfwGetSkinSurface
glfwGetSkinSurface.argtypes = [ct.c_void_p] # <- correct?
glfwGetSkinSurface.restypes = [ct.c_int]  

then calling the function from another file in these formats. All of them are producing an error.

result = glfwGetSkinSurface(glfw.get_window_user_pointer(window))
# OSError: exception: access violation reading 0x0000000000000358

result = glfwGetSkinSurface(ct.c_void_p(glfw.get_window_user_pointer(window)))
# OSError: exception: access violation reading 0x0000000000000358

result = glfwGetSkinSurface(ct.cast(glfw.get_window_user_pointer(window), ct.c_void_p))
# OSError: exception: access violation reading 0x0000000000000358

How do I solve this problem?

Prashant

There are a few existing GLFW bindings for Python. Here’s a few (they may be forks of each other though, don’t know):

Maybe you can see how they do it? I am creating bindings for F#, but I haven’t created bindings for that specific function (Edit: I see now that that is your special wrapper function). And I haven’t used ctypes to any extent.

However, all that being said, I have creating bindings for functions that use GLFWwindow, and I don’t think you should be declaring the argument type as c_void_p. That is the same as void* (see ctypes — A foreign function library for Python — Python 3.11.4 documentation), and you need a pointer to GLFWwindow. I think you need to use POINTER to declare the type. See here: ctypes — A foreign function library for Python — Python 3.11.4 documentation.