Advertisement

CreateWindowEX() failing...Why?

Started by June 15, 2002 06:51 PM
16 comments, last by MrBeaner 22 years, 5 months ago
Your WndProc looks fine - have you got it prototyped or declared in scope of the WndClass?

Check the return value from RegisterClassEx. Use sark''s snippet to format the error message or pass it along to the func I laid out. Let windows tell you what the errno means rather than guess at it yourself.

Here''s the full listing of the adaptation that I was able to get running.


  #include <windows.h>VOID ReportError(DWORD errno){	LPVOID lpMsgBuf;	DWORD msglen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM		    , NULL		    , errno		    , MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)		    , (LPTSTR) &lpMsgBuf		    , 0, NULL);	if ( msglen > 0 ) {		MessageBox(GetActiveWindow(), lpMsgBuf, TEXT("Error Report"), MB_OK + MB_ICONEXCLAMATION);	}	LocalFree( lpMsgBuf );}HINSTANCE g_hInstance;TCHAR g_cAppName[] = TEXT("mrbeaner");LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){	if (msg == WM_DESTROY)	{		PostQuitMessage(0);		return 0;	}	return DefWindowProc(hwnd, msg, wParam, lParam);}int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrvInst,LPSTR lpCmdLine,int nShowCmd){	WNDCLASSEX wc;	g_hInstance = hInstance;	MSG Msg;	HWND hWnd;	wc.cbSize=sizeof(wc);	wc.cbClsExtra = 0;	//NULL;	wc.cbWndExtra= 0; // NULL;	wc.hbrBackground=(HBRUSH)GetStockObject(DKGRAY_BRUSH);	wc.hCursor=LoadCursor(NULL,IDC_ARROW);	wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);	wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);	wc.hInstance = hInstance;	wc.lpfnWndProc = WndProc;	wc.lpszClassName = "DX";	wc.lpszMenuName = 0;	wc.style=CS_VREDRAW | CS_HREDRAW;	RegisterClassEx(&wc);	hWnd = CreateWindowEx(0L, //NULL,							wc.lpszClassName,						//This is the class name of the Widnows class							g_cAppName,								//This is The captoin of teh window							WS_OVERLAPPEDWINDOW,					//These are style flags							CW_USEDEFAULT,							CW_USEDEFAULT,							//Initial Startup position							512,							512,									//The actual Window "Size"							NULL,									//Handle to the parent window							NULL,									//Handle to a amenu							hInstance,								//The handle to the instance of our app							NULL									//Nothing.							);	if ( hWnd == NULL ) {		ReportError(GetLastError());		PostQuitMessage(0); // to keep with the original	}	ShowWindow(hWnd, nShowCmd);	UpdateWindow(hWnd);	while ( GetMessage (&Msg, NULL, 0, 0) )	{				TranslateMessage(&Msg);				DispatchMessage(&Msg);	}	return Msg.wParam ;}  

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Yes, all of my functions are in scope. My habit is to write the functions before i use them, as i don''t understand why you would need to make function prototypes when you could just as easily just write the code there. (i understand the use for them, but in smaller projects i just code the function definition before anything else.)

OK, thanks, it tells me that i have an Invalid Window Handle. Didn''t i get this number from Windows?

Thanks for the code snippit, that is really helpful!
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
Advertisement
Try returning the value DefWndProc returns.
---visit #directxdev on afternet <- not just for directx, despite the name
try changing the first null in CreateWindowEX to WS_EX_OVERLAPPEDWINDOW;



-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."

-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."
The DefWindowProc returns a "Operation COmpleted Successfully".

Adding WS_EX_OVERLAPPED to the extended styles didn''t change the outcome.

This is weird.. The Hwnd that Windows passes into the WndProc()is valid. If i create a g_hWnd1 and assign it a value from the WndProc, then test it in the WinMain, it doesn''t return as an invalid handle. But if i use the Hwnd returned to me from CreateWindowEx, it is invalid. WTF?

IS there a patch for C++?
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
When I plugged your WndProc in I got an error - "The handle is invalid" - and combined with what you've said - it looks as if your WndProc is the culprit - in fact I think I've spotted it.

Change
        DefWindowProc(hWnd,Msg,wParam,lParam);	return 0;  


to

        return DefWindowProc(hWnd,Msg,wParam,lParam);  


and see if that makes a difference.

And that ValidateRect call from WM_PAINT makes my debugger jiggle...
It's really not necessary.

[edited by - lessbread on June 15, 2002 11:52:42 PM]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Advertisement
Good stuff!! Thanks!!! That ABsolutly fixed it! I looked over the code over and over again, and it blows my mind that i missed it every time!

LessBread, you are MoreBread in my book
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
Cool! I''m glad it worked. It had me baffled there for a while too.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement