What's the error"CreateDXGIFactory cannot be called from DllMain"?

I tried to draw a cone with the glu library,but the output of the program is a blank graphic frame,not the cone.
In the output window of vs studio 2019, there are many lines of

    DXGI ERROR: CreateDXGIFactory cannot be called from DllMain. [ MISCELLANEOUS ERROR #76: ]

My classmate uses vs studio 2012,so it works correctly.

#include "stdafx.h"
#include <GL/glut.h>   
#include <math.h>    

GLUquadricObj* quadObj1;
GLUquadricObj* quadObj2;
GLUquadricObj* quadObj3;
GLUquadricObj* quadObj4;
GLUquadricObj* quadObj5;

static float light_ambient[] = { 0.1,0.1,0.1,1.0 };
static float light_diffuse[] = { 0.5,1.0,1.0,1.0 };
static float light_specular[] = { 0.5,1.0,1.0,1.0 };
static float light_position[] = { 90.0,90.0,150.0,0.0 };

static float front_mat_shininess[] = { 60.0 };
static float front_mat_specular[] = { 0.2,0.2,0.2,1.0 };
static float front_mat_diffuse[] = { 0.5,0.5,0.28,1.0 };

static float back_mat_shininess[] = { 60.0 };
static float back_mat_specular[] = { 0.5,0.5,0.2,1.0 };
static float back_mat_diffuse[] = { 1.0,0.9,0.2,1.0 };

static float Imodel_ambient[] = { 1.0,1.0,1.0,1.0 };
static float Imodel_twoside[] = { GL_TRUE };
static float Imodel_oneside[] = { GL_FALSE };

void myInit(void)
{
	
	glClearColor(1.0, 1.0, 1.0, 1.0);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	
	glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);   
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);    
	glLightfv(GL_LIGHT0, GL_POSITION, light_position); 

	
	glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);

	glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
	glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
	glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);

	
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, Imodel_ambient);
	glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, Imodel_twoside);

	
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glShadeModel(GL_SMOOTH);
}

void myDisplay(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	/*create four quadric objects*/
	quadObj1 = gluNewQuadric();
	quadObj2 = gluNewQuadric();
	quadObj3 = gluNewQuadric();
	quadObj4 = gluNewQuadric();
	quadObj5 = gluNewQuadric();

	/*draw a cylinder*/
	glPushMatrix();
	gluQuadricDrawStyle(quadObj1, GLU_FILL);
	gluQuadricNormals(quadObj1, GL_FLAT);
	gluQuadricOrientation(quadObj1, GLU_INSIDE);
	gluQuadricTexture(quadObj1, GL_TRUE);

	glColor3f(1.0, 1.0, 0.0);
	glRotatef(30, 1.0, 0.0, 0.0);
	glRotatef(40, 0.0, 1.0, 0.0);
	gluCylinder(quadObj1, 2.0, 2.0, 9.0, 20.0, 8.0);
	glPopMatrix();

	/*draw a cone*/
	glPushMatrix();
	gluQuadricDrawStyle(quadObj5, GLU_FILL);
	gluQuadricNormals(quadObj5, GL_FLAT);
	gluQuadricOrientation(quadObj5, GLU_INSIDE);
	gluQuadricTexture(quadObj5, GL_TRUE);
	//glColor3f(1.0,0.0,0.0); 
	glRotatef(30, 1.0, 0.0, 0.0);
	glRotatef(40, 0.0, 1.0, 0.0);
	gluQuadricDrawStyle(quadObj5, GLU_FILL);
	glTranslatef(5.0, 5.0, 0.0);
	gluCylinder(quadObj5, 3.0, 0.0, 5.0, 20.0, 8.0);
	glPopMatrix();


	/*draw a sphere*/
	glPushMatrix();
	gluQuadricDrawStyle(quadObj2, GLU_SILHOUETTE);
	glTranslatef(-5.0, -1.0, 0.0);
	gluSphere(quadObj2, 3.0, 20.0, 20.0);
	glPopMatrix();

	/*draw a disk*/
	glPushMatrix();
	gluQuadricDrawStyle(quadObj3, GLU_LINE);
	glTranslatef(-2.0, 4.0, 0.0);
	gluDisk(quadObj3, 2.0, 5.0, 15.0, 10.0);
	glPopMatrix();

	/*draw a part of a disk*/
	glPushMatrix();
	gluQuadricDrawStyle(quadObj4, GLU_POINT);
	glTranslatef(-3.0, -7.0, 0.0);
	gluPartialDisk(quadObj4, 2.0, 5.0, 15.0, 10.0, 10.0, 100.0);
	glPopMatrix();

	/*delete four Quadric objects*/
	gluDeleteQuadric(quadObj1);
	gluDeleteQuadric(quadObj2);
	gluDeleteQuadric(quadObj3);
	gluDeleteQuadric(quadObj4);
	gluDeleteQuadric(quadObj5);
	glFlush();
}

void myReshape(int w, int h)
{
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 1.0, 50.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslatef(0.0, 0.0, -25.0);
}

int main(int argc, char** argv)
{
	/*initialize*/
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(400, 400);
	glutInitWindowPosition(100, 100);

	/*create a window*/
	glutCreateWindow(" DRAW QUADRIC OBJECTS ");

	/*draw and display*/
	myInit();
	glutReshapeFunc(myReshape);
	glutDisplayFunc(myDisplay);

	glutMainLoop();
	return 0;
}

The issue isn’t with the code, but with the setup of your development toolchain. Maybe you’re linking some libraries which you shouldn’t be linking, maybe your version of GLUT isn’t compatible. Ultimately, we can only guess.

FWIW, CreateDXGIFactory is a DirectX function which shouldn’t have any relevance to an OpenGL project.

1 Like
  • OpenGL and Direct3D are built on top of DXGI
  • OpenGL and Direct3D are “producers” in DXGI speak.
  • Initializing OpenGL will indirectly init DXGI.
  • CreateDXGIFactory is one of those init functions.

This error tends to suggest that an OpenGL context may be being initialized from inside a DLL entry/exit function rather than from inside the main program flow (see the link below).

Had you not posted your code, I would have suggested you double-check that you’re not indirectly initializing an OpenGL context inside the constructors of global objects or shared libraries. However, your code above looks like it’s doing the right thing as far as setting up the GL context via GLUT (…at least for what we can see here).

So I’d start out by commenting out (or #if’ing out) big sections of your program until the problem goes away. Then you’ll know what’s causing this. If it never goes away, then look to what libraries you are linking with. It could be that one of them has “gone rogue” and is doing something like trying to create and initialize an OpenGL context inside of them. You’ll know it’s this because removing the link dependency on that library will get rid of your DXGI error. That’s my guess.

1 Like

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.