Click to See Complete Forum and Search --> : Unable to Create Window


umarbzu
November 6th, 2003, 01:56 AM
Hi !!!
I am unable to create the window getting NULL in handle of window but when i tried to get the las error it give me zero error code plzzzzzzz help me
Here is the code
#include "windows.h"
#include "stdio.h"
//PROTOTYPES
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, LPSTR lpCmdLine, int nShowCmd);
ATOM InitApplication(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);


//EXECUTION POINT
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, LPSTR lpCmdLine, int nShowCmd)
{
if(!InitApplication(hInstance))
{
MessageBox(NULL,"Unable to register the window class","Registeration Error",MB_OK);
return (0);
}

HWND hTestWnd = CreateWindow("TEST",
"Test Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL
);

if(!hTestWnd)
{
DWORD Error = GetLastError();
char temp[50] = "";
sprintf(temp,"The Error Number is %d",Error);
MessageBox(NULL,temp,"Registeration Error",MB_OK);
return (0);
}


MSG msg;
// message loop
while(GetMessage(&msg,NULL,0,0))
{
DispatchMessage(&msg);
}

return (0);
}

ATOM InitApplication(HINSTANCE hInstance)
{
WNDCLASSEX wndcls;

wndcls.cbClsExtra = 0;
wndcls.cbSize = sizeof(wndcls);
wndcls.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor = LoadCursor(hInstance,IDC_ARROW);
wndcls.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
wndcls.hIconSm = NULL;
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = WndProc;
wndcls.lpszClassName = "TEST";
wndcls.lpszMenuName = NULL;
wndcls.style = 0;

return (RegisterClassEx(&wndcls));

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
DefWindowProc(hWnd,Message,wParam,lParam);
return (0);
}

OriginalDazed
December 4th, 2003, 01:12 AM
The problem is in your "WndProc" function... change it to...


LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
return (DefWindowProc(hWnd,Message,wParam,lParam));
}

And it will run without an error, you still need to show the window and do all that other stuff...

The Original -=[DazeD]=-

TSmooth
December 4th, 2003, 08:14 AM
Also, I believe this line:
wndcls.cbSize = sizeof(wndcls);

needs to be:
wndcls.cbSize = sizeof(WNDCLASSEX);

OriginalDazed
December 4th, 2003, 01:06 PM
*cough* Actually....

Sam Hobbs
December 4th, 2003, 09:28 PM
Originally posted by TSmooth
Also, I believe this line:
wndcls.cbSize = sizeof(wndcls);

needs to be:
wndcls.cbSize = sizeof(WNDCLASSEX); Why?

TSmooth
December 5th, 2003, 12:02 AM
Was thinking wndcls was a pointer when I said that, sorry about that. Either one works.