CreateWindow returns NULL

Thema 21 Reputation points
2021-04-20T12:43:35.187+00:00

The Windows function CreateWindow fails and returns NULL.
Here is the code:

 WNDCLASS *tmpwindow;
 HWND windowhandle;

 tmpwindow = (WNDCLASS*)calloc(1, sizeof(WNDCLASS));

 tmpwindow->style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
 tmpwindow->lpfnWndProc = winProc;
 tmpwindow->hInstance = hinstance;
 tmpwindow->lpszMenuName = NULL;
 tmpwindow->hbrBackground = COLOR_WINDOW;
 tmpwindow->lpszClassName = "WINCLASS1";

 ret = RegisterClassA(tmpwindow);
 if(ret == 0){
  MessageBox(NULL, "RegisterClass failed", \
         "DrawSection results", MB_OK);
 }

 windowhandle = CreateWindowA("WINCLASS1", \
                     "DrawSection Test1", \
                     WS_VISIBLE | WS_OVERLAPPEDWINDOW, \
                     CW_USEDEFAULT, CW_USEDEFAULT, \
                     CW_USEDEFAULT, CW_USEDEFAULT, \
                     NULL, \
                     NULL, \
                     hinstance, \
                     NULL);
 if(windowhandle == NULL){
  MessageBox(NULL, "CreateWindow failed", \
         "DrawSection results", MB_OK);
  return 0;
 }
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,426 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,537 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Thema 21 Reputation points
    2021-04-20T13:22:40.77+00:00

    I'm trying to avoid C++ code and only use C.
    The window procedure does not contain any code.

    0 comments No comments

  2. Viorel 112.5K Reputation points
    2021-04-20T13:33:50.347+00:00

    Make sure that the Windows Procedure (winProc) has the correct signature and at least returns the value of DefWindowProc according to documentation and samples.