Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CreateWindow returns NULL.

316 views
Skip to first unread message

Brian

unread,
Apr 20, 2001, 2:32:10 AM4/20/01
to
Greetings,

Relatively new to Win32 from C++. I am looking for some insight on a
seemingly intractable issue regarding a CreateWindow call returning null
rather than a valid hwnd.

The winmain and window procedure are listed below. I seem to be getting
a valid class atom (correct term?) from the RegisterClass call. (BTW
this code is originally from Petzold's sample called Colors1.c) I have
begun adding debug code into it to see if I mis-keyed something or what
my deal is... After the call to CreateWindow I can break in the window
procedure and see that I am getting WM_GETMINMAXINFO, then WM_NCCREATE
and then WM_NCDESTROY.

I have looked at the CREATEINFO struct given in the WM_NCCREATE
message. It appears in order, has the valid hInstance, and the
positioning fields are CW_USEDEFAULT as they should be, menu and hwnd
are null as expected, the class name matches and the window title is
per the parameter... Then CreateWindow comes back with NULL...

My system resources seem to be dropping over time while debugging
-though I'm not sure how direct the correlation is. GetLastError always
returns zero. Does this have anything to do with running Win 98?

For curiosity sake I swapped in a different window procedure and I was
able to get a valid window created and painted. I guess I would like to
know what is the ABSOLUTE minimum a window procedure can process and
expect to create valid windows? I have this one stripped so that it
only calls DefWindowProc and returns the value now and still I get
null...

Looking for ideas on what to look for or try next.

TIA,

Brian Battah

//WINMAIN EXCERPT

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Colors1");
int errno;
int reg_result;
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
memset(&wndclass, 0,sizeof(wndclass));

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = MyWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = CreateSolidBrush(0);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;

if(!(reg_result = RegisterClass(&wndclass)))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
errno = GetLastError();

hwnd = CreateWindow(szAppName,TEXT("Color Scroll"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);

errno = GetLastError();
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(!GetMessage(&msg, NULL, 0, 0 ))
{
TranslateMessage (&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

//WINDOW PROCEDURE
LRESULT CALLBACK MyWndProc(HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam)
{
int i, cxClient, cyClient, ret;
CREATESTRUCT st;
memset(&st, 0,sizeof(st));
if(lParam != NULL)
st = *(LPCREATESTRUCT)lParam;

//... much code commented out for experimentation with seemingly no
effect on issue
ret = DefWindowProc( hwnd, message, lParam, wParam);
return ret ;
}

Brian

unread,
Apr 20, 2001, 3:06:10 AM4/20/01
to
Hmmm.

Never fails - go public with a question then notice the silly mistake.
In the code below there are two major problems. The first is that my
call to
DefWindowProc had the lParam and wParam parameters in the wrong order.
Presumably screwing up the result...

That fixed the window proc problem. The only other thing was that
somewhere along the line I added the ! character in front of the
GetMessage call in the message loop... Duh.

Works like a charm now. Thanks anyway, I had fun reading the 1200 posts
and scratching my head for 2 days. Hope I haven't embarrassed myself
too badly. Only one way to learn I suppose :)

Brian

0 new messages