首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误代码6(无效句柄) wglCreateContext失败

错误代码6(无效句柄) wglCreateContext失败
EN

Stack Overflow用户
提问于 2017-08-08 15:38:33
回答 1查看 1.1K关注 0票数 0

根据(WGL)的教程,我试图创建一个wgl上下文。无论出于什么原因,wglCreateContext返回nullGetLastError返回6或无效句柄。我以前使用过这个教程,它运行得很好。

我试图为隐藏窗口创建一个虚拟上下文,以便为用户窗口创建另一个上下文。到底怎么回事?

Windows通过winAPI标识符引用,wgl是一个全局结构,包含指向OpenGL的函数指针。

代码语言:javascript
复制
struct WglContext
{
    winAPI.HDC hdc;
    winAPI.HGLRC handle;
}

__gshared Win32GL wgl;

///WGL-specific global data.
struct Win32GL
{
    import oswald : OsWindow, WindowConfig, WindowError;

    OsWindow helperWindow;
    winAPI.HINSTANCE instance;

    PFN_wglCreateContext createContext;
    PFN_wglDeleteContext deleteContext;
    PFN_wglGetProcAddress getProcAddress;
    PFN_wglMakeCurrent makeCurrent;

    PFN_wglCreateContextAttribsARB createContextAttribsARB;
    PFN_wglGetExtensionStringARB getExtensionStringARB;
    PFN_wglGetExtensionStringEXT getExtensionStringEXT;

    bool extensionsAreLoaded;

    static void initialize()
    {
        if (wgl.instance !is null)
            return; //The library has already been initialized

        WindowConfig cfg;
        cfg.title = "viewport_gl_helper";
        cfg.hidden = true;
        auto windowError = OsWindow.createNew(cfg, &wgl.helperWindow);
        if (windowError != WindowError.NoError)
        {
            import std.conv : to;

            assert(false, "Failed to create helper window: " ~ windowError.to!string);
        }

        wgl.instance = winAPI.LoadLibrary("opengl32.dll");

        if (wgl.instance is null)
            assert(false, "Viweport failed to load opengl32.dll");

        wgl.bind(cast(void**)&wgl.createContext, "wglCreateContext\0");
        wgl.bind(cast(void**)&wgl.deleteContext, "wglDeleteContext\0");
        wgl.bind(cast(void**)&wgl.getProcAddress, "wglGetProcAddress\0");
        wgl.bind(cast(void**)&wgl.makeCurrent, "wglMakeCurrent\0");
    }

    static void terminate()
    {
        if (!wgl.instance)
            return;

        winAPI.FreeLibrary(wgl.instance);
        wgl.helperWindow.destroy();
    }

    void bind(void** func, in string name)
    {
        *func = winAPI.GetProcAddress(this.instance, name.ptr);
    }
}

struct WglContext
{
    winAPI.HDC hdc;
    winAPI.HGLRC handle;
}

WglContext wglCreateTmpContext()
{
    assert(wgl.instance);

    winAPI.PIXELFORMATDESCRIPTOR pfd;
    pfd.nSize = winAPI.PIXELFORMATDESCRIPTOR.sizeof;
    pfd.nVersion = 1;
    pfd.dwFlags = winAPI.PFD_DRAW_TO_WINDOW | winAPI.PFD_SUPPORT_OPENGL | winAPI.PFD_DOUBLEBUFFER;
    pfd.iPixelType = winAPI.PFD_TYPE_RGBA;
    pfd.cColorBits = 24;

    auto hdc = winAPI.GetDC(wgl.helperWindow.platformData.handle);

    const pixelFormat = winAPI.ChoosePixelFormat(hdc, &pfd);
    if (winAPI.SetPixelFormat(hdc, pixelFormat, &pfd) == winAPI.FALSE)
        assert(false, "Failed to set pixel format for temp context");

    writeln(hdc);
    writeln(pixelFormat);

    winAPI.HGLRC context = wgl.createContext(hdc);
    if (context is null)
    {
        import std.conv: to;
        assert(false, "Failed to create temp context: Error Code " ~ winAPI.GetLastError().to!string);
    }

    if (wgl.makeCurrent(hdc, context) == winAPI.FALSE)
    {
        wgl.deleteContext(context);
        assert(false, "Failed to make temp context current");
    }

    return WglContext(hdc, context);
}

void main()
{
    wgl.initialize(); //Fetches function pointers, and loads opengl32.dll
    auto context = wglCreateTmpContext();
    wglDeleteContext(context); //Delegates to wgl.deleteContext
    wgl.terminate(); //Unloads opengl32.dll and nulls function pointers
}
EN

回答 1

Stack Overflow用户

发布于 2017-08-09 15:43:15

我对你使用的那个winAPI一无所知。不管怎样,我确信:

你必须:

  1. 创建一个窗口并获取它的HWND句柄(Windows,见下文)。提示,该窗口使用WS_CLIPCHILDREN | WS_CLIPSIBLINGS样式;但也可以尝试WS_OVERLAPPEDWINDOW,如您发布的链接中所示。
  2. 验证您得到了一个有效的HWND,并从它获得了一个有效的HDC

HWNDHDC被定义为*void。我不相信auto

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45572591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档