Topic: Object Windows, Borland Classlib problem


Author: xiao@tii.com (Xiao Gong)
Date: Sat, 18 Sep 1993 15:33:23 GMT
Raw View
   I have a problem with string class used in OWL enviroment, I hope
friends on the net can help with this.

   The program is very simple. It pops up a simple input dialog box
on a menu choice, and the user enters a line of text, then the line
is saved in a borland list class as a string.

   I have defined a paint member function, in that I simply go through
the list and paint the text out.

   However, this code creates a protection fault on the strcpy line
in the paint method.

   I have done basically the same thing in the data entry function,
and it works fine.   Is there something I am missing?



#include <stdio.h>
#include <owl.h>
#include <edit.h>

#include <dialog.h>
#include <listbox.h>
#include <dialog.h>
#include <combobox.h>
#include <inputdia.h>
#include <contain.h>
#include <stack.h>
#include <strng.h>
#include "CHECKEID.h"
#include "CHECKER.h"
#include "CHECKCls.h"

// Define application class derived from  TApplication
class TCHECKER : public TApplication
{
public:
  TCHECKER(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
     LPSTR lpCmdLine, int nCmdShow)
    : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
    virtual void InitMainWindow();
};

_CLASSDEF(TMainWindow)
class TMainWindow : public TWindow
{
   HBITMAP boardsq1bitmap,boardsq2bitmap, piecebitmap;
   POINT mpos;
   bdmarker board[9][9];
   int firsttime;
   List namestack;

public:
   TMainWindow(PTWindowsObject AParent, LPSTR ATitle);
   ~TMainWindow();
   virtual void CHEAT(RTMessage Msg)      = [CM_FIRST + IDM_CHEAT  ];
   virtual void SWEAR(RTMessage Msg)      = [CM_FIRST + IDM_SWEAR  ];
   virtual void SURREND(RTMessage Msg)    = [CM_FIRST + IDM_SURREND];
   virtual void DATA_ENTRY(RTMessage Msg) = [CM_FIRST + IDM_ENTRY];
   virtual void WMPaint(RTMessage Msg) = [WM_FIRST + WM_PAINT];
   virtual void WMLButtonDown(RTMessage Msg) = [WM_FIRST + WM_LBUTTONDOWN];

};


/****************************************************
 * TMainWindow implementations:
 ****************************************************/

// Define TMainWindow, a TWindow constructor
TMainWindow::TMainWindow(PTWindowsObject AParent, LPSTR ATitle)
    : TWindow(AParent, ATitle)
{
   AssignMenu("CHECKER");
   boardsq1bitmap = LoadBitmap(GetApplication()->hInstance,"BITMAP_6");
   boardsq2bitmap = LoadBitmap(GetApplication()->hInstance,"BITMAP_7");
   piecebitmap = LoadBitmap(GetApplication()->hInstance,"BITMAP_1");
   firsttime=0;


}

// Define TMainWindow destructor
TMainWindow::~TMainWindow()
{

    DeleteObject(boardsq1bitmap);
    DeleteObject(boardsq1bitmap);
    DeleteObject(piecebitmap);

}

void TMainWindow::WMPaint(RTMessage)
{
   PAINTSTRUCT ps;
   HDC hDC;
   HDC MemDC1,MemDC2, MemDC3;
   BITMAP BitInfo;

   int x,y;
   char outline[100];
   memset(&ps,0x00,sizeof(PAINTSTRUCT));
   hDC = BeginPaint(HWindow,&ps);

   MemDC1 = CreateCompatibleDC(hDC);
   MemDC2 = CreateCompatibleDC(hDC);
   MemDC3 = CreateCompatibleDC(hDC);
   SelectObject(MemDC1, boardsq1bitmap);
   SelectObject(MemDC2, boardsq2bitmap);
   SelectObject(MemDC3, piecebitmap);

      for ( x =0; x<9; x=x+2)
      { for (y = 0; y<9; y=y+2)
 BitBlt(hDC,50+40*x,25+40*y,40,40,MemDC1,0,0,SRCCOPY);
      }
      for ( x =1; x<9; x=x+2)
      { for (y = 1; y<9; y=y+2)
 BitBlt(hDC,50+40*x,25+40*y,40,40,MemDC2,0,0,SRCCOPY);
      }
   // print only the part that is uncovered or is being convered

   BitBlt(hDC,50,25,360,360,MemDC1,0,0,SRCCOPY);
   BitBlt(hDC,mpos.x,mpos.y,32,32,MemDC2,0,0,SRCCOPY);
   ContainerIterator& c = namestack.initIterator();
   do {

/* this strcpy generate exception 13 */
    strcpy(outline, (const char *) (String&) (c.current()));
    TextOut(hDC,mpos.x,mpos.y,  outline, strlen(outline));
    c++;
    mpos.y = mpos.y + 20;
   }  while (c !=0);

   EndPaint(HWindow, &ps);
   DeleteDC(hDC);
   DeleteDC(MemDC1);
   DeleteDC(MemDC2);
   DeleteDC(MemDC3);
 }


void TMainWindow::CHEAT(RTMessage)
{
   //REGEN_CHEAT_EXEC

   MessageBox(HWindow,"It's unfair to cheat a computer","", MB_OK);

   //REGEN_CHEAT_EXEC
}

void TMainWindow::SWEAR(RTMessage)
{
   //REGEN_SWEAR_EXEC
     MessageBox(HWindow,"Go ahead and make my day!","", MB_OK);

   //REGEN_SWEAR_EXEC
}

void TMainWindow::SURREND(RTMessage)
{
   //REGEN_ITEM3_EXEC
 MessageBox(HWindow,"What a loser!","", MB_OK);

   //REGEN_ITEM3_EXEC
}

void TMainWindow::DATA_ENTRY(RTMessage)
{
   char edittext[80];
   char outline[100];
   strcpy(edittext,"");
   if ( GetApplication()->ExecDialog(new TInputDialog(this, "Enter Name",
    "Input a name", edittext, sizeof edittext )) == IDOK )
  {
   namestack.add(*(new String(edittext)));
  }
     ContainerIterator& c = namestack.initIterator();
   do {
    strcpy(outline, (const char *) (String&) (c.current()));
    MessageBox(NULL,outline,outline,MB_OK);
    c++;
   }  while (c !=0);


}

void TMainWindow::WMLButtonDown(RTMessage Msg)
{

   int tempx, tempy;
   mpos.x = Msg.LP.Lo;    /* horizontal position of cursor */
   mpos.y = Msg.LP.Hi;    /* vertical position of cursor   */
//testing which square the piece falls in
 /*
   if (mpos.x > 50)
   tempx =  (mpos.x - 50) / 40;
   if (mpos.y > 25)
   tempy =  (mpos.y - 25) / 40;
   if ((mpos.x > 0) && (mpos.y > 0))
   (board[tempx][tempy]).occupier = 1;
  */
   InvalidateRect(HWindow,NULL,TRUE);
   // MessageBox(HWindow,"Left Button is down","", MB_OK);
   //REGEN_ITEM3_EXEC
}

/***************************************************
 * TCHECKERApp method implementations:
 ***************************************************/

// Construct the TCHECKER's MainWindow of type TMainWindow
void TCHECKER::InitMainWindow()
{
   MainWindow = new TMainWindow(NULL, Name);
}

// Main program
int PASCAL WinMain(HINSTANCE hInstance,
     HINSTANCE hPrevInstance,
     LPSTR lpCmdLine,
     int nCmdShow)
{
   HINSTANCE hBorLibrary;

   TCHECKER CHECKER ("TRIDENT SOFTWARE INC", hInstance, hPrevInstance,
      lpCmdLine, nCmdShow);

   CHECKER.Run();

   return CHECKER.Status;
}





Author: xiao@tii.com (Xiao Gong)
Date: Tue, 21 Sep 1993 13:28:19 GMT
Raw View
     This is a reply to myself because I figured out the problem already.
Don't waste your time on it.
     THanks.