• Advertisement
Sign in to follow this  

Problem with glew.h

This topic is 938 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I try use glew. This my code

if (glewInit() == GLEW_NO_ERROR)
    {
      GLuint vertexbuffer;
      glGenBuffers(1, &vertexbuffer);
    }

and I get an error on glGenBuffers An unhandled exception of type 'System.AccessViolationException' occurred in ManagedLib.dll, in compiled my dll c++/cli

Share this post


Link to post
Share on other sites
Advertisement

I create context on event form_load 

GLManagedWrapper::GLManagedWrapper(INT_PTR hWnd)
	{

		if (!glewInit()) return;
		glewExperimental = GL_TRUE;


		m_Hwnd = (HWND)hWnd;
		if (!m_Hwnd) return;

		int m_Width, m_Height;
		LPRECT rect = (LPRECT)malloc(sizeof(RECT));
		if (GetWindowRect(m_Hwnd, rect))
		{
			m_Width = rect->right - rect->left;
			m_Height = rect->bottom - rect->top;
		}
		else return;

		free(rect);

		m_Hdc = GetDC(m_Hwnd);

		if (m_Hdc)
		{
			GLSetPixelFormat(m_Hdc);
			ReSizeGLScene(m_Width, m_Height);
			InitGL();
		}
	}

Share this post


Link to post
Share on other sites

I try minimal example and have error 

#include "TestGlew.h"

using namespace TestTest;

void Test1::Test()
{
	glewExperimental = GL_TRUE;
	glewInit();
	GLuint vertexBuffer;
	glGenBuffers(1, &vertexBuffer);
	printf("%u\n", vertexBuffer);
}

and C# call dll

var t = new Test1();
            t.Test();

Share this post


Link to post
Share on other sites

As you are using C# i assume your OS is Windows?

 

I had a similary error once, where everything worked fine until i tried to create the first buffer in OpenGL.

 

Unfortunately i don't remember the excact solution an have currently no acces to my code, but i believe it was some wrong header/header order.

Edited by mgubisch

Share this post


Link to post
Share on other sites

Yes, Windows 7

#define GLEW_STATIC
#include <glew.h>
#include <stdio.h>

namespace TestTest
{
	public ref class Test1
	{
	public:
		void Test();
	};
};

and include glew32.lib

Share this post


Link to post
Share on other sites

I've found some other code which words for me on windows: IIRC the crash was somewhere deep in the driver.

 

I included my headers the following order:

 

#include <windows.h>
#include <glew.h>
#include <wglew.h>

 

this is how i initialized glew and OpenGL

	m_hDC = GetDC(m_hWnd);
	m_hGlrc = wglCreateContext(m_hDC);
	wglMakeCurrent(m_hDC, m_hGlrc);
	glewInit();
        wglMakeCurrent(m_hDC, m_hGlrc);

Hope you can find there what you are missing

 

Share this post


Link to post
Share on other sites

Create the context before calling glewInit.  Your code:

GLManagedWrapper::GLManagedWrapper(INT_PTR hWnd)
	{

		if (!glewInit()) return;
		glewExperimental = GL_TRUE;


		m_Hwnd = (HWND)hWnd;
		if (!m_Hwnd) return;

		int m_Width, m_Height;
		LPRECT rect = (LPRECT)malloc(sizeof(RECT));
		if (GetWindowRect(m_Hwnd, rect))
		{
			m_Width = rect->right - rect->left;
			m_Height = rect->bottom - rect->top;
		}
		else return;

		free(rect);

		m_Hdc = GetDC(m_Hwnd);

		if (m_Hdc)
		{
			GLSetPixelFormat(m_Hdc);
			ReSizeGLScene(m_Width, m_Height);
			InitGL();
		}
	}

This will never work because you're calling glewInit before you create your context.  You need to do it the other way around - move your glewInit call to after your InitGL call and it will work.  You also need to set glewExperimental before calling glewInit too.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement