用C++代码获取显卡支持的OpenGL最高版本的代码很简单:
const GLubyte* OpenGLVersion = glGetString(GL_VERSION); //返回当前OpenGL实现的版本号
但是这行代码必须在windows desktop application建立起图形化环境并启用opengl以后才能被获取到,因此我们可以做一个一闪而过的windows desktop application来将获取到的版本信息写入一个文件,其它需要这个信息的代码可以从这个文件来读取。
具体实现过程如下:
1. visual studio 2022 community 新建项目,选择C++, Windows, Desktop, Desktop Application
2. 在项目的源文件<project_name>.cpp的WndProc函数中添加写入版本到文件(C:\idealand\sysinfo\opengl_version.txt)的代码:
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);
char* opengl_version = (char*)glGetString(GL_VERSION);
std::ofstream opengl_version_file("C:\\idealand\\sysinfo\\opengl_version.txt"); opengl_version_file.write(opengl_version, strlen(opengl_version)); opengl_version_file.close();
//MessageBoxA(0, opengl_version, "OPENGL VERSION", 0);
wglDeleteContext(ourOpenGLRenderingContext);
PostQuitMessage(0);
}
break;
//-------------添加代码结束 -------------------
case WM_COMMAND:
{
.................
build 执行这个项目就会在文件(C:\idealand\sysinfo\opengl_version.txt)中得到版本号,比如:
---------------------------
4.6.0 NVIDIA 512.15
---------------------------