OpenGL 加载在看似随机的点失败

OpenGL loading fails at seemingly random points

我正在使用 LoadLibrary/GetProcAddress/wglGetProcAddress 在 C# 中加载 OpenGL。由于某种原因,它无法加载 glBlendFunc 的指针,以及该点之后的其他随机指针。我已经查看了我的代码一个多小时了,所有这些失败的部分看起来完全正常吗?

加载函数指针并将其转换为委托的代码。

    public override T GetFunction<T>(string name)
    {
        IntPtr pointer = wglGetProcAddress(name);
        if (pointer == IntPtr.Zero)
            pointer = GetProcAddress(Handle, name);
        return Marshal.GetDelegateForFunctionPointer<T>(pointer);
    }

调用 GetFunction 的代码示例。

 BlendEquationSeparate = Loader.GetFunction<glBlendEquationSeparate>("glBlendEquationSeparate");
 BlendEquationSeparatei = Loader.GetFunction<glBlendEquationSeparatei>("glBlendEquationSeparatei");
 BlendFunc = Loader.GetFunction<glBlendFunc>("glBlendFunc");
 BlendFunci = Loader.GetFunction<glBlendFunci>("glBlendFunci");
 BlendFuncSeparate = Loader.GetFunction<glBlendFuncSeparate>("glBlendFuncSeparate");
 BlendFuncSeparatei = Loader.GetFunction<glBlendFuncSeparatei>("glBlendFuncSeparatei");

委托代码示例。

public delegate void glBlendEquationSeparate(BlendEquationMode modeRGB, BlendEquationMode modeAlpha);
public static glBlendEquationSeparate BlendEquationSeparate;
public delegate void glBlendEquationSeparatei(UInt32 buf, BlendEquationMode modeRGB, BlendEquationMode modeAlpha);
public static glBlendEquationSeparatei BlendEquationSeparatei;
public delegate void glBlendFunc(BlendingFactorSrc sfactor, BlendingFactorDest dfactor);
public static glBlendFunc BlendFunc;
public delegate void glBlendFunci(UInt32 buf, BlendingFactorSrc sfactor, BlendingFactorDest dfactor);
public static glBlendFunci BlendFunci;
public delegate void glBlendFuncSeparate(BlendingFactorSrc srcRGB, BlendingFactorDest dstRGB, BlendingFactorSrc srcAlpha, BlendingFactorDest dstAlpha);
public static glBlendFuncSeparate BlendFuncSeparate;
public delegate void glBlendFuncSeparatei(UInt32 buf, BlendingFactorSrc srcRGB, BlendingFactorDest dstRGB, BlendingFactorSrc srcAlpha, BlendingFactorDest dstAlpha);
public static glBlendFuncSeparatei BlendFuncSeparatei;

我正在使用的 DllImports

[DllImport("kernel32")]
public static extern IntPtr LoadLibrary(string fileName);

[DllImport("kernel32")]
public static extern IntPtr GetProcAddress(IntPtr module, string procName);

[DllImport("opengl32.dll", EntryPoint = "wglGetProcAddress", ExactSpelling = true)]
public static extern IntPtr wglGetProcAddress(string lpszProc);

所有枚举都非常标准,不需要上下文。我像往常一样加载库:LoadLibrary(opengl32.dll);。谁能向我解释我哪里出错了?

我发现了一个有点奇怪的解决方案。如果我将代码移至静态构造函数,使其在程序开头运行,它会突然起作用。我不知道为什么我的第一种方法不起作用。