Skip to main content
gamedev.net
🔒 Locked

Router Problem

Started by Premonition 2 replies 300+ views
Original Post
Premonition
Premonition
Hello everyone. I know that I have posted a lot in the last day or so but I am totally stumped... I wrote a windows router/handler for a windows wrapper class and have run into a few errors (most of which are minor). The major problem is here:
p_wnd = reinterpret_cast<CWND*>(LPCREATESTRUCT(lParam)->lpCreateParams);
    instance = LPCREATESTRUCT(lParam)->hInstance;

    SetWindowLong(hwnd, GWL_USERDATA, reinterpret_cast<long>(p_wnd));
 
The lpCreateParams member of the LPCREATESTRUCT is always zero and thus the SetWindowLong function fails and returns zero as well (even after the error code is reset). I am not clear if I have to pass a pointer in the CreateWindowEx function that allows use of this member... but here is my window creation code:
void CWND::CreateWND(int showCmd)
{
  DWORD		dwExStyle;		// Window Extended Style

	DWORD		dwStyle;			// Window Style

	
	HWND hwnd;
  
  // Setup the windows class

  m_wndClass.cbSize = sizeof(WNDCLASSEX);
  m_wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  m_wndClass.lpfnWndProc = CWND::MessageRouter; // window procedure

  m_wndClass.cbClsExtra = 0;
  m_wndClass.cbWndExtra = 0;
  m_wndClass.hInstance = m_instance;
  m_wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);  // default app icon

  m_wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);    // default cursor

  m_wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);; // windows default black

  m_wndClass.lpszClassName = m_className; // class name

  m_wndClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); 
  
  RECT windowRect;							// Grabs Rectangle Upper Left / Lower Right Values

	windowRect.left = (long)0;						// Set Left Value To 0

	windowRect.right = (long)m_width;						// Set Right Value To Requested Width

	windowRect.top = (long)0;							// Set Top Value To 0

	windowRect.bottom = (long)m_height;						// Set Bottom Value To Requested Height  

  
  // register the class

  if (!RegisterClassEx(&m_wndClass))
        { MessageBox(NULL, "Class Registration Failed!", "ERROR!", MB_OK | MB_ICONEXCLAMATION); }
  
  if (m_fullscreen)								// Attempt Fullscreen Mode?

	{
    DEVMODE dmScreenSettings;					// Device Mode

		memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));		// Makes Sure Memory's Cleared

		dmScreenSettings.dmSize = sizeof(dmScreenSettings);		// Size Of The Devmode Structure

		dmScreenSettings.dmPelsWidth	= m_width;			// Selected Screen Width

		dmScreenSettings.dmPelsHeight	= m_height;			// Selected Screen Height

		dmScreenSettings.dmBitsPerPel	= m_bpp;		 		  // Selected Bits Per Pixel

		dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
		
		// Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.

		if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
		{
		  // If The Mode Fails, Offer Two Options.  Quit Or Run In A Window.

			if (MessageBox(NULL, "The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Using windowed mode instead.",
          "ERROR", MB_OK | MB_ICONEXCLAMATION) == IDOK)
			{
			  	m_fullscreen = false;
			}
		}
	}
	
	if (m_fullscreen)								// Are We Still In Fullscreen Mode?

	{
	  dwExStyle = WS_EX_APPWINDOW;	// Window Extended Style

		dwStyle = WS_POPUP;						// Windows Style

	}
	else
	{
    dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;			// Window Extended Style

		dwStyle = WS_OVERLAPPEDWINDOW;					// Windows Style

	}
	
	// Adjust Window To True Requested Size

	AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);

  // Create the window

  hwnd = CreateWindowEx(dwExStyle,     // Extended Style

             m_className,            
             m_className,             
             dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
             0,                          // X

             0,                          // Y

             m_width,                    // Height

             m_height,                   // Width

             NULL,                       // Handle to parent

             NULL,                       // Handle to menu

             m_instance,                // Application Instance

             NULL);                      // No Extra Params

  
  // Check to see if window creation failed

  if(!hwnd)
      { MessageBox(NULL, "Window Creation Failed!", "ERROR!", MB_OK | MB_ICONEXCLAMATION); }
  
  // Show and update the window

  ShowWindow(hwnd, showCmd);
  UpdateWindow(hwnd);
}
 
And here is the router in its entirity:
// Class message router: this function is static and will rout messages

// to the correct class instance

LRESULT CALLBACK CWND::MessageRouter(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  CWND *p_wnd = 0;
  HINSTANCE instance;

  if(msg == WM_NCCREATE)
  {
    // retrieve Window instance from window creation data and associate

    p_wnd = reinterpret_cast<CWND*>(LPCREATESTRUCT(lParam)->lpCreateParams);
    instance = LPCREATESTRUCT(lParam)->hInstance;

    SetWindowLong(hwnd, GWL_USERDATA, reinterpret_cast<long>(p_wnd));

	  // save window handle

    if(p_wnd)
    {
      p_wnd->SetHWND(hwnd);
      p_wnd->SetInstance(instance);
    }
  }
  else
  {
    // retrieve associated Window instance

    p_wnd = reinterpret_cast<CWND*>(GetWindowLong(hwnd, GWL_USERDATA));
  }
  
  if(p_wnd)
  {
    t_messageIterator i;
    
    // retrieve the associated message handler

    i = p_wnd->GetMessageHandler(msg);
    return (i->second)(p_wnd, hwnd, wParam, lParam); 
  }
    
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
 
It is imperative that I get this router up and running and I would appreciate any help that someone can offer... [edited by - Premonition on April 26, 2004 8:15:55 PM]
SiCrane
SiCrane
Here''s a hint: from your thread title, I thought the thread would be about networking and skipped it the first few times. Probably a few other people did the same.

As for you questions, did you try passing the object pointer as the last parameter for CreateWindowEx()?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.