c++ 创建渲染上下文,使用win API检查GL_VERSION

wsxa1bj1  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(111)

error(1)Severity Code Description项目文件字符串抑制状态警告C28252“WinMain”的注解不一致:Param(3)包含前一示例中的“SAL_null(__no)”。请参见c:\program files(x86)\windows kits\10\include\10.0.19041.0\um\winbase.h(1006)。Project1 C:\Users\setro\OneDrive\Desktop\Project1\Source.cpp 8
error(2)Severity Code Description项目文件字符串抑制状态警告C28253“WinMain”的注解不一致:Param(3)在本例中包含“SAL_null(__maybe)”。请参见c:\program files(x86)\windows kits\10\include\10.0.19041.0\um\winbase.h(1006)。Project1 C:\Users\setro\OneDrive\Desktop\Project1\Source.cpp 8
error(3)Severity Code Description项目文件字符串抑制状态错误LNK 2019引用函数“int __cdecl invoke_main(void)”中未解析的外部主要字符(?)invoke_main@@YAHXZ)。Project1 C:\Users\setro\OneDrive\Desktop\Project1\MSVCRTD.lib(exe_main.obj)1
error(4)Severity Code Description项目文件字符串抑制状态LNK 1120未解析外部元素的错误:1 Project1 C:\Users\setro\OneDrive\Desktop\Project1\x64\Pwg\Project1.exe 1

#include <windows.h>
#include <GL/GL.h>

#pragma comment (lib, "opengl32.lib")

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd)
{
    MSG msg = { 0 };
    WNDCLASS wc = { 0 };
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
    wc.lpszClassName = L"oglversionchecksample";
    wc.style = CS_OWNDC;
    if (!RegisterClass(&wc))
        return 1;
    CreateWindowW(wc.lpszClassName, L"openglversioncheck", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 640, 480, 0, 0, hInstance, 0);

    while (GetMessage(&msg, NULL, 0, 0) > 0)
        DispatchMessage(&msg);

    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
    {
        PIXELFORMATDESCRIPTOR pfd =
        {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,    //Flags
            PFD_TYPE_RGBA,        // The kind of framebuffer. RGBA or palette.
            32,                   // Colordepth of the framebuffer.
            0, 0, 0, 0, 0, 0,
            0,
            0,
            0,
            0, 0, 0, 0,
            24,                   // Number of bits for the depthbuffer
            8,                    // Number of bits for the stencilbuffer
            0,                    // Number of Aux buffers in the framebuffer.
            PFD_MAIN_PLANE,
            0,
            0, 0, 0
        };

        HDC ourWindowHandleToDeviceContext = GetDC(hWnd);

        int  letWindowsChooseThisPixelFormat;
        letWindowsChooseThisPixelFormat = ChoosePixelFormat(ourWindowHandleToDeviceContext, &pfd);
        SetPixelFormat(ourWindowHandleToDeviceContext, letWindowsChooseThisPixelFormat, &pfd);

        HGLRC ourOpenGLRenderingContext = wglCreateContext(ourWindowHandleToDeviceContext);
        wglMakeCurrent(ourWindowHandleToDeviceContext, ourOpenGLRenderingContext);

        MessageBoxA(0, (char*)glGetString(GL_VERSION), "OPENGL VERSION", 0);

        //wglMakeCurrent(ourWindowHandleToDeviceContext, NULL); Unnecessary; wglDeleteContext will make the context not current
        wglDeleteContext(ourOpenGLRenderingContext);
        PostQuitMessage(0);
    }
    break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;

}

图片来源:https://www.khronos.org/opengl/wiki/Creating_an_OpenGL_Context_(WGL)

eqfvzcg8

eqfvzcg81#

WinMain()的第三个参数不是可选的,所以使用__in而不是__in_opt(或者根本不注解WinMain(),因为开始调用它的不是您)。
此外,您还缺少WinMain()上的WINAPI__stdcall)调用约定。参见winbase.hWinMain()的声明:

int
#if !defined(_MAC)
#if defined(_M_CEE_PURE)
__clrcall
#else
WINAPI
#endif
#else
CALLBACK
#endif
WinMain (
    __in HINSTANCE hInstance,
    __in_opt HINSTANCE hPrevInstance,
    __in LPSTR lpCmdLine,
    __in int nShowCmd
    );

另外,请确保您的项目设置为GUI项目,而不是控制台项目。GUI应用使用WinMain(),控制台应用使用main()。在项目设置中,Linker -> System -> SubSystem应该是Windows

相关问题