CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2017
    Posts
    50

    [RESOLVED] Exception thrown at 0x00000000 when using DLL.

    I wrote a function that compile vertex shader and fragment shader of GLSL and return a shader program ID.If I put that function code directly a program it works fine.The problem is when I compile that code as a DLL and use it in a program, the dll throw an exception.Exception thrown at 0x00000000 in Triangle.exe: 0xC0000005: Access violation executing location 0x00000000.

    Name:  Access Violation at.jpg
Views: 3327
Size:  28.0 KB

    Here's the code;
    Code:
    #include "stdafx.h"
    
    
    GLuint createShaderProgram(const char* vertex_file, const char* fragment_file)
    {
    	using namespace std;
    
    	fstream vertexFile;
    	vertexFile.open(vertex_file, ios::in);
    
    	string inputVertexCode;
    	string vertexCode_str;
    	ostringstream oivc;
    	GLuint vertexShader = 0;
    
    	int success;
    	char infoLog[512];
    
    	if (vertexFile)
    	{
    		while (getline(vertexFile, inputVertexCode))
    		{
    			oivc << inputVertexCode << endl;
    		}
    		vertexCode_str = oivc.str();
    		const char* vertexCode = vertexCode_str.c_str();
    
    		vertexShader = glCreateShader(GL_VERTEX_SHADER); //Exception is thrown at this part
    		glShaderSource(vertexShader, 1, &vertexCode, 0);
    		glCompileShader(vertexShader);
    
    		glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    		if (!success)
    		{
    			glGetShaderInfoLog(vertexShader, 512, 0, infoLog);
    			MessageBoxA(0, infoLog, "ERROR-Failed to compile vertex shader !", MB_OK | MB_ICONSTOP);
    		}
    	}
    	else
    	{
    		MessageBoxA(0, "Failled to open vertex shader source code file!", "ERROR", MB_OK | MB_ICONSTOP);
    	}
    
    	fstream fragmentFile;
    	fragmentFile.open(fragment_file, ios::in);
    
    	string inputFragmentCode;
    	string fragmentCode_str;
    	ostringstream oifc;
    	GLuint fragmentShader = 0;
    
    	if (fragmentFile)
    	{
    		while (getline(fragmentFile, inputFragmentCode))
    		{
    			oifc << inputFragmentCode << endl;
    		}
    		fragmentCode_str = oifc.str();
    
    		const char* fragmentCode = fragmentCode_str.c_str();
    
    		fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    		glShaderSource(fragmentShader, 1, &fragmentCode, 0);
    		glCompileShader(fragmentShader);
    
    		glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    		if (!success)
    		{
    			glGetShaderInfoLog(fragmentShader, 512, 0, infoLog);
    			MessageBoxA(0, infoLog, "ERROR-Failed to compile fragment shader", MB_OK | MB_ICONSTOP);
    		}
    	}
    	else
    	{
    		MessageBoxA(0, "Failled to open fragment shader source code file!", "ERROR", MB_OK | MB_ICONSTOP);
    	}
    
    	GLuint shaderProgram = 0;
    	shaderProgram = glCreateProgram();
    	glAttachShader(shaderProgram, vertexShader);
    	glAttachShader(shaderProgram, fragmentShader);
    	glLinkProgram(shaderProgram);
    
    	glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    	if (!success)
    	{
    		glGetProgramInfoLog(shaderProgram, 512, 0, infoLog);
    		MessageBoxA(0, infoLog, "ERROR-Failed to create shader program", MB_OK | MB_ICONSTOP);
    	}
    
    	return shaderProgram;
    }

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Exception thrown at 0x00000000 when using DLL.

    At which line does the issue occur?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Oct 2017
    Posts
    50

    Re: Exception thrown at 0x00000000 when using DLL.

    Code:
    if (vertexFile)
    	{
    		while (getline(vertexFile, inputVertexCode))
    		{
    			oivc << inputVertexCode << endl;
    		}
    		vertexCode_str = oivc.str();
    		const char* vertexCode = vertexCode_str.c_str();
    
    		vertexShader = glCreateShader(GL_VERTEX_SHADER); //Exception is thrown at this part
    		glShaderSource(vertexShader, 1, &vertexCode, 0);
    		glCompileShader(vertexShader);
    
    		glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    		if (!success)
    		{
    			glGetShaderInfoLog(vertexShader, 512, 0, infoLog);
    			MessageBoxA(0, infoLog, "ERROR-Failed to compile vertex shader !", MB_OK | MB_ICONSTOP);
    		}

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Exception thrown at 0x00000000 when using DLL.

    What is the type of DLL you created?
    Did you try to debug the DLL code?
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2017
    Posts
    50

    Re: Exception thrown at 0x00000000 when using DLL.

    DLL project template.

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Exception thrown at 0x00000000 when using DLL.

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Oct 2017
    Posts
    50

    Re: Exception thrown at 0x00000000 when using DLL.

    Thank you so much 2kaud your help gave me hint I need to do some opengl initialization procedure for the DLL and now it's work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured