Click to See Complete Forum and Search --> : why this code gives me an error....?


jasie24
January 4th, 2003, 04:30 PM
Why this simple program gives me a protection fault error?(it does what i want,but in the same time it appears that error..)
it is written in borlandc 3.1 for windows but ,it's strange that another almost same program with this ,but it has something in adition,so it's larger ,and that works and this not.why?

here is the code....

#include <windows.h>

long FAR PASCAL WndProc(HWND hWnd,WORD message,WORD wParam,LONG lParam);

char name[]="Text";

int PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)
{
HWND hwnd;
MSG msg;

if(!hPrevInstance)
{
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=(WNDPROC)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=name;

if(!RegisterClass(&wndclass)) return FALSE;
}

hwnd=CreateWindow(name,"Afisare cu TextOut",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
if(!hwnd) return FALSE;

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)
{
HDC hdc;
PAINTSTRUCT ps;
static TEXTMETRIC tm;
static short cxChar,cyChar;
char szBuffer[10];

switch(message)
{
case WM_CREATE:
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
cxChar=tm.tmAveCharWidth;
cyChar=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
return 0;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,cxChar,cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmHeight= %d",tm.tmHeight));
TextOut(hdc,cxChar,2*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmAscent= %d",tm.tmAscent));
TextOut(hdc,cxChar,3*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmDescent= %d",tm.tmDescent));
TextOut(hdc,cxChar,4*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmInternalLeading= %d",tm.tmInternalLeading));
TextOut(hdc,cxChar,5*cyChar,szBuffer,
wsprintf(szBuffer,"tm.tmExternalLeading= %d",tm.tmExternalLeading));
EndPaint(hwnd,&ps);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}

mdmd
January 4th, 2003, 06:30 PM
Hullo Jasie

char szBuffer[10];
wsprintf(szBuffer,"tm.tmExternalLeading= %d",tm.tmExternalLeading));

Biggest problem ? You need to count the chars a little higher;

You have a buffer of 10 and you are copying far more chars into it.

Also, watch the first call to textout, the buffer is uninitialized there and can cause probs in release build.
do something like
char szBuffer[100] = {0}; // or zeromemory, memset
Or stick a string into it before the first call to textout.

S'all I can see, I don't have a 16bit complier installed so I cant build it and duplicate any other probs.

jasie24
January 5th, 2003, 03:57 PM
yeah,i think that could b the problem. it slept away from my eyes:)
thks

Bengi
January 5th, 2003, 05:05 PM
havnig a crash is not nice, even if it is a n human error!
the best way for us to 'avoid' them is to use SEH.
in c++:
try{
throw xx
}
catch(...){
}

TheCPUWizard
January 5th, 2003, 05:40 PM
Bengi :(

Actually that is NOT an SEH, it is a C++ exception which is a totally different animal (although SEH can be mapped into the generation of C++ Exceptions...

Additionally the bug in this case would generate NO exception, merely a corruption in 99.5% of all cases.