C# - kernel32.dll 的 GetProcedureAddress 问题

C# - Problems with kernel32.dll's GetProcedureAddress

我是 P/Invoking LoadLibrary,正在加载 opengl32.dll。我有所有 OpenGL 函数的委托和加载代码,如下例所示:

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));

出于某种原因,Library.GetProcedureAddress returns 0x00000000000。有谁知道为什么会这样吗?

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

这不是从 OpenGL 1.1 之后的版本加载 OpenGL 函数的方式。

而是使用 wglGetProcAddress(在使上下文成为当前上下文之后)。这个函数知道如何为您的视频卡特定的 OpenGL 实现搜索驱动程序...不是 opengl32.dll

如果您不知道您的函数是 OpenGL 1.1 还是更高版本,您应该按照 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:

当 运行 在其他操作系统上时,您也不应该在共享库上执行 dlsym,而是调用 glXGetProcAddress.