By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,612 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,612 IT Pros & Developers. It's quick & easy.

Problem with Win32 RegisterClass() runtime call

P: 1
Hi,

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
  1. HWND CreateWindow( void )
  2. {
  3.    WNDCLASSEX        tWc;            /* Windows Class structure */
  4.    LPTSTR            lpMsgBuf;       /* Windows O/S Error Buffer */
  5.    PCXAChar          sClassName = "WndProc";
  6.    DWORD             dError;
  7.    HMODULE           tModule = GetModuleHandle(NULL);
  8.  
  9.    /* Only need to create a single Event window no matter how many sockets */
  10.    if ( tcp_g.hWSA_IO_comp_window != NULL )
  11.    {
  12.       return tcp_g.hWSA_IO_comp_window;
  13.    }
  14.  
  15.    if (!GetClassInfoEx(tModule, sClassName, &tWc))
  16.    {
  17.       dError = GetLastError();
  18.  
  19.       /* Get error Details */
  20.       FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  21.          NULL,
  22.          dError,
  23.          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
  24.          (LPTSTR)&lpMsgBuf,
  25.          0,
  26.          NULL );
  27.  
  28.       /* display socket stats */
  29.       TRACE("CcreateWindow - GetClassInfoEx Error %s \n", lpMsgBuf);
  30.  
  31.       /* Free the buffer */
  32.       LocalFree( lpMsgBuf );
  33.  
  34.       /* Initialize Window Class structure to describe Window. */
  35.       tWc.cbSize        = sizeof(WNDCLASSEX);
  36.       tWc.style         = 0;         /* Class style - Local */
  37.       tWc.cbClsExtra    = 0;         /* Class extra data.   */
  38.       tWc.cbWndExtra    = 0;         /* Window extra data.  */
  39.       tWc.hCursor       = NULL;
  40.       tWc.hbrBackground = NULL;
  41.       tWc.lpszMenuName  = NULL;
  42.       tWc.hIcon         = NULL;
  43.       tWc.hIconSm       = NULL;
  44.       tWc.hInstance     = tModule;
  45.       tWc.lpszClassName = sClassName;          /* Window Class Name    */
  46.       tWc.lpfnWndProc   = (WNDPROC)WndProc_WIN;
  47.  
  48.       /* windows of this class. */
  49.       /* Register the window class and get Class Atom Id */
  50.       if(!RegisterClassEx(&tWc))
  51.       {
  52.          dError = GetLastError();
  53.  
  54.          /* Get error Details */
  55.          FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  56.             NULL,
  57.             dError,
  58.             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
  59.             (LPTSTR)&lpMsgBuf,
  60.             0,
  61.             NULL );
  62.  
  63.          /* display socket stats */
  64.          TRACE( "CreateWindow - RegisterClassEx Error %s \n", lpMsgBuf);
  65.  
  66.          /* Free the buffer */
  67.          LocalFree( lpMsgBuf );
  68.       }
  69.    }
  70.  
  71.    /* Create Winsock Event Window */
  72.    tcp_g.hWSA_IO_comp_window = CreateWindowEx( 
  73.       0,                                         /* Extended window style */
  74.       sClassName,                                /* Class Name */
  75.       tSymbolicProcessId,                        /* Window title bar ( Use Symbolic ID)   */
  76.       WS_OVERLAPPEDWINDOW,                       /* Window style.                         */
  77.       CW_USEDEFAULT,                             /* Default horizontal position.          */
  78.       CW_USEDEFAULT,                             /* Default vertical position.            */
  79.       CW_USEDEFAULT,                             /* Default width.                        */
  80.       CW_USEDEFAULT,                             /* Default height.                       */
  81.       NULL,                                      /* Overlapped windows have no parent.    */
  82.       NULL,                                      /* Use the window class menu.            */
  83.       tModule,                                   /* Application instance owns this window */
  84.       NULL                                       /* Pointer to window creation Data       */ );
  85.  
  86.    if ( tcp_g.hWSA_IO_comp_window == NULL )
  87.    {
  88.       dError = GetLastError();
  89.  
  90.       /* Get error Details */
  91.       FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  92.          NULL,
  93.          dError,
  94.          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
  95.          (LPTSTR)&lpMsgBuf,
  96.          0,
  97.          NULL );
  98.  
  99.       /* display socket stats */
  100.       TRACE( "CreateWindow - CreateWindowEx Error %s \n", lpMsgBuf);
  101.  
  102.       /* Free the buffer */
  103.       LocalFree( lpMsgBuf );
  104.    }
  105.  
  106.    /* Return Window Handle */
  107.    return tcp_g.hWSA_IO_comp_window;
  108. }
  109.  
and output from various runs.....


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.
Jul 24 '07 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
One thing I see is the incorrect use of tchar.h mappings. RegisterClassEx is a macro that resolves to RegisterClassA or RegisterClassW depending upon the character set used on the build. Ditto for CreateWindowEx().

I assume you are compiling in ASCII because this code won't work in Unicode.

I'm not sure it works now. For example, the class name required by CreateWindow() is an LPCTSTR and you are providing a PCXAChar instead (whatever that is).

I would have thought you would:

LPCTSTR sClassName = TEXT("WndProc");


I recommend you adhere strictly to the tchar.h mappings and then see of the problem persists.
Jul 24 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.