Thread: Windows API runtime bug please help

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    62

    Question Windows API runtime bug please help

    Hey all. I'm still a learning programmer, and I made a small "Hello World!" with Windows API. the only problem I've had is that each time I run it an empty cmd prompt pops up. @_@ this is really confusing as the code is quite simple and isnt supposed to do that. I'm using GCC as a compiler, and code::blocks IDE.
    heres the source code for those that wonder.






    Code:
    #include <windows.h>
    #define IDBUTTON 102 //This is a standart defenition for a GUI control ID
    //Used to link the button to handle the event of a click
    #define IDQUITBUTTON 103 //quit button
    
    //declaring windows peocedure
    
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    //HWND = GUI elements. represents the actual window/window element of a program
    //UINT = unsigned int
    //WPARAM/LPARAM = parameter messages that window uses to add data to a message
    
    //class name will be a global variable
    
    char szClassName [ ] = "windowWithButton";  //class name for the windows operating system
    HINSTANCE g_hInst;                          //Program instance. defines what program a GUI belongs to. used throughout the program
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
                        //WinMain is recognised by the system, like main()
    {
        HWND hwnd;          //window handle
        MSG messages;       //messages from windows (all inputs) stored here
        WNDCLASSEX wincl;   //data structure for the window class
    
        /*Window Structure*/
        g_hInst = hThisInstance;                //Set the program instance to this instance
        wincl.hInstance = hThisInstance;        //Set the window class instance to this instance
        wincl.lpszClassName = szClassName;      //Set the according class name
        wincl.lpfnWndProc = WindowProcedure;    //function called by windows
        wincl.style = CS_DBLCLKS;               //Catch double-clicks
        wincl.cbSize = sizeof (WNDCLASSEX);     //the size of a parameter of WNDCLASSEX will be its own size
    
        /*We will use the default icon and mouse cursor*/
        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
        //Using windows default color as the background for the window
        wincl.hbrBackground = (HBRUSH) (COLOR_BTNFACE+1);
    
        //Register the window class. on fail quit
        if(!RegisterClassEx (&wincl))
            return 0;
    
        //now that the class is registered, we can create the program
    
        hwnd = CreateWindowEx (
        0,                      //extended possibilities for variation - TEST
        szClassName,            //ClassName
        "GUI v.0.2",            //Title Text
        WS_OVERLAPPEDWINDOW,    //default window
        CW_USEDEFAULT,          //windows will decide the position
        CW_USEDEFAULT,          //where the window ends up on the screen
        240,                    //width
        140,                     //heigth
        HWND_DESKTOP,           //child-window to desktop
        NULL,                   //no menu
        hThisInstance,          //program instance handler
        NULL                    //no window creation data
        );
    
        //Make the window visivle
        ShowWindow (hwnd, SW_SHOW);
        UpdateWindow (hwnd);
    
        //Message loop time!
    
        while (GetMessage (&messages, NULL, 0, 0))
        {
            //Translate virtual key messages into character messages
            TranslateMessage(&messages);
            //send it to WindowProcedure
            DispatchMessage(&messages);
        }
    
        //program return value is 0. PostQuitMessage
        return messages.wParam;
    }
    
    //The Window Procedure is the way of sending/recieving windows messages declared as WM_
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HWND hwndButton; //make a HWND for the button
        HWND hwndQButton; //HWND for the Quit Button
        switch(message) //handle all windws messages/inputs
        {
            case WM_COMMAND: //if a command has happened
            {
                if(((HWND)lParam) && (HIWORD(wParam) == BN_CLICKED)) //if it was a click
                {
                    int iMID;
                    iMID = LOWORD(wParam);
                    switch(iMID)
                    {
                        case IDBUTTON: //if it was the button
                        {
                            MessageBox(hwnd, (LPCTSTR)"Hello World!", (LPCTSTR) "My Program!", MB_OK|MB_ICONEXCLAMATION);
                            break; //send a message
                        }
                        case IDQUITBUTTON: //if it was the quit button
                        {
                            PostQuitMessage (0);
                            break;
                        }
                        default:
                            break;
                    }
                }
                break;
            }
            case WM_DESTROY: //if it was a close off sign
            {
                PostQuitMessage (0); //send WM_QUIT to message queue, shut down
                break;
            }
            case WM_CREATE: //here we make the buttons
            {
                hwndButton = CreateWindowEx(0,                                      //more / extended
                                            TEXT("BUTTON"),                         //create what?
                                            TEXT("Push Me"),                        //Caption
                                            WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,   //control styles seperated by |
                                            10,                                     //LEFT POSITION (from left)
                                            10,                                     //TOP POSITION (from top)
                                            200,                                    //WIDTH OF CONTROL
                                            30,                                     //HEIGHT OF CONTROL
                                            hwnd,                                   //parent windows handle
                                            (HMENU)IDBUTTON,                        //control id for WM_COMMAND
                                            g_hInst,                                //application instance
                                            NULL);
                hwndQButton = CreateWindowEx(0,
                                             TEXT("BUTTON"),
                                             TEXT("Quit"),
                                             WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
                                             10,
                                             50,    //10 space between window top and button1, then 30 of button1, then 10 between the 2 buttons
                                             200,
                                             30,
                                             hwnd,
                                             (HMENU)IDQUITBUTTON,
                                             g_hInst,
                                             NULL);
                break;
            }
            default:    //messages we will not process
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Code:
    MessageBox(hwnd, TEXT("Hello World!"), TEXT("My Program!"), MB_OK|MB_ICONEXCLAMATION);
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Shingetsu Kurai View Post
    Hey all. I'm still a learning programmer, and I made a small "Hello World!" with Windows API. the only problem I've had is that each time I run it an empty cmd prompt pops up. @_@ this is really confusing as the code is quite simple and isnt supposed to do that. I'm using GCC as a compiler, and code::blocks IDE.
    heres the source code for those that wonder.
    In the GCC linker flags (settable from within Code:Blocks) make sure that you are linking for "Subsystem:Windows" not "Subsystem:Console"... when you link for console the cmd window is created automatically... but not for windows"

    In future when creating projects be sure to pick GUI mode for Windows projects...

    Also, it looks like you would benfit from a visit to TheForger's tutorial...

    http://www.winprog.org/tutorial/

    ... lots and lots of really great information there....
    Last edited by CommonTater; 07-01-2011 at 12:27 PM.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    Quote Originally Posted by CommonTater View Post
    In the GCC linker flags (settable from within Code:Blocks) make sure that you are linking for "Subsystem:Windows" not "Subsystem:Console"
    I'll make sure to do that. Any specification onto where it would be set? I've looked in the compiler&linker settings, but only an option to add libraries was there...

    Quote Originally Posted by CommonTater View Post
    In future when creating projects be sure to pick GUI mode for Windows projects...
    If you mean make the project of the Win32 GUI project type, that's what i have done. If you mean something else I don't know about it.

    Quote Originally Posted by CommonTater View Post
    Also, it looks like you would benfit from a visit to TheForger's tutorial...

    theForger's Win32 API Tutorial

    ... lots and lots of really great information there....
    I'll make sure to look at it, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime problem
    By SantoshN in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 02:42 PM
  2. Error in runtime
    By sick in forum C Programming
    Replies: 7
    Last Post: 08-15-2009, 02:51 AM
  3. Runtime formation and execution at runtime
    By Soham in forum C Programming
    Replies: 17
    Last Post: 08-27-2008, 08:45 AM
  4. runtime error
    By chatesmick in forum C Programming
    Replies: 4
    Last Post: 11-22-2005, 08:48 AM
  5. runtime error, windows XP, borland C++ 5.5
    By finnepower in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2005, 12:29 PM

Tags for this Thread