Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to print message on console in mfc program?

How to print message on console in mfc program?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialc++javaquestion
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David YueZuo
    wrote on last edited by
    #1

    Hi, I have been thinking about the problem below for a week :((, Is there anyone can help me? I m working on a MFC program, and I want to make the program support console (Command Line). I don't know why I can't use cout or printf to print anything out. I thought It could display messages on console when I launch the program on console (windows xp), like: c:\app.exe -number 1 but nothing came out on consle actually. I know that I can create another console to recieve messages from cout and printf, but I don't want to create any console in my program, I just want print some message on original console. In Java, we can print message on console by using System.out.print(), so is there any way, function or class I can use to print messages on the console in MFC program? Could you show me a simple example? Thanks so much! David Zuo

    R 1 Reply Last reply
    0
    • D David YueZuo

      Hi, I have been thinking about the problem below for a week :((, Is there anyone can help me? I m working on a MFC program, and I want to make the program support console (Command Line). I don't know why I can't use cout or printf to print anything out. I thought It could display messages on console when I launch the program on console (windows xp), like: c:\app.exe -number 1 but nothing came out on consle actually. I know that I can create another console to recieve messages from cout and printf, but I don't want to create any console in my program, I just want print some message on original console. In Java, we can print message on console by using System.out.print(), so is there any way, function or class I can use to print messages on the console in MFC program? Could you show me a simple example? Thanks so much! David Zuo

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #2

      Everything is a bit mixed up in your post. - A console program does usually not involve any mfc. It is a program that usually does not have any GUI and in which you can use cout and cin. - An MFC application is an application using classes that makes it easy to handle the GUI. Both are seldom mixed together. - A command line is a set of parameter you can give to your executable, no matter if it is a console application or a MFC app, it works in both ways.

      c:\app.exe -number 1 // works if app is MFC or console

      David.YueZuo wrote:

      nothing came out on consle actually

      What did you expect here ? How are you handling the command line in your application ?

      David.YueZuo wrote:

      I can use to print messages on the console in MFC program

      As I explained, MFC and console are usually different types of app. If you want to display something in a MFC app, use a CEdit, a CStatic or a MessageBox. ~RaGE();

      D 1 Reply Last reply
      0
      • R Rage

        Everything is a bit mixed up in your post. - A console program does usually not involve any mfc. It is a program that usually does not have any GUI and in which you can use cout and cin. - An MFC application is an application using classes that makes it easy to handle the GUI. Both are seldom mixed together. - A command line is a set of parameter you can give to your executable, no matter if it is a console application or a MFC app, it works in both ways.

        c:\app.exe -number 1 // works if app is MFC or console

        David.YueZuo wrote:

        nothing came out on consle actually

        What did you expect here ? How are you handling the command line in your application ?

        David.YueZuo wrote:

        I can use to print messages on the console in MFC program

        As I explained, MFC and console are usually different types of app. If you want to display something in a MFC app, use a CEdit, a CStatic or a MessageBox. ~RaGE();

        D Offline
        D Offline
        David YueZuo
        wrote on last edited by
        #3

        Thanks so much for your infor. Here, c:\app.exe -number 1 app.exe is just a MFC program (GUI-Based), I added some printf statements in it, like printf("Hello, world"), I hope this "Hello, world" can be displayed on my console. Actually, I used CEdit, CStatic and MessageBox as well, but only if I want the messages to be displayed on a dialog, editor. In Java, we can have: { ...... JOptionPane.showMessageDialog("Hello World"); System.out.print("Hello World"); ...... } then, message "Hello world" will be displayed on two different places, one is on a dialog, another is on the console. I want to use the same way to deal with messages in MFC. Could you tell me how to do? Thanks David Zuo

        W R 2 Replies Last reply
        0
        • D David YueZuo

          Thanks so much for your infor. Here, c:\app.exe -number 1 app.exe is just a MFC program (GUI-Based), I added some printf statements in it, like printf("Hello, world"), I hope this "Hello, world" can be displayed on my console. Actually, I used CEdit, CStatic and MessageBox as well, but only if I want the messages to be displayed on a dialog, editor. In Java, we can have: { ...... JOptionPane.showMessageDialog("Hello World"); System.out.print("Hello World"); ...... } then, message "Hello world" will be displayed on two different places, one is on a dialog, another is on the console. I want to use the same way to deal with messages in MFC. Could you tell me how to do? Thanks David Zuo

          W Offline
          W Offline
          Wim Engberts
          wrote on last edited by
          #4

          A GUI program is usually not connected to a console. On your windows dektop, you may have 0 to many consoles, so which one would you like to send your messages to? Like you said yourself, you can allocate a console to your application using "AllocConsole ()" When you do that, then your printf statements will send their output to this console. William

          D 1 Reply Last reply
          0
          • W Wim Engberts

            A GUI program is usually not connected to a console. On your windows dektop, you may have 0 to many consoles, so which one would you like to send your messages to? Like you said yourself, you can allocate a console to your application using "AllocConsole ()" When you do that, then your printf statements will send their output to this console. William

            D Offline
            D Offline
            David YueZuo
            wrote on last edited by
            #5

            Now, I open a console by using "run" -> "cmd", type the directory of the file, then run my program on this console like: c:\cd temp c:\temp\app.exe -number 10 Again, app is a GUI-based MFC program, I added some printf statements in it, like printf("Hello world"); or std::cout << "hello world"; I hope the message "Hello world" could be displayed on my current console. Could you tell me how to do? Thanks! David Zuo

            W 1 Reply Last reply
            0
            • D David YueZuo

              Thanks so much for your infor. Here, c:\app.exe -number 1 app.exe is just a MFC program (GUI-Based), I added some printf statements in it, like printf("Hello, world"), I hope this "Hello, world" can be displayed on my console. Actually, I used CEdit, CStatic and MessageBox as well, but only if I want the messages to be displayed on a dialog, editor. In Java, we can have: { ...... JOptionPane.showMessageDialog("Hello World"); System.out.print("Hello World"); ...... } then, message "Hello world" will be displayed on two different places, one is on a dialog, another is on the console. I want to use the same way to deal with messages in MFC. Could you tell me how to do? Thanks David Zuo

              R Offline
              R Offline
              Rage
              wrote on last edited by
              #6

              David.YueZuo wrote:

              I want to use the same way to deal with messages in MFC

              This is difficult. As I already explained, MFC and console are two different kinds of application. I may even add that mixing both of them is extremely bad design, IMO :-O. What are you trying to achieve exactely ? Why do you need a console aside your application ? Do you really think the user of your app wants to have a console pop up, or is this only for debugging purposes ? This may be a start point: QuickWin[^] However, this is the opposite to what you need: make a console run as a plain Win32 application. ~RaGE();

              D 1 Reply Last reply
              0
              • R Rage

                David.YueZuo wrote:

                I want to use the same way to deal with messages in MFC

                This is difficult. As I already explained, MFC and console are two different kinds of application. I may even add that mixing both of them is extremely bad design, IMO :-O. What are you trying to achieve exactely ? Why do you need a console aside your application ? Do you really think the user of your app wants to have a console pop up, or is this only for debugging purposes ? This may be a start point: QuickWin[^] However, this is the opposite to what you need: make a console run as a plain Win32 application. ~RaGE();

                D Offline
                D Offline
                David YueZuo
                wrote on last edited by
                #7

                Actually, I was a Java programmer. In Java, we can use both ways to display messages, no matter on console or GUI. Now, I am a C++ programmer, I m trying to use the same way to create a GUI-based MFC program. To create a console aside GUI for a actual user may not a good idea, but for the designer and programmer, it's a esaier and faster way to debug and monitor our program. In fact, I don't want to create any console, instead, I want to make the program able to run on console, and to recieve arguments from console. Also, program's messages would be displayed on current console during the running time. David Zuo

                A B 2 Replies Last reply
                0
                • D David YueZuo

                  Actually, I was a Java programmer. In Java, we can use both ways to display messages, no matter on console or GUI. Now, I am a C++ programmer, I m trying to use the same way to create a GUI-based MFC program. To create a console aside GUI for a actual user may not a good idea, but for the designer and programmer, it's a esaier and faster way to debug and monitor our program. In fact, I don't want to create any console, instead, I want to make the program able to run on console, and to recieve arguments from console. Also, program's messages would be displayed on current console during the running time. David Zuo

                  A Offline
                  A Offline
                  Abebe
                  wrote on last edited by
                  #8

                  You can use debug statements in your MFC application, using 'OutputDebugString()' (if i remember correctly). The strings supplied as an argument will be displayed in the 'Output' section of Visual Studio (assuming that you use VS ofcourse :)). You can also use an external debugging application that is able to capture output generated by 'OutputDebugString()'. You can use 'windbg' for example to receive the debugging output. Er zit een korstje op mijn aars.

                  1 Reply Last reply
                  0
                  • D David YueZuo

                    Actually, I was a Java programmer. In Java, we can use both ways to display messages, no matter on console or GUI. Now, I am a C++ programmer, I m trying to use the same way to create a GUI-based MFC program. To create a console aside GUI for a actual user may not a good idea, but for the designer and programmer, it's a esaier and faster way to debug and monitor our program. In fact, I don't want to create any console, instead, I want to make the program able to run on console, and to recieve arguments from console. Also, program's messages would be displayed on current console during the running time. David Zuo

                    B Offline
                    B Offline
                    beko
                    wrote on last edited by
                    #9

                    you may use a RichEdit Control to display debug info etc. http://www.codeproject.com/richedit/RichEditLog\_Demo.asp In order to enable a kind of interaction through commands (and do not want to use button etc.) , you can embed (TCL, LUA, Python) in or develop a small scripting facility. Bekir.

                    1 Reply Last reply
                    0
                    • D David YueZuo

                      Now, I open a console by using "run" -> "cmd", type the directory of the file, then run my program on this console like: c:\cd temp c:\temp\app.exe -number 10 Again, app is a GUI-based MFC program, I added some printf statements in it, like printf("Hello world"); or std::cout << "hello world"; I hope the message "Hello world" could be displayed on my current console. Could you tell me how to do? Thanks! David Zuo

                      W Offline
                      W Offline
                      Wim Engberts
                      wrote on last edited by
                      #10

                      As said before, your app and the console are two separate programs. If you start your MFC application from a console, then this application will start independently of that console. I.e., the two will not be connected! The only way is to allocate a console window to your application with AllocConsole. For as far as I know, it is not possible to connect an existing console to you MFC application. William

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups