We help IT Professionals succeed at work.

Create Window failure

calsoftware
calsoftware asked
on

static wchar_t g_szClassName[] = L"MyTestApp";
static HINSTANCE g_hInst = NULL;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  LPSTR lpCmdLine, int nCmdShow)
{
  WNDCLASSEX WndClass;
  HWND hwnd;
  MSG Msg;

  g_hInst = hInstance;

  WndClass.cbSize        = sizeof(WNDCLASSEX);
  WndClass.style         = NULL;
  WndClass.lpfnWndProc   = WndProc;
  WndClass.cbClsExtra    = 0;
  WndClass.cbWndExtra    = 0;
  WndClass.hInstance     = g_hInst;
  WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  WndClass.lpszMenuName  = NULL;
  WndClass.lpszClassName = g_szClassName;
  WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

  if(!RegisterClassEx(&WndClass))
  {
     MessageBox(0, L"Window Registration Failed!", "Error!",
        MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
     return 0;
  }

  hwnd = CreateWindowEx(
     WS_EX_CLIENTEDGE,
     g_szClassName,
     L"",
     WS_OVERLAPPEDWINDOW & ~WS_BORDER,
     CW_USEDEFAULT, CW_USEDEFAULT, 320, 240,
     NULL, NULL, GetModuleHandle(NULL), NULL);

  DWORD x = GetLastError();

  if(hwnd == NULL)
  {
     MessageBox(0, L"Window Creation Failed!", L"Error!",
        MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
     return 0;
  }

  ShowWindow(hwnd, SW_HIDE);
  UpdateWindow(hwnd);


  while(GetMessage(&Msg, NULL, 0, 0))
  {
     TranslateMessage(&Msg);
     DispatchMessage(&Msg);
  }
  return Msg.wParam;
}

In the above code snippet the hwnd is always NULL, Any ideas

Thanks all
Comment
Watch Question

Commented:
GetLastError says something?

Try this code:
hwnd = CreateWindowEx(0,
                g_szClassName, g_szClassName,
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
                NULL, NULL, hInstance, NULL);

Author

Commented:
Nope doesn't work.The GetLastError() code is 0.
Commented:
The attached program works. Take it, compile, check. Then modify per your needs.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

LPCWSTR s_szWndClassName = L"AnApplication";

ATOM RegisterWndClass(HINSTANCE hInstance, LPCWSTR lpszWndClassName);
LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
        LPWSTR lpCmdLine, int nCmdShow)
{
        UNREFERENCED_PARAMETER(hPrevInstance);
        UNREFERENCED_PARAMETER(lpCmdLine);
        HWND hWnd = NULL;
        MSG msg = { 0 };

        RegisterWndClass(hInstance, s_szWndClassName);

        hWnd = CreateWindow(s_szWndClassName, s_szWndClassName, 
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 
                NULL, NULL, hInstance, NULL);

        if (hWnd != NULL)
        {
                ShowWindow(hWnd, nCmdShow);
                UpdateWindow(hWnd);

                // Main message loop:
                while (GetMessage(&msg, NULL, 0, 0))
                {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                }
        }

        return (int)msg.wParam;
}

ATOM RegisterWndClass(HINSTANCE hInstance, LPCWSTR lpszWndClassName)
{
        WNDCLASSEX wcex         = { 0 };
        wcex.cbSize             = sizeof(WNDCLASSEX);
        wcex.style              = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc                = WndProc;
        wcex.hInstance          = hInstance;
        wcex.hCursor            = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground      = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName       = NULL;
        wcex.lpszClassName      = lpszWndClassName;
        return RegisterClassEx(&wcex);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, 
        WPARAM wParam, LPARAM lParam)
{
        PAINTSTRUCT ps;
        HDC hdc;

        switch (message)
        {
        case WM_PAINT:
                hdc = BeginPaint(hWnd, &ps);
                EndPaint(hWnd, &ps);
                break;

        case WM_DESTROY:
                PostQuitMessage(0);
                break;

        default:
                return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
}

Open in new window

Commented:
The code you posted does not contain WndProc.

Author

Commented:
Well i didn't post that, as it's trivial. But i do have a wndproc

Commented:
check the code I posted - it works always. It can be start point.

Author

Commented:
Thanks it works, the hwnd is not null any more, but when i change
ShowWindow(hWnd, nCmdShow); to ShowWindow(hWnd, SW_HIDE); the windproc function does nothing.. there are no events flowing.

Commented:
Because this function really does nothing. You need to add the handlers you need there.

Author

Commented:
OK. Thanks