0

I have a simple program that I am having trouble debugging.
It contains two different problems.

1. If I run a MessageBox in this program after the program is
up and running, the program crashes. I have to call the
Task Manager to shut down the program.

2. SetTimer will not work unless I put other code in the
funtion containing it. I've pinned down what happens using
a debugger but don't know how to cure it. I'm popping BPX
with a bad momory position after SetTimer completes.

The program is in working order now by fudging and the
"points of failure" are documented in the program.
I tried to eliminate compiler problems by compiling
using two different compilers. "cl" and Borland BCC.
Both act the same.

I run an AMD with SP1.

Attachments
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>

#define ID_FILE_EXIT         408
#define ID_FILE_LOAD	     409

//***** START Prototypes

int WINAPI WinMain (HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nCmdShow);
LRESULT CALLBACK MainWndProc(HWND hwndMainFrame, UINT uMsg , WPARAM wParam , LPARAM lParam);
HWND DoMenu(HWND hwndMainFrame);
HWND MyTimerFunc(HWND hwndMainFrame);
VOID CALLBACK TimerProc(HWND hwndMainFrame,UINT uMsg,UINT idEvent,DWORD dwTime);

//*******END Prototypes

   HWND hwndMainFrame;
   WPARAM wParam;
   LPARAM lParam;
   HINSTANCE hInstance;
   WNDCLASSEX wc;
   UINT uMsg;
   MSG msg;
   MSG message;
   PAINTSTRUCT paintStruct;
   HDC  hDC;

   HMENU hMyMenu;
   HWND Menubar;
   HMENU hFileMenu;
   MENUITEMINFO mii;

 TIMERPROC lpTimerFunc;
 UINT idEvent;
 DWORD dwTime;

  int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
{
    (void)UNREFERENCED_PARAMETER(hPrevInstance);
    (void)UNREFERENCED_PARAMETER(lpCmdLine);
    (void)UNREFERENCED_PARAMETER(nCmdShow);

	wc.cbSize=sizeof(WNDCLASSEX);
	wc.style=CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = MainWndProc;
	wc.cbClsExtra=0;
	wc.cbWndExtra=0;
	wc.hInstance=hInstance;
	wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wc.hCursor=LoadCursor(NULL,IDC_ARROW);
        wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName=NULL;
	wc.lpszClassName="MyClass";
	wc.hIconSm=LoadIcon(NULL,IDI_WINLOGO);

	if(!RegisterClassEx(&wc))
		return 0;
        
	hwndMainFrame = CreateWindowEx(NULL,"MyClass","MAINFRAME",WS_OVERLAPPEDWINDOW |
                        WS_VSCROLL | WS_VISIBLE | WS_CLIPCHILDREN,0,0,1024,740,
                        NULL,NULL,hInstance,NULL);

            if (!hwndMainFrame)
		return 0;
               
        ShowWindow(hwndMainFrame, nCmdShow);
	UpdateWindow(hwndMainFrame);
        
	while (GetMessage(&message,NULL,0,0)>0)

	{
	  TranslateMessage(&message);
	  DispatchMessage(&message);
	}
          return msg.wParam;
} 

   LRESULT CALLBACK MainWndProc(HWND hwndMainFrame, UINT uMsg, WPARAM wParam, LPARAM lParam)

{                               // ****************************** START MAIN WIND PROCEDURE

	 switch(uMsg)
	{                               // ********************** START SWITCH

	      case WM_CREATE:
                        DoMenu(hwndMainFrame); 
  		        break;
 
              case WM_PAINT:
  		        break;
 
		case WM_SIZE:
 		        break;

		 case WM_CLOSE: 
			PostQuitMessage(0);
			DestroyWindow(hwndMainFrame);
		        break;

		case WM_DESTROY:
                        KillTimer(hwndMainFrame,idEvent);
			PostQuitMessage(0);
			DestroyWindow(hwndMainFrame);
		        break;

	     case WM_COMMAND:
		switch(wParam)
       	            {                 // ***************** START MAIN WM_COMMAND

                case ID_FILE_LOAD:

                    MyTimerFunc(hwndMainFrame);
                    break;

	        case ID_FILE_EXIT:

                    SendMessage(hwndMainFrame,WM_DESTROY,0,0);
		    break;

                    }  // ********************************* END MAIN WM_COMMAND
	       	 break;

		           default:

	             return DefWindowProc(hwndMainFrame,uMsg, wParam, lParam);

         }                               // ********************** END SWITCH
        	return 0; 
}                                         // **************************** END MAIN WIND PROCEDURE

                  HWND DoMenu(HWND hwndMainFrame)

             {                                // ****************** START MAIN MENU
                HMENU hMyMenu = CreateMenu();
                IsMenu(hMyMenu);
               if(hMyMenu == NULL)
                   MessageBox(hwndMainFrame,"No Menu","MBOX3",MB_OK);

                   AppendMenu(hMyMenu,MF_STRING,ID_FILE_LOAD,"LOAD");
                   AppendMenu(hMyMenu,MF_STRING,ID_FILE_EXIT,"EXIT");
                   SetMenu(hwndMainFrame,hMyMenu);
                   DrawMenuBar(hwndMainFrame);
                   UpdateWindow(hwndMainFrame);

                return 0;
             }                                // ****************** END MAIN MENU

                   HWND MyTimerFunc(HWND hwndMainFrame)

              {          // *************************************** START TIMER

            SetTimer(hwndMainFrame,idEvent,1000,(TIMERPROC)TimerProc);

// ----- Remove below and the function fails --------------------------- TEST INFO

            hDC = GetDC(hwndMainFrame);
            BeginPaint(hwndMainFrame, &paintStruct);
            TextOut(hDC,1,20,"MyTimerFunc has been called.",28);
            EndPaint(hwndMainFrame, &paintStruct);
            ReleaseDC(hwndMainFrame, hDC);

// ----- Keep above and fuction works ---------------------------------- TEST INFO

            return 0;
              }          // *************************************** END TIMER

        VOID CALLBACK TimerProc(HWND hwndMainFrame,UINT uMsg,UINT idEvent,DWORD dwTime)

          {          // *************************************** START CALLBACK

            hDC = GetDC(hwndMainFrame);
            BeginPaint(hwndMainFrame, &paintStruct);
            TextOut(hDC,1,90,"This indicates that TimerProc timed out.",40);
            EndPaint(hwndMainFrame, &paintStruct);
            ReleaseDC(hwndMainFrame, hDC);

// ----- Allowing below to run causes crash --------------------------- TEST INFO

//  MessageBox(hwndMainFrame,"Worked","MBOXTEST",MB_OK); 

// ----- Allowing above to run causes crash --------------------------- TEST INFO

          }          // *************************************** END CALLBACK 

// ********************** END OF PROGRAM ***********************************
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by toolmanx
0

Sorry I got onto the wrong thread. Please delete this from this thread. I have copied it all over to the recommended thread.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.