-2

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?

  • How exactly would I do that? –  Mar 04 '21 at 22:52
  • FWIW the actual Win32 function is `GetProcAddress`, only your p/invoke wrapper spells out "Procedure". This may help you in searching... – Ben Voigt Mar 04 '21 at 23:06
  • I have an abstraction over it, because I have code for multiple platforms. –  Mar 04 '21 at 23:08
  • 1
    For example, by using the correct name you would easily find [the official documentation for `GetProcAddress`](https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getprocaddress) which tells you that when the return value is NULL, you should call `GetLastError()`. You can have p/invoke do this for you by putting the appropriate flag on your DllImportAttribute. – Ben Voigt Mar 04 '21 at 23:08
  • As written, you are asking a question about your abstraction which no one can answer but you. I think you want to ask about the Win32 functions that your abstraction uses (and then you should show the pinvoke import, actual function call, actual parameter values, etc.) – Ben Voigt Mar 04 '21 at 23:10
  • I am just using GetProcAddress, but I have an abstraction over it because I have the equivalent of other systems also (OSX, Linux). –  Mar 04 '21 at 23:11

1 Answers1

0

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • It did not work. It returned 0x0000000000. I ran GetLastError() and it returned 6. –  Mar 04 '21 at 23:29
  • `net helpmsg 6` tells you that error means "The handle is invalid". – Ben Voigt Mar 04 '21 at 23:30
  • I tested it with glActiveShaderProgram. –  Mar 04 '21 at 23:31
  • The [documentation for `wglGetProcAddress`](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-wglgetprocaddress) tells you it will return NULL when no current rendering context exists. Have you created a context and called `wglMakeCurrent` before using `wglGetProcAddress` ? – Ben Voigt Mar 04 '21 at 23:33
  • I setup GLFW, and it works! Thanks! I will accept your answer. –  Mar 04 '21 at 23:39