Link to home
Start Free TrialLog in
Avatar of 10goto20
10goto20

asked on

Problem with CreateWindowEx

Hi!

I'm developing a function for a Windows Mobile 5 device, which should display a window and draw some stuff in it. The function is in a DLL which is called repeatedly during the execution of the program. The first, second and third time the function runs, everything works as it should, but from the fourth time and on, no window is displayed.

Calling GetLastError after CreateWindowEx, says the error ERROR_ACCESS_DENIED occured. The message loop aborts immediatelly, and no WM_PAINT event occurs.

Can anyone see what may be causing this?

Thanks!


#include <stdafx.h>

#include <windows.h>
#include <commctrl.h>

static LPCWSTR g_szClassName = _T("Name");

static LRESULT CALLBACK WndProc (HWND hwnd , UINT msg,WPARAM wParam , LPARAM lParam);

static BOOL bsRunning = FALSE;
static UINT nTimerEvent = NULL;

BOOL doCmd(PTCHAR* cmdArgs, DWORD countArgs, PTCHAR* cmdReply, HINSTANCE hInstance)
{
      iAngle = _wtoi(cmdArgs[0]);
      iSpacing = _wtoi(cmdArgs[1]);

      if(!bsRunning)
      {
            bsRunning = TRUE;
            
            WNDCLASS wc;

            wc.style  = 0;
            wc.lpfnWndProc = WndProc;
            wc.cbClsExtra  = 0;
            wc.cbWndExtra  = 0;

            wc.hInstance   = hInstance;
            wc.hIcon       = LoadIcon(NULL,NULL);
            wc.hCursor     = LoadCursor(NULL,IDC_ARROW);

            wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
            wc.lpszMenuName  = NULL;
            wc.lpszClassName = g_szClassName;

            if(!RegisterClass(&wc))
            {
                  *cmdReply = _T("Failed to create window");
                  return FALSE;
            }

            HWND hwnd;
            hwnd = CreateWindowEx(WS_EX_DLGMODALFRAME, g_szClassName, g_szClassName, WS_DLGFRAME | WS_VISIBLE, 0, 0, 240, 320, NULL, NULL, hInstance, NULL);

            if(hwnd == NULL)
             {
                     *cmdReply = _T("Failed to create window");
                     return FALSE;
             }

            nTimerEvent = SetTimer(hwnd,1000,2000,NULL);
            
            MSG Msg;

            while(GetMessage(&Msg,hwnd,0,0))
            {
                                   TranslateMessage(&Msg);
                  DispatchMessage (&Msg);
            }

            UnregisterClass(g_szClassName, hInstance);

            *cmdReply = _T("OK\0");
            return TRUE;
      }
      return FALSE;
}

static void DrawTest(HWND hwnd)
{
      PAINTSTRUCT paintStruct;
      HDC hDC;
      hDC = BeginPaint(hwnd,&paintStruct);

// Drawing here...
      
      EndPaint(hwnd, &paintStruct);
}

LRESULT CALLBACK WndProc (HWND hwnd , UINT msg,WPARAM wParam , LPARAM lParam)
{
      DWORD dwState;

      switch(msg)
      {      
            case WM_CLOSE:
                  DestroyWindow  (hwnd);
                  break;
            case WM_PAINT:
                  DrawTest(hwnd);
                  break;
            case WM_DESTROY:
                  PostQuitMessage(0);
                  break;
            case WM_TIMER:
                  KillTimer(hwnd,nTimerEvent);
                  DestroyWindow(hwnd);
                  break;
            default:
                  return DefWindowProc(hwnd,msg,wParam,lParam);
        }
        return 0;
}

ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial