Book example error?

This is a discussion on Book example error? within the Windows Programming forums, part of the Platform Specific Boards category; I am reading through the pages of "Programming Windows, fifth edition" by Charles Petzold. I use Dev_c++ complier. I created ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Book example error?

    I am reading through the pages of "Programming Windows, fifth edition" by Charles Petzold. I use Dev_c++ complier. I created a windows application and it created most of the basic windows stuff I wanted. Looking trhough the pages I saw an example program that creates a window and the text Hello Windows 98! is printed to the center of the created window. The code for it to do that is:
    Code:
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC          hdc;
        PAINTSTRUCT  ps;
        RECT         rect;
        
        switch (message)                  /* handle the messages */
        {
            case WM_PAINT:
                 hdc = BeginPaint (hwnd, &ps);
                 
                 GetClientRect (hwnd, &rect);
                 
                 DrawText (hdc, TEXT("Hello!!!!!"), -1, &rect,
                          DT_SINGLELINE | DT_CENTER | DT_VCENTER);
                 
                 EndPaint (hwnd, &ps);
                 return 0;
                 
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    That is not exactly letter for letter I added a few things that Dev didn't have included...mainly the WM_PAINT stuff. But anyways that is what I have and I am getting an error message.
    Code:
    Permission denied
    Id returned 1 exit status
    [Build error] [Project1.exe] Error 1
    Why isn't this working, what did I do wrong?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    If this is all the code you have.. then you are in trouble..

    You need to initialize and register a parent window.. set up a message loop.. and then your windows procedure (that you have posted above) then compile your code as a windows project and you will have success.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    1479
    Join Date
    Aug 2003
    Posts
    253
    Here is all the code:
    Code:
    #include <windows.h>
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC          hdc;
        PAINTSTRUCT  ps;
        RECT         rect;
        
        switch (message)                  /* handle the messages */
        {
            case WM_PAINT:
                 hdc = BeginPaint (hwnd, &ps);
                 
                 GetClientRect (hwnd, &rect);
                 
                 DrawText (hdc, TEXT("Hello!!!!!"), -1, &rect,
                          DT_SINGLELINE | DT_CENTER | DT_VCENTER);
                 
                 EndPaint (hwnd, &ps);
                 return 0;
                 
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    I assumed I was only having problems with the WM_PAINT in the WinProc function, that is why it was the only part I posted. Most of the code is the code that the Dev-C++ compiler printed when I created a windows application.
    Knowledge is power and I want it all

    -0RealityFusion0-

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Compiled fine for me.. using dev-cpp

    compiler log:
    Code:
    Compiler: Default compiler
    Building Makefile: "F:\Dev-Cpp\Makefile.win"
    Executing  make...
    make.exe -f "F:\Dev-Cpp\Makefile.win" all
    g++.exe -c test.cpp -o test.o -I"F:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"F:/Dev-Cpp/include/c++/3.4.2/backward"  -I"F:/Dev-
    Attached Images Attached Images  
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    1479
    Join Date
    Aug 2003
    Posts
    253
    Ah, took me a while but I finally got it. I keep forgetting that when I do a windows application program and I save over something else it never compiles. I had to rename my project and the .cpp file for it to work correctly.
    Knowledge is power and I want it all

    -0RealityFusion0-

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    Permission denied
    Id returned 1 exit status
    [Build error] [Project1.exe] Error 1
    This happens when an old version of the program (Project1.exe) is running in the background. Because of this, Dev-C++ (or other compilers) is unable to write over the exe file. To fix this, just open up task manager (Ctrl+Alt+Del -> Task Manager), right click on Project1.exe and kill it.

    This is generally only a problem with Windows programs because they can run in the background with no window while console programs usually have a console.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21