   Win32 Wrapper window handle
  I am trying to create a simple wrapper class. The window is created but the window handle always fails and when using debugger is indicated as unusedCXX0030: Error: expression cannot be evaluated-gzWindow0xcccccccc {unused=??? }HWND__ *#ifndef GZ_WINDOW#define GZ_WINDOW#include <Windows.h>class GzWindow{protected:WNDCLASS gzWndClass;HWND gzWindow;MSG gzMessage;private:public:GzWindow(void);~GzWindow(void);bool Create(LPSTR Title,int Width=640,int Height=480);static LRESULT CALLBACK WndProc(HWND hWindow,UINT iMessage, WPARAM wParam, LPARAM lParam);void Run(void);virtual bool MessageHandler(UINT iMessage, WPARAM wParam, LPARAM lParam);virtual bool Update(void)=0;virtual bool Initate(void)=0;virtual bool Release(void)=0;};#endif#include "GzWindow.h"GzWindow::GzWindow(void){}GzWindow::~GzWindow(void){}bool GzWindow::Create(LPSTR Title,int Width,int Height){gzWndClass.hCursor = LoadCursor (NULL, IDC_ARROW);gzWndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);gzWndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);gzWndClass.hInstance = GetModuleHandle(NULL);gzWndClass.lpfnWndProc = WndProc;gzWndClass.lpszClassName = "01 Basic Window";gzWndClass.lpszMenuName = NULL;gzWndClass.cbClsExtra = NULL;gzWndClass.cbWndExtra = NULL;gzWndClass.style = NULL;if (!RegisterClass (&gzWndClass)){    MessageBox(NULL,"FAILED TO REGISTER WINDOW CLASS","ERROR",MB_OK);return false;}if(gzWindow = CreateWindow ("01 Basic Window", Title,WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,CW_USEDEFAULT, Width, Height,NULL, NULL, GetModuleHandle(NULL), (void*) this)){MessageBox(NULL,"FAILED TO CREATE WINDOW","ERROR",MB_OK);return false;}ShowWindow(gzWindow,SW_SHOW);return true;}LRESULT CALLBACK GzWindow::WndProc(HWND hWindow, UINT iMessage, WPARAM wParam, LPARAM lParam){GzWindow * pkWindow = NULL;bool bProcessed = false;switch (iMessage){case WM_NCCREATE:SetWindowLong (hWindow, GWL_USERDATA,(long)((LPCREATESTRUCT(lParam))->lpCreateParams));break;default:pkWindow = (GzWindow *) GetWindowLong (hWindow, GWL_USERDATA);if (NULL != pkWindow){bProcessed = pkWindow->MessageHandler (iMessage, wParam, lParam);} break;}if (false == bProcessed){return DefWindowProc (hWindow, iMessage, wParam, lParam);}return 0;}void GzWindow::Run(void){Initate();while(1){if (PeekMessage (&gzMessage, gzWindow, 0, 0, PM_REMOVE)){if (WM_QUIT == gzMessage.message){break;}else{TranslateMessage (&gzMessage);DispatchMessage (&gzMessage);}}else{Update();}}Release();}bool GzWindow::MessageHandler (UINT iMessage, WPARAM wParam, LPARAM lParam){switch (iMessage){case WM_CLOSE:PostQuitMessage (0);return true;break;default:return false;break;}}and the main file is#include <iostream>#include "GzWindow.h"using namespace std;class CustomWindow: public GzWindow{public: CustomWindow(void){};~CustomWindow(void){};bool Update(void){return true;};bool Initate(void){return true;};bool Release(void){return true;};};int main(int argc, char* argv[]){CustomWindow win;win.Create("Test");win.Run();return 0;}I need the window handle to be valid as I intend to extend the window class into and opengl window class and I cant get the rendering context without the window handle. Any ideas that dont point to using the wrapper class tutorial already on this site.   
  You're using a main() entry point instead of a WinMain as Windows requires. WinMain receives the HINSTANCE handle for your application that you need to use to work with windows (eg: to register your Window class and so on)EDIT: Just noticed you're calling GetModuleHandle, passing NULL - you should be fine on that - my bad[Edited by - evolutional on May 24, 2009 6:45:05 PM];  I see a couple of problems. The first is that your conditional check for the Window creation is not quite right. CreateWindow returns a HWND and if that HWND is non-NULL, then it is a successful creation, not a failure. if(gzWindow = CreateWindow ("01 Basic Window", Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, Width, Height, NULL, NULL, GetModuleHandle(NULL), (void*) this)){MessageBox(NULL,"FAILED TO CREATE WINDOW","ERROR",MB_OK);return false;}should be:gzWindow = CreateWindow ("01 Basic Window", Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, Width, Height, NULL, NULL, GetModuleHandle(NULL), (void*) this);if(gzWindow == NULL){   MessageBox(NULL,"FAILED TO CREATE WINDOW","ERROR",MB_OK);   return false;}Second, your WM_NCCREATE message handler is also incorrect. If you read the MSDN page for that message, you will see:Quote:    If an application processes this message, it should return TRUE to continue creation of the window. If the application returns FALSE, the CreateWindow or CreateWindowEx function will return a NULL handle. Right now, when you return 0, it will make the CreateWindow function fail and return 0. I would suggest double checking all your code with MSDN documentation if you run into anything else. I know it can be boring or tedious since there is so much information, but in doing so you will be able to easily identify your problems because 99% of the time MSDN has the techniical information you need to succeede in Win32 programming.Good luck!   ;Quote:Original post by evolutionalYou're using a main() entry point instead of a WinMain as Windows requires. WinMain receives the HINSTANCE handle for your application that you need to use to work with windows (eg: to register your Window class and so on)That's OK, using main() instead of WinMain() implies he's creating a console application (which can still create windows, obviously). GetModuleHandle(NULL) returns the module handle of the executable, which is the same as the HINSTANCE passed into WinMain.As Drew_Benton says, the problem is returning 0 from WM_NCCREATE.   ;Quote:Original post by CodekaQuote:Original post by evolutionalYou're using a main() entry point instead of a WinMain as Windows requires. WinMain receives the HINSTANCE handle for your application that you need to use to work with windows (eg: to register your Window class and so on)That's OK, using main() instead of WinMain() implies he's creating a console application (which can still create windows, obviously). GetModuleHandle(NULL) returns the module handle of the executable, which is the same as the HINSTANCE passed into WinMain.As Drew_Benton says, the problem is returning 0 from WM_NCCREATE.But, hes not returning 0 from a NCCREATE, he's modifying the struct with SetWindowLong then sending the processing to the DefWinProc; hes controlling it with the bProcessed bool, whose default thing to do is to return True when processing NCCREATE. It maybe that the modification to the struct is causing DefWinProc to not return true. Try not setting 32-bit and see what happens.   ;  The code works fine for me if I change the CreateWindow line to:gzWindow = CreateWindow ("01 Basic Window", Title,WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT,CW_USEDEFAULT, Width, Height,NULL, NULL, GetModuleHandle(NULL), (void*) this);if(!gzWindow){MessageBox(NULL,"FAILED TO CREATE WINDOW","ERROR",MB_OK);return false;}Although there's no message handler for WM_CLOSE (normally DestroyWindow()).   ;Quote:Original post by RIZAXBut, hes not returning 0 from a NCCREATE, he's modifying the struct with SetWindowLong then sending the processing to the DefWinProc; hes controlling it with the bProcessed bool, whose default thing to do is to return True when processing NCCREATE.Ah yes, you're right...   ;  Hi. Thanks for all the quick replies I have tried everything you suggested but the window handle still always ends up asunused CXX0030: Error: expression cannot be evaluated- gzWindow 0xcccccccc {unused=??? } HWND__ *which stops me getting the DC properly for setting up with opengl. The window is created fine as I said before but the handle is never set.   ;  At what point is that happening though? You're obviously debugging the application, what line is your cursor on when you see that?   ;  Straight after the window creation line or inside the WndProc all them when debugging give that error for the window handle.   