Search code examples
pythonopenglctypesaccess-violation

python - ctypes opengl functions loading error (OSError: exception: access violation writing 0x0000000000000000)


Im getting an error:

OSError: exception: access violation writing 0x0000000000000000

While trying to load an opengl function with python ctypes,

from ctypes import *
gl = windll.opengl32
get_proc_addr_prototype = WINFUNCTYPE(WINFUNCTYPE(c_uint), c_char_p)
get_proc_addr_paramflags = (1, "Arg1"),
get_proc_addr = get_proc_addr_prototype(("wglGetProcAddress", gl), get_proc_addr_paramflags)
glCreateProgram = get_proc_addr(c_char_p(b"glCreateProgram"))
glCreateProgram()

I thought it's because i haven't created an opengl context, so i ran this code:

from ctypes import *
gl = windll.opengl32
gl.glClear(0x00004000)    #    GL_COLOR_BUFFER_BIT = 0x00004000

which worked, but trying this way:

from ctypes import *
gl = windll.opengl32
get_proc_addr_prototype = WINFUNCTYPE(WINFUNCTYPE(None, c_uint), c_char_p)
get_proc_addr_paramflags = (1, "Arg1"),
get_proc_addr = get_proc_addr_prototype(("wglGetProcAddress", gl), get_proc_addr_paramflags)
glClear = wgl_get_proc_addr(c_char_p(b"glClear"))
glClear(0x00004000)    #    GL_COLOR_BUFFER_BIT = 0x00004000

causes the same error.


Solution

  • You're never creating an OpenGL context. Also if glCreateShader is available at all depends on the OpenGL version the context is created for. Why are you using ctypes in the first place anyway? There are perfectly fine OpenGL Python bindings that abstract away all of the tedious bookkeeping code for you.

    http://pyopengl.sourceforge.net/

    pip install PyOpenGL