Thread: How do yuo mix c++ and win32 ?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    91

    How do yuo mix c++ and win32 ?

    How do you mix c++ and win32, take this code for example, can you use standard c++ functions in this like cout, cin, cin.get etc or how does it work ?



    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)
    {
        switch (message)                  /* handle the messages */
        {
            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;
    }

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    of course you can mix Win32 and standard C++. What do you think Win32 API is? The Win32 API is just a set of C++ functions and variables that let you program Windows programs in C++, so why couldn't you use standard C++ to do that?

    Now, there is a catch about cout and cin. cout and and cin are abbreviations for shorter terms, if you did not know that already.

    cout = "console output"
    cin = "console input"

    They are meant to be directed towards a console (DOS) window. You do not normally have console windows in Win32 programs, so normally you don't typically use these in a Win32 program, however, that doesn't mean you cant, because you can. But they are not particularly helpful in Win32...so I wouldnt if I were you.

    However, you CAN redirect cout and cin to output to files if you want using fstreams (#include <fstream>). Just say:

    ofstream load ( "myfile.dat" );
    cout = load;

    That will redirect cout and make it output everything you say using cout to the file myfile.dat.

    Of course why do that when you can just use the ofstream in the first place and not redirect anything? It is all a matter of preference.
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What platform does your application run on? Is it a win32 console applications?

    Kuphryn

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    This is one of the things that makes Windows programming difficult to learn. There are lots of WinAPI functions (maybe 2000?) , and you have to learn when to use them. (Sometimes you must use them, other times they are optional.) There are also lots of constants, typedefs, and structures in the WinAPI.

    The other thing that makes learning Windows difficult is the amount of "overhead"... Lots of code for a little program that doesn't do much.

    You use the WinAPI stuff for the user interface and OS interface (I/O). For the most part, the WinAPI is used to make your program look good and easy to use.

    You use standard C++ functions & features for the underlying logic... to make your program do something useful and work well "under the hood"... stuff like calculations, data manipulation, or sorting.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I've written a small streambuffer class that outputs to message boxes. You can rebind the stream buffer of cout to it.

    Unfortunatly it's not in an uploadable state because I'm adding input support to it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    91
    Originally posted by CornedBee
    I've written a small streambuffer class that outputs to message boxes. You can rebind the stream buffer of cout to it.

    Unfortunatly it's not in an uploadable state because I'm adding input support to it.
    Sounds good,

    tks for all the other replies, very helpful.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    ofstream load ( "myfile.dat" );
    cout = load;
    HIDEOUSLY WRONG !!!!!

    You need to read this immediately....redirections....

    God im whacked tonight
    Last edited by Stoned_Coder; 01-13-2004 at 03:36 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Nice article. I didn't know you have to restore the buffers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed