I have an unusual situation whereby we create a Window during initialisation. We have used this code for many years and have found one unusual case with a customer when the RegiterClass fails and then the following CreateWindow also fails, making our process timeout and fail.
My real problem is trying to understand what is causing the RegisterClass to fail and I believe that this function does not set the last error so GetLastError() either reports OK or the error to a previous call (not ReisterClass call).
My feeling is that I am running out of system resources, but I cannot prove this unless I get sensible error returned from RegisterClass(). Below is my code and also tracing output when the process is running.
Does anyone have any suggestions how I may find out what is the underlying cause here, or why RegisterClass fails?
Expand|Select|Wrap|Line Numbers
- HWND CreateWindow( void )
- {
- WNDCLASSEX tWc; /* Windows Class structure */
- LPTSTR lpMsgBuf; /* Windows O/S Error Buffer */
- PCXAChar sClassName = "WndProc";
- DWORD dError;
- HMODULE tModule = GetModuleHandle(NULL);
- /* Only need to create a single Event window no matter how many sockets */
- if ( tcp_g.hWSA_IO_comp_window != NULL )
- {
- return tcp_g.hWSA_IO_comp_window;
- }
- if (!GetClassInfoEx(tModule, sClassName, &tWc))
- {
- dError = GetLastError();
- /* Get error Details */
- FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- dError,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
- (LPTSTR)&lpMsgBuf,
- 0,
- NULL );
- /* display socket stats */
- TRACE("CcreateWindow - GetClassInfoEx Error %s \n", lpMsgBuf);
- /* Free the buffer */
- LocalFree( lpMsgBuf );
- /* Initialize Window Class structure to describe Window. */
- tWc.cbSize = sizeof(WNDCLASSEX);
- tWc.style = 0; /* Class style - Local */
- tWc.cbClsExtra = 0; /* Class extra data. */
- tWc.cbWndExtra = 0; /* Window extra data. */
- tWc.hCursor = NULL;
- tWc.hbrBackground = NULL;
- tWc.lpszMenuName = NULL;
- tWc.hIcon = NULL;
- tWc.hIconSm = NULL;
- tWc.hInstance = tModule;
- tWc.lpszClassName = sClassName; /* Window Class Name */
- tWc.lpfnWndProc = (WNDPROC)WndProc_WIN;
- /* windows of this class. */
- /* Register the window class and get Class Atom Id */
- if(!RegisterClassEx(&tWc))
- {
- dError = GetLastError();
- /* Get error Details */
- FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- dError,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
- (LPTSTR)&lpMsgBuf,
- 0,
- NULL );
- /* display socket stats */
- TRACE( "CreateWindow - RegisterClassEx Error %s \n", lpMsgBuf);
- /* Free the buffer */
- LocalFree( lpMsgBuf );
- }
- }
- /* Create Winsock Event Window */
- tcp_g.hWSA_IO_comp_window = CreateWindowEx(
- 0, /* Extended window style */
- sClassName, /* Class Name */
- tSymbolicProcessId, /* Window title bar ( Use Symbolic ID) */
- WS_OVERLAPPEDWINDOW, /* Window style. */
- CW_USEDEFAULT, /* Default horizontal position. */
- CW_USEDEFAULT, /* Default vertical position. */
- CW_USEDEFAULT, /* Default width. */
- CW_USEDEFAULT, /* Default height. */
- NULL, /* Overlapped windows have no parent. */
- NULL, /* Use the window class menu. */
- tModule, /* Application instance owns this window */
- NULL /* Pointer to window creation Data */ );
- if ( tcp_g.hWSA_IO_comp_window == NULL )
- {
- dError = GetLastError();
- /* Get error Details */
- FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- dError,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
- (LPTSTR)&lpMsgBuf,
- 0,
- NULL );
- /* display socket stats */
- TRACE( "CreateWindow - CreateWindowEx Error %s \n", lpMsgBuf);
- /* Free the buffer */
- LocalFree( lpMsgBuf );
- }
- /* Return Window Handle */
- return tcp_g.hWSA_IO_comp_window;
- }
sample 1
--------
CreateWindow - RegisterClass Error The operation completed successfully.
CreateWindow - CreateWindow Error Cannot find window class.
sample 2
--------
CreateWindow - CreateWindowEx Error Not enough storage is available to complete this operation.
sample 3
--------
CreateWindow - RegisterClassEx Error Class already exists.
CreateWindow - CreateWindowEx Error Not enough storage is available to complete this operation.
So as you can see, the error messages and results don't make sense....
Thanks.