Thread: Your program has encountered a problem and needs to close error

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    4
    Here is all my code that my project contains. I am trying to create a toolbar that lets me use my own images as buttons. I copied this directly off another webiste. I am using Dev-C++ as my compiler and I get no compiler errors when I run this, just the message saying "Your program has encountered an error and needs to close. I am also including the following library in the parameters section of project options in the linker box like this -lcomctl32

    My project contains 3 files. 1 C++ source file, 1 Header file, 1 Resource File
    __________________________________________________ ______________
    header file, resource.h:

    Code:
            #define IDC_TOOLBAR 1000
            #define IDB_TOOLBAR 1001
            #define IDM_BUTTON0 1002
            #define IDM_BUTTON1 1003
            #define IDM_BUTTON2 1004
            #define IDM_BUTTON3 1005
            #define IDM_BUTTON4 1006
            #define IDM_BUTTON5 1007
            #define IDM_BUTTON6 1008
            #define IDM_BUTTON7 1009
    --------------------------------------------

    resource file, ToolBarCustom.rc:

    Code:
            #include "resource.h"
            IDB_TOOLBAR BITMAP "standard.bmp" 
            //I have a bitmap file in the same directory that is 96x16 and contains 6 images
    --------------------------------------------

    c++ source file, main.cpp:

    Code:
    #include <windows.h>
    #include <commctrl.h>
    #include "resource.h"
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    static char gszClassName[] = "MyWindowClass";
    static HINSTANCE ghInstance = NULL;
    HWND ghToolBar;
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    
        WNDCLASSEX WndClass;
        HWND hwnd;
        MSG Msg;
    
        ghInstance = hInstance;
    
        WndClass.cbSize = sizeof(WNDCLASSEX);
        WndClass.style = NULL;
        WndClass.lpfnWndProc = WndProc;
        WndClass.cbClsExtra = 0;
        WndClass.cbWndExtra = 0;
        WndClass.hInstance = ghInstance;
        WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        WndClass.lpszMenuName = NULL;
        WndClass.lpszClassName = gszClassName;
        WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&WndClass)) {
             MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONSTOP | MB_OK);
             return 0;
        }
    
        hwnd = CreateWindowEx(
             WS_EX_STATICEDGE,
             gszClassName,
             "Windows Title",
             WS_OVERLAPPEDWINDOW,
             CW_USEDEFAULT, CW_USEDEFAULT,
             320, 240,
             NULL, NULL,
             ghInstance,
             NULL);
    
        if(hwnd == NULL) {
             MessageBox(0, "Window Creation Failed!", "Error!", MB_ICONSTOP | MB_OK);
             return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while(GetMessage(&Msg, NULL, 0, 0)) {
             TranslateMessage(&Msg);
             DispatchMessage(&Msg);
        }
    
        return Msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
            switch(Message) {
                            case WM_CREATE:
                                 TBADDBITMAP tbAddBitmap;
                                 TBBUTTON tbButton[8];
    
                                 InitCommonControls();
    
                                 ghToolBar = CreateWindowEx(
                                 NULL,
                                 TOOLBARCLASSNAME,
                                 NULL,
                                 WS_CHILD | WS_VISIBLE,
                                 0, 0,
                                 0, 0,
                                 hwnd, (HMENU)IDC_TOOLBAR,
                                 ghInstance,
                                 NULL);
    
                                 SendMessage(ghToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), NULL);
    
                                 tbAddBitmap.hInst = ghInstance;
                                 tbAddBitmap.nID = IDB_TOOLBAR;
                                 
                                 SendMessage(ghToolBar, TB_ADDBITMAP, 0, (LPARAM) &tbAddBitmap);
    
                                 ZeroMemory(tbButton, sizeof(tbButton));
    
                                 tbButton[0].iBitmap = 0;
                                 tbButton[0].fsState = TBSTATE_ENABLED;
                                 tbButton[0].fsStyle = TBSTYLE_BUTTON;
                                 tbButton[0].idCommand = IDM_BUTTON0;
    
                                 tbButton[1].iBitmap = 1;
                                 tbButton[1].fsState = TBSTATE_ENABLED;
                                 tbButton[1].fsStyle = TBSTYLE_BUTTON;
                                 tbButton[1].idCommand = IDM_BUTTON1;
    
                                 tbButton[2].fsStyle = TBSTYLE_SEP;
    
                                 tbButton[3].iBitmap = 2;
                                 tbButton[3].fsState = TBSTATE_ENABLED;
                                 tbButton[3].fsStyle = TBSTYLE_BUTTON;
                                 tbButton[3].idCommand = IDM_BUTTON2;
    
                                 tbButton[4].iBitmap = 3;
                                 tbButton[4].fsState = TBSTATE_ENABLED;
                                 tbButton[4].fsStyle = TBSTYLE_BUTTON;
                                 tbButton[4].idCommand = IDM_BUTTON3;
    
                                 tbButton[5].fsStyle = TBSTYLE_SEP;
    
                                 tbButton[6].iBitmap = 4;
                                 tbButton[6].fsState = TBSTATE_ENABLED;
                                 tbButton[6].fsStyle = TBSTYLE_BUTTON;
                                 tbButton[6].idCommand = IDM_BUTTON4;
    
                                 tbButton[7].iBitmap = 5;
                                 tbButton[7].fsState = TBSTATE_ENABLED;
                                 tbButton[7].fsStyle = TBSTYLE_BUTTON;
                                 tbButton[7].idCommand = IDM_BUTTON5;
    
                                 SendMessage(ghToolBar, TB_ADDBUTTONS, 8, (LPARAM) &tbButton);
                            break;//WM_CREATE BREAK
    
                            case WM_COMMAND:
                                 switch(LOWORD(wParam)) {
                                                        case IDM_BUTTON0:
                                                        case IDM_BUTTON1:
                                                        case IDM_BUTTON2:
                                                        case IDM_BUTTON3:
                                                        case IDM_BUTTON4:
                                                        case IDM_BUTTON5:
                                                        MessageBox(hwnd, "A Button was clicked.", "Toolbar Message", MB_ICONINFORMATION | MB_OK);
                                                        break;
                                 }
                            break;//WM_COMMAND BREAK
    
                            case WM_SIZE:
                                 SendMessage(ghToolBar, TB_AUTOSIZE, 0, 0);
                            break;//WM_SIZE BREAK
    
                            case WM_CLOSE:
                                 DestroyWindow(hwnd);
                            break;//WM_CLOSE BREAK
    
                            case WM_DESTROY:
                                 PostQuitMessage(0);
                            break;//WM_DESTROY BREAK
            
            default://default for Message Switch statement
                    return DefWindowProc(hwnd, Message, wParam, lParam);
            }
    
    return 0;
    }
    -----------------------------------------------------------------------------
    If any one can give me any suggestions what so ever that would be very helpful. Thank you. If anyone would like my files attached please let me know.
    Last edited by taka209; 07-15-2008 at 01:47 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yeah, first thing is indent your code so it's readable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    4
    I hope thats better, sorry for the irreadability.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure that if you run this with a debugger, you should be able to see the error on the call-stack.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    4
    Okay, I ran the debugger and I get a "Warning" message that pops up saying "An Access Violation (Segmentation Fault) raised in your program."

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So now you view the call stack window (it's on one of the menus), then you click on one of the functions (written by you) which is in the stack trace.
    Then you start looking at variables to try to determine the cause of the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    MS debugger shoud take you right to the line where the error occured.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Sounds to me like it didn't create your window or your window handle is invalid. I could be wrong but something doesn't look right about extended style static edge with base style overlapped window.

    If you attempt to show a window and pass in an invalid handle you very well could get this type of error or the show function may just completely ignore your request to show the window.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    4
    I think you may be right, im just not sure how to make it work correctly I have made the following changes.

    Code:
      LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
       static char gszClassName[] = "MyWindowClass";
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    
    
       WNDCLASSEX WndClass;
       HWND hwnd;
       MSG Msg;
    
    
       WndClass.cbSize = sizeof(WNDCLASSEX);
       WndClass.style = 0;
       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 = (HBRUSH)(COLOR_WINDOW+1);
       WndClass.lpszMenuName = NULL;
       WndClass.lpszClassName = gszClassName;
       WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
       //.........
    
       hwnd = CreateWindowEx(
          WS_EX_STATICEDGE,
          gszClassName,
          "Windows Title",
          WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, CW_USEDEFAULT,
          320, 240,
          NULL, NULL,
          hInstance,
          NULL);
    
    //......
    
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
    
    //......
       TBADDBITMAP tbAddBitmap;
       TBBUTTON tbButton[8];
    
       InitCommonControls();
    
       HWND ghToolBar;
       ghToolBar = CreateWindowEx(0,TOOLBARCLASSNAME,NULL,WS_CHILD |           WS_VISIBLE,0,0,0,0,hwnd,(HMENU)IDC_TOOLBAR,GetModuleHandle(NULL),NULL);
    
       SendMessage(ghToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
    
       tbAddBitmap.hInst = hInstance;//replaced it with hInstance 
       tbAddBitmap.nID = IDB_TOOLBAR;
    //.....
    }
    I deleted the stuff with static HINSTANCE ghInstance = NULL and just used the hInstance declared in the WinMain function. When I try to use hInstance in tbAddBitmap.hInst. I says hInstance is undeclared. I'm sorry this is so confusing, thank you for all your suggestions.

Popular pages Recent additions subscribe to a feed

Tags for this Thread