Advertisement

CreateWindowEX() failing...Why?

Started by June 15, 2002 06:51 PM
16 comments, last by MrBeaner 22 years, 3 months ago
Hello- I am trying to obtain a handle to a window, but the CreateWindowEx() function keeps failing and returning NULL. Below is the source:
  
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrvInst,LPSTR lpCmdLine,int nShowCmd)
{

	WNDCLASSEX wc;
	g_hInstance = hInstance;
	MSG Msg;
	HWND hWnd;
	

	wc.cbClsExtra = NULL;
	wc.cbSize=sizeof(wc);
	wc.cbWndExtra=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(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)
	{
		MessageBox(NULL,"Did not generate a handle to a window!","ERROR",MB_OK);
		PostQuitMessage(0);
	}
  
I have no idea what could be wrong. I have been thinking about it for the past 4 hours, and have looked for info where i could. It doesn''t return any comile errors or linking errors.. i''m just not getting that number, and i think right now it is the reason why i can''t create a D3D Device Thanks in advance!
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
wc.cbSize = sizeof(WNDCLASSEX);

Also check the return value of RegisterClassEx.
Advertisement
Use GetLastError() and FormatMessage() to get a description of the error set by CreateWindowEx():


    if(hWnd == NULL){  char* buffer;  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, GetLastError(), 0, buffer, 0, NULL);  MessageBox(NULL, buffer, "Error", MB_OK);  LocalFree(buffer);}    


And dont use PostQuitMessage(0) there - remember there isnt a window message pump to send it to!

[edited by - sark on June 15, 2002 8:05:50 PM]
You might try passing 0L as the extended style parameter instead of NULL and you might also try using a string literal for the classname parameter rather than using the member from the wndclass struct.

You might also want to call GetLastError and plug the return value into this function


  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 );}// use like thishWnd = CreateWindowEx(...);if ( hWnd == NULL )    ReportError(GetLastError());  
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
I compiled MrBeaner''s code virtually verbatim into a WinMain after setting up a wndproc and such and CreateWindowEx does not return NULL (it creates a window fine.) The only thing I changed was the g_cAppname parameter to CreateWindowEx (I replaced it with a string literal.) This is on Win2k with a fairly standard, current VC6 install. Perhaps g_cAppName is NULL?
I checked the last error, and it told me that it had "(null) failed GetLastError returned 0". I also changed the extended parameters to 0L, did nothing.

The only reason why i am sending a PostQuitMessage here is because it never shows the window, so the only way i can break out of it cleanly is by sending the Message loop a quit message. Makes sense i think, and besides it''s only temporary.

Here is the code as it stands after making the corrections.

  int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrvInst,LPSTR lpCmdLine,int nShowCmd){	WNDCLASSEX wc;	g_hInstance = hInstance;	MSG Msg;	HWND hWnd;		wc.cbClsExtra = NULL;	wc.cbSize=sizeof(WNDCLASSEX);	wc.cbWndExtra=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(NULL,							wc.lpszClassName,						//This is the class name of the Widnows class							g_cAppName,								//This is The captoin of teh window							WS_OVERLAPPEDWINDOW| WS_POPUP,					//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							0L									//Nothing.							);	if(hWnd == NULL)	{		MessageBox(NULL,"Did not generate a handle to a window!","ERROR",MB_OK);						LPSTR lpszFunction;		CHAR szBuf[80]; 		DWORD dw = GetLastError();  		sprintf(szBuf, "%s failed: GetLastError returned %u\n",         hWnd, dw);  		MessageBox(NULL, szBuf, "Error", MB_OK); 		ExitProcess(dw); 				PostQuitMessage(0);	}  


I have no idea. Could the fact that i am using XP have anything to do with it?
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
Advertisement
quote: Original post by SHilbert
I compiled MrBeaner''s code virtually verbatim into a WinMain after setting up a wndproc and such and CreateWindowEx does not return NULL (it creates a window fine.) The only thing I changed was the g_cAppname parameter to CreateWindowEx (I replaced it with a string literal.) This is on Win2k with a fairly standard, current VC6 install. Perhaps g_cAppName is NULL?


No, i watched the value in the debug window and it holds a value, and i am also specifing the wc.lpszClassName in the argument list. (i wasn''t doing this before, i changed it to make sure that it wasn''t setting my char const to NULL).

This sucks. I need the handle for other functions (like getting a D3D Device) and i am getting all these errors because of this one problem arhh...
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
The predefined variable ''err'' will show GetLastError value when you add it to watch window. Look up that value in winerror.h for a message string.
---visit #directxdev on afternet <- not just for directx, despite the name
I got the code to work just fine too - I''m using straight C - so I had to change some of the NULL references to 0

	wc.cbClsExtra = 0;	//NULL;	wc.cbWndExtra= 0; // NULL;	hWnd = CreateWindowEx(0L, //NULL, 


And supply a few variables

HINSTANCE g_hInstance;TCHAR g_cAppName[] = TEXT("mrbeaner"); 


And a WndProc and message loop - and the window manifests normally. So, like SHilbert suggested - maybe g_cAppName is NULL - or maybe something is wrong with the way you''ve declared your WndProc? I used a barebones one just to get off the ground.

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);} 



"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Well, it says that the err variable is at 2 as soon as the CreateWIndowEX executes. I don''t know what that means. If i go by 2L, then it is a "ERROR_FILE_NOT_FOUND" This is the closest match i could find. SHould i do something with this number, or should i be able to find a direct match?

I also hard coded the string names in. Currently the class name and CreateWIndowEx parameter are both "DX" with the old g_cAppName being commented out.

Here is the source for my WndProc:

  long CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam,LPARAM lParam){	switch (Msg)	{	case WM_CREATE:		{			return 0;		}	case WM_PAINT:		{			ValidateRect(g_hWnd,NULL);			return 0;		}	case WM_DESTROY:		{			PostQuitMessage(0);			return 0;		}	default:		{			DefWindowProc(hWnd,Msg,wParam,lParam);			return 0;		}	}	return 0;}  


Thanks for all of the help, i really appretiate it. I hope i can fix this so i can move on!
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.

This topic is closed to new replies.

Advertisement