Search code examples
c#interoppinvokeloadlibrary

C# - Problems with kernel32.dll's GetProcedureAddress


I am P/Invoking LoadLibrary, and loading opengl32.dll. I have delegates and loading code for all the OpenGL functions, just like this example below:

internal delegate void ActiveShaderProgram(UInt32 pipeline, UInt32 program);

IntPtr glActiveShaderProgram_Ptr = Library.GetProcedureAddress("glActiveShaderProgram");
Delegates.glActiveShaderProgram = (Delegates.ActiveShaderProgram)Marshal.GetDelegateForFunctionPointer(glActiveShaderProgram_Ptr, typeof(Delegates.ActiveShaderProgram));

For some reason, Library.GetProcedureAddress returns 0x00000000000. Does anyone know why this is the case?


Solution

  • I am P/Invoking LoadLibrary, and loading opengl32.dll. I have delegates and loading code for all the OpenGL functions

    That isn't how you load OpenGL functions from later than OpenGL 1.1.

    Instead, use wglGetProcAddress (after making a context current). This function knows how to search the driver for your videocard-specific OpenGL implementation... not opengl32.dll

    If you don't know whether your function is OpenGL 1.1, or is later, you should try both as recommended by https://www.khronos.org/opengl/wiki/Load_OpenGL_Functions

    wglGetProcAddress will not return function pointers from any OpenGL functions that are directly exported by the OpenGL32.DLL itself. This means the old ones from OpenGL version 1.1. Fortunately those functions can be obtained by the Win32's GetProcAddress. On the other hand GetProcAddress will not work for the functions for which wglGetProcAddress works. So in order to get the address of any GL function one can try with wglGetProcAddress and if it fails, try again with the Win32's GetProcAddress:

    When running on other operating systems you shouldn't be doing dlsym on the shared library either, but call glXGetProcAddress.