Thread: Windows apps verses Console apps

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Windows apps verses Console apps

    With console apps I can use cin and cout, but then in windows apps (using bloodshed dev c++ 's windows template) If I try and use cin or cout I get nothing. I assume this has something to do with redirecting the standard input and output streams. So I have a few questions about this,

    1) For the windows app where is the standard input and output stream redirected as default?

    2) How can I redirect the standard inout and output streams to a console if one does not exist in a windows app?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    cin and cout go to the console. A windows application doesn't HAVE a console by default.

    You could create a console, but if you want the output to appear IN YOUR window, that's not what you want to do - you'll essentially have to do it "some other way".

    If you want to use cin/cout for debug purposes, then you can use AllocConsole:
    http://msdn2.microsoft.com/en-us/library/ms681944.aspx

    AllocConsole will do everything you need for cin/cout to work - just make sure you call AllocConsole first and enjoy.

    --
    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.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    Well I inserted a call to AllocConsole and it made a console window show up, but I cannot write to it or get input from it. It the documentation it says it initializes standard input and standard output. I am a little lost as to why this is not working then.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's strange - perhaps the cin/cout aren't set up correctly (or failed to initiailize to their respective file handles as they weren't valid at that point, perhaps).

    I'll have a look to see if I can figure out what needs to be done.

    --
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seems like it's a bit more to it: I think this page describes what you need to do
    http://www.codeproject.com/cpp/EditBin.asp

    --
    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.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You can use AllocConsole(), you just have to do a little work to "hook up" your CRT's implementation to that console. I believe mingw in DevC++ still uses the Microsoft CRT - in which case here's some information on "hook up" code for the MS CRT:

    http://www.halcyon.com/~ast/dload/guicon.htm

    But it's also easy to just change your subsystem at compile time (or by using editbin).

    gg

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Codeplug, That's roughly what the link I posted does, just not all the
    Code:
    std{in,out,err} FILE's
    .

    --
    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.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The point of the editbin article is that you can't change the subsystem of an MFC application. If you're not using MFC, there's nothing stopping you from writing windows GUI code within a console application (using DevC++'s console app template for instance). The downside is that you'll always have a console window that pops up when you start your application.

    If you would like to create a console window at runtime, then you'll need to "hook up" the allocated console as described. The advantage here is that you can create the console when/if you want it.

    More information on creating a console at runtime:
    http://www.codeproject.com/useritems/ConsoleAdapter.asp

    gg

  9. #9
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    AllocConsole is just the start.

    try taking a look at the left menu when you're looking at the AllocConsole entry at msdn.

    you can use ReadConsole and WriteConsole to read and write with a console. hell, you can even use ReadFile and WriteFile to read and write with a console. you can use GetStdHandle and SetStdHandle to redirect I/O to handles of your choice (pipes, files, even sockets, you're dealing with void *).

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One process with two console windows
    By siavoshkc in forum Windows Programming
    Replies: 8
    Last Post: 01-30-2009, 04:13 PM
  2. Console C++ to full Windows
    By foxon177 in forum Windows Programming
    Replies: 5
    Last Post: 05-12-2008, 09:37 PM
  3. windows fonts for console
    By glUser3f in forum Game Programming
    Replies: 6
    Last Post: 10-21-2003, 04:50 AM
  4. windows dos console
    By dune911 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-16-2002, 11:30 PM
  5. Turning a Console APP into Windows.
    By Darkflame in forum C++ Programming
    Replies: 3
    Last Post: 09-14-2001, 03:10 AM