0

我正在使用 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);. 谁能向我解释我哪里出错了?

4

1 回答 1

0

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

于 2022-01-03T03:00:26.727 回答