Why does this go wrong?

Started by
4 comments, last by trysil 22 years, 2 months ago
Becoming a real nag, aren''t I? I have done some debugging of my WIN32 code and narrowed down my problem (Can''t create a rendering context) to these lines: As the constructor of my class is called I down the line call the CreateWindowEx operation wich creates an event caught by the winproc function registered with my WNDCLASS.
  
hWnd = CreateWindowEx(NULL, "OpenGL", name, dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
						  0, 0, width, height, NULL, NULL, (HINSTANCE)GetModuleHandle(NULL),(void*)this);
  
In my winproc I to begin with creatig an instance of my window class and since the WM_CREATE event has been cast I begin with:
  
case WM_CREATE:	
		{
			HINSTANCE hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
			glWindow = (teGLWindow*)(((LPCREATESTRUCT)lParam)->lpCreateParams);

			SetWindowLong(hWnd, GWL_USERDATA, (LONG)glWindow);
			glWindow->hWnd = hWnd;
			return glWindow->createGLWindow();
}
  
It goes wrong in the SetWindowLong call. I suspect that I for some reason get the "wrong" indata to the winproc. I''ve done a walkthrough of the code, compared it to a working implementation and the only difference is the fact that the winproc is declared a friend to the window class. the window class is a superclass inherited by the engine class that holds the main message loop. "A witty and slightly sarcastic quote from an unkown source" -- The generic SIG /trysil
"A witty and slightly sarcastic quote from an unkown source"-- The generic SIG/trysil
Advertisement
c++ 101, remember the hidden this pointer passed to all memeber functions of a class? this is so you can access the ''local'' (to the class) variables for this particular class.
Are you sure that the problem is in the SetWindowLong call? If SetWindowLong returns 0 it doesn''t mean that there was an error. It just returns the previous value of atrribute it changes (at least on my computer it always returns 0 when first called).

Also, do you know that WM_CREATE is not the first message window procedure receives. Make sure that you do not read the GWL_USERDATA value before it is set.
Well, I just found your source code in your previous post and I think I can see the problem. Try to replace
if(!SetWindowLong(hWnd, GWL_USERDATA, (LONG)glWindow)){  DWORD a = GetLastError();  return a;} 

with
SetLastError(0);if(!SetWindowLong(hWnd, GWL_USERDATA, (LONG)glWindow)){  DWORD a = GetLastError();  if(a)  {    // There actually is an error    // Handle it    return 0;  }  // No, that wasn't an error} 




Edited by - Advanced Bug on February 8, 2002 4:38:21 AM
I looked through the error codes for the different system calls I make and found I
few errors.

i.e. one realy stuuupid one: I forgot to clear the depth and color buffer and set the background
color.





Edited by - trysil on February 8, 2002 8:30:55 AM
"A witty and slightly sarcastic quote from an unkown source"-- The generic SIG/trysil
That''s a very good point that was made. This is what MSDN says about GWL_USERDATA: Sets the user data associated with the window. This data is intended for use by the application that created the window. Its value is initially zero. Therefore your call to SetWindowLong will ALWAYS return zero whether there is an error or not. There''s literally no point in checking the return value.

Do what Advanced Bug suggests, except don''t even check the return value of SetWindowLong.
SetLastError( 0 );SetWindowLong( hWnd, GWL_USERDATA, (long)glWindow );if( a = GetLastError() ){    You get the idea.} 

Personally I cannot imagine why this would ever fail, since it''s just user data and Windows can care less what it is. If hWnd is valid, it will work.

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement