From: jayward@rigel.cs.pdx.edu (Jay Ward)
Newsgroups: comp.os.ms-windows.programmer.misc
Subject: WndProc comes a crashing down! (Borland C)
Summary: Please Help
Keywords: windows borland c++ help!!
Date: 28 Aug 92 00:26:01 GMT
Article-I.D.: pdxgate.6036

I am a new-to-Windows programmer and I seem to be living in interesting
times!  No matter what I try my WndProc crashes after processing
the WM_CREATE message.  My WM_CREATE section calls another function
and all seems to go well (according to Turbo Debugger/W), but when
the WndProc tries to return I get an 'Exception 13' (from TD/W) and
an Application Error (when run from within the BCW IDE).  I've tried
compiling with both the small and medium models with no success.

Please help me before I go bald from rooting my hair!!!

Source follows (use default BCW .DEF file):

-------------8<---------------cut here -------->8--------------------


/*--------------------------------------------
	CPB2.H
--------------------------------------------*/

#define	ALERT(str)	MessageBox(hwnd, str, "DEBUG", MB_ICONEXCLAMATION|MB_OK)

/*--------------------------------------------
	END -- CPB2.H
--------------------------------------------*/


/*-----------------------------------------------------
	 CPB2.C
-----------------------------------------------------*/

#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <dir.h>
#include "cpb2.h"

extern	char	*envpath, *oldpath;
void FAR PASCAL init(HWND);
void cleanup(HWND);
long FAR PASCAL WndProc(HWND, WORD, WORD, LONG);

int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
                    LPSTR lpszCmdLine, int nCmdShow)
     {
     static char    szAppName[] = "CPB2";
     HWND           hwnd;
     MSG            msg;
     WNDCLASS       wndclass;

     if (!hPrevInstance)
          {
          wndclass.style           = CS_HREDRAW | CS_VREDRAW;
          wndclass.lpfnWndProc     = WndProc;
          wndclass.cbClsExtra      = 0;
          wndclass.cbWndExtra      = 0;
          wndclass.hInstance       = hInstance;
          wndclass.hIcon           = LoadIcon (NULL, IDI_APPLICATION);
          wndclass.hCursor         = LoadCursor (NULL, IDC_ARROW);
          wndclass.hbrBackground   = GetStockObject(WHITE_BRUSH);
          wndclass.lpszMenuName    = NULL;
          wndclass.lpszClassName   = szAppName;
     
          RegisterClass (&wndclass) ;
          }
     
     hwnd = CreateWindow (szAppName, "CPBackup .DIR info",
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT, CW_USEDEFAULT,
                         CW_USEDEFAULT, CW_USEDEFAULT,
                         NULL, NULL, hInstance, NULL);
     
     ShowWindow (hwnd, nCmdShow);
     UpdateWindow (hwnd);
     
     while (GetMessage (&msg, NULL, 0, 0))
          {
          TranslateMessage (&msg);
          DispatchMessage (&msg);
          }
     return msg.wParam;
     }

long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
{
	char			szBuffer[10], szCurr_dir[MAXDRIVE], szPct_dir[MAXDRIVE];
	HDC				hdc;
	PAINTSTRUCT		ps;

	switch (message)
		{
		case WM_CREATE :
//			hdc = GetDC(hwnd);
			init(hwnd);
			ALERT("Done with init()");
			sprintf(szCurr_dir, "The current directory is: %s", oldpath);
            sprintf(szPct_dir, "The PCTOOLS directory is: %s", envpath);
//			ReleaseDC (hwnd, hdc);
			ALERT("We outta here");
			return 0;

		case WM_PAINT :
			hdc = BeginPaint (hwnd, &ps);
			TextOut(hdc, 1,1, szCurr_dir, strlen(szCurr_dir));
            TextOut(hdc, 1,10 ,szPct_dir, strlen(szPct_dir));
			EndPaint(hwnd, &ps);
			return 0;

		case WM_DESTROY :
			PostQuitMessage(0);
            cleanup(hwnd);
			return 0;
		}

	return DefWindowProc (hwnd, message, wParam, lParam);
}

/*-------------------------------------------------
	END CPB2.C
-------------------------------------------------*/

/*-----------------------------------------
	INIT.C
-----------------------------------------*/

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <dir.h>
#include <direct.h>
#include "cpb2.h"

char	*envpath, envdrive[MAXDRIVE], *oldpath, olddrive[MAXDRIVE];

void FAR PASCAL init(HWND hwnd)
{
	// Remember, oldpath needs to be free()'d
	if((oldpath=getcwd(NULL, MAXPATH))==NULL){
		MessageBox(hwnd, "getcwd()", "ERROR!", MB_ICONSTOP|MB_OK);
        exit(-1);
	}
	fnsplit(oldpath, olddrive, NULL, NULL, NULL);

	envpath=getenv("PCTOOLS");
	fnsplit(envpath, envdrive, NULL, NULL, NULL);
	chdir(envpath);
	if(_chdrive(*envdrive-'A'+1)) {
		MessageBox(hwnd, "_chdrive()", "Error in function init():", MB_ICONHAND|MB_OK);
		exit(-1);
	}
}

void cleanup(HWND hwnd)
{
	chdir(oldpath);
	free(oldpath);
	if(_chdrive(*olddrive-'A'+1)) {
		MessageBox(hwnd, "_chdrive()", "Error in function init():", MB_ICONHAND|MB_OK);
		exit(-1);
	}

}

/*--------------------------------------------
	END -- INIT.C
--------------------------------------------*/


          ||  Jay Ward || jayward@rigel.cs.pdx.edu  || 
          ||=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-||
          ||        Portland State University       ||
          ||            Portland, Oregon            ||