Generating a console for iostream cin and cout from a win32 dll.  Can it be done?  How?

Posted on 2005-05-10
Last Modified: 2013-12-03
I am looking to construct a debug .dll that would drop in place of a third-party .dll; I have full headers.  I would like to have my debug API .dll use cin/cout to a console session so that a test engineer could interact with the main system is if the test engineer were the third party API.  Thus each .dll call would go to the debug API .dll and that .dll would open a console and use cout to specify which API call was just made then wait for the engineer to input data for that API (received via cin) so the debug API .dll would feed the engineer's input to the caller prgram just as the API would.  (Well "just like" if you ignor timing.  I don't expect an engineer to enter response to API calls as fas as the computer.

I guess, technically, I would not need a console, I just need a widow of text area that can interact with cin/cout,  there is a LOT of code already wirtten with cin/cout.

Question by:DaleStrickler
    LVL 86

    Expert Comment

    You can use

    #include <iostream>
    using namespace std;

    int WINAPI WinMain(HINSTANCE hInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR     lpCmdLine,
                        int       nCmdShow)

       int hCrt;
       FILE *hf;

       hCrt = _open_osfhandle(
                 (long) GetStdHandle(STD_OUTPUT_HANDLE),

        cout.attach ( hCrt);

        cout << "Some debug output" << endl;

        return 0;

    See also;en-us;105305 ("INFO: Calling CRT Output Routines from a GUI Application")
    LVL 55

    Assisted Solution

    by:Jaime Olivares

    Author Comment

    I need to play with this but I have a couple of immeadiate concerns:
    1. This is all happening in a WinMain not in a DllMain which is where I need it to work!  And where I am having the problems.
    2. I can't find ANY Visual Studio 7.1 documentation on the AllocConsole(), so where does this come from?  What exactly does it do.
    3. So far I have not found a set of preprecossor defines or compile switches that allow me to reference #include <iostream> in a dll project without compile errors.
    LVL 86

    Expert Comment

    >>This is all happening in a WinMain not in a DllMain which is where I need it to work!

    This was just a sample - actually, you can move that code anywhere in your program.

    >>I can't find ANY Visual Studio 7.1 documentation on the AllocConsole(), so where does this come from?

    See ("AllocConsole function [Base]") and ("Creation of a Console [Base]")

    >>reference #include <iostream> in a dll project without compile errors.

    What errors are you getting? Wthis works just fine in any DLL project.
    LVL 86

    Accepted Solution

    Oh, BTW, I see that you also need 'cin'. You could

    void OpenConsole () {

      int hIn, hOut;


      hOut = _open_osfhandle(
                (long) GetStdHandle(STD_OUTPUT_HANDLE),
      hIn = _open_osfhandle(
                (long) GetStdHandle(STD_INPUT_HANDLE),

       cout.attach ( hOut);
       cin.attach ( hOut);

    void CloseConsole() {


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now