Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: why is my programs general protection faulting  (Read 3532 times)

0 Members and 1 Guest are viewing this topic.

smeezekitty

  • Guest
why is my programs general protection faulting
« on: August 25, 2009, 11:27:03 AM »
recently my programs are encountering GPFs
i dont know how to solve it
here is one that is GPF ing
Code: [Select]
#include <windows.h>
#include <stdio.h>
#define intx2(x) (int)((int)x * 2)
#define doub(v) ((intx2((v)) > 255) ? ((wchar_t)intx2((v))): 255)
#define TOGRAY(R, G, B) ((wchar_t)((R+G+B) / 3))
#define NORMALIZE(X) (((X) > 255) ? 255: (((X) < 0) ? 0: (X)))
#define hdc H
#define bool char
float xc=1.0,yc=1.0;
DWORD bw=160,bh=100;
int width=160,height=99;
char filename[25];
HWND TEXT;
bool testfile(char *name){
     FILE *x =fopen(name, "r");       
     if(fgetc(x)!='B'){fclose(x); return (FALSE);}
     if(x == NULL){return FALSE;}
     fclose(x);
     return (TRUE);
     }

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);


char szClassName[ ] = "mini bitmap viewer";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               
    MSG messages;           
    WNDCLASS wincl;       
                               
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;     
    wincl.style = CS_DBLCLKS;             
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;               
    wincl.cbClsExtra = 0;                     
    wincl.cbWndExtra = 0;                     
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

  if (!RegisterClass (&wincl))
        return 0;

   
    hwnd = CreateWindowEx (
           0,               
           szClassName,       
           "Windows App",     
           WS_OVERLAPPEDWINDOW,
           CW_USEDEFAULT,     
           CW_USEDEFAULT,     
           170,             
           140,                 
           HWND_DESKTOP,       
           NULL,             
           hThisInstance,       
           NULL             
           );

   
    ShowWindow (hwnd, nFunsterStil);

 
    while (GetMessage (&messages, NULL, 0, 0))
    {
     
        TranslateMessage(&messages);           
        DispatchMessage(&messages);
    }

   
    return messages.wParam;
}




LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                 
    {
        case WM_CREATE:
             TEXT=
             CreateWindow("edit", NULL,
             WS_CHILD |WS_VISIBLE |WS_BORDER,
             0, 0, 150, 20,
             hwnd, (NULL)/*ID_EDIT*/,
             ((LPCREATESTRUCT)lParam)->hInstance, NULL);
             break;
             case WM_SETFOCUS:
                  SetFocus(TEXT);
                  break;
             case WM_COMMAND:
              if(/*HIWORD(wParam) == EN_UPDATE*/1)
     {
            GetWindowText(TEXT, filename, 32);
             if(!testfile(filename) && filename[0] != '\0'){break;}
             FILE *BITMAP=
             fopen(testfile(filename)? filename: "D:\\_3_1.bmp", "rb");
             fseek(BITMAP, 0x12, 0);
             fread(&bw, 4, 1, BITMAP);
             fread(&bh, 4, 1, BITMAP);
             fclose(BITMAP);
             *(float *)&xc = (float)((float)width/(float)bw);
             *(float *)&yc = (float)((float)(height-20)/(float)bh);
            InvalidateRect(hwnd, NULL, TRUE);
            }
        break;   
        case WM_SIZE:
             {
             RECT x = {0, 0, LOWORD(lParam), HIWORD(lParam)};
             width = LOWORD(lParam);
             height = HIWORD(lParam);
             *(float *)&xc = (float)((float)LOWORD(lParam)/(float)bw);
             *(float *)&yc = (float)((float)(HIWORD(lParam)-20)/(float)bh);
             InvalidateRect(hwnd, &x, 1);
        break;
        }
         case WM_PAINT:{
             PAINTSTRUCT p;
             HBRUSH brush;
             HDC H=
             BeginPaint(hwnd, &p);
             FILE *BITMAP=
             fopen(testfile(filename) ? filename: "D:\\_3_1.bmp", "rb");
             fseek(BITMAP, 0x12, 0);
             fread(&bw, 4, 1, BITMAP);
             fread(&bh, 4, 1, BITMAP);
             fseek(BITMAP, 54, 0);
             wchar_t R,G,B,GRAY; 
             char dump[6];
            // int NR,NG,NB;
             int x,y;
             for(y = 0;y < bh;y++){
              for(x = 0;x < bw;x++){
             fread(&B, 1, 1, BITMAP);
             fread(&G, 1, 1, BITMAP);
             fread(&R, 1, 1, BITMAP);
             GRAY = TOGRAY(R,G,B);
             if(bw%2==0 ||y%2==0){
             brush = CreateSolidBrush(PALETTERGB(R,G,(B/16)*16));
             }else
             brush = CreateSolidBrush(PALETTERGB(GRAY,GRAY,GRAY));
             RECT R = {x*xc, ((bh-y)*yc)+20, (x+1)*xc, (((bh-y)+1)*yc)+20};
             FillRect(hdc, &R, brush);
             DeleteObject(brush);
             }   
             if((bw % 4 )!= 0){
             fread(&dump, bw % 4, 1, BITMAP);
             } 
             }
             fclose(BITMAP);
             /*RECT r = {0, 0, 160, 100};
             InvalidateRect(hwnd, &r, 0);*/
             EndPaint(hwnd, &p);
             break;
             }                                         
             
              case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                   
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: why is my programs general protection faulting
« Reply #1 on: August 25, 2009, 12:12:22 PM »
where does it GPF?

I'll see if I can load it into C++ 2008. 
I was trying to dereference Null Pointers before it was cool.

smeezekitty

  • Guest
Re: why is my programs general protection faulting
« Reply #2 on: August 25, 2009, 12:17:42 PM »
at seemingly random times
i looked and there doesnt seem to be any memory leaks or overflows
i just dont get it
sometimes it works ok sometimes it doesnt
p.s. its a processor hog

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: why is my programs general protection faulting
« Reply #3 on: August 25, 2009, 12:23:57 PM »
hmmm


well you have



Code: [Select]
char filename[25];

and then later...

Code: [Select]
GetWindowText(TEXT, filename, 32);


so your telling GetWindowText() That it has 32-bytes to work with in the buffer, but it actually only has 25...
I was trying to dereference Null Pointers before it was cool.

smeezekitty

  • Guest
Re: why is my programs general protection faulting
« Reply #4 on: August 25, 2009, 02:00:58 PM »
oops
but anyway
i only enter 15 chars of text

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: why is my programs general protection faulting
« Reply #5 on: August 25, 2009, 02:06:39 PM »
oops
but anyway
i only enter 15 chars of text



ahh! but- as you've said, the program is CPU intensive, and this might cause Windows to tack on "(Not Responding)" making it much longer.

try resizing the buffer, see if that helps any.

I was trying to dereference Null Pointers before it was cool.

smeezekitty

  • Guest
Re: why is my programs general protection faulting
« Reply #6 on: August 25, 2009, 02:08:30 PM »
after resizeing the buffer to 35 bytes
it worked fine but as i closed the window
guess what......general protection fault