• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 753
  • Last Modified:

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

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.

0
DaleStrickler
Asked:
DaleStrickler
  • 3
2 Solutions
 
jkrCommented:
You can use

#include <iostream>
using namespace std;

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

   int hCrt;
   FILE *hf;

   AllocConsole();
   hCrt = _open_osfhandle(
             (long) GetStdHandle(STD_OUTPUT_HANDLE),
             _O_TEXT
          );

    cout.attach ( hCrt);

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

    return 0;
}

See also http://support.microsoft.com/default.aspx?scid=kb;en-us;105305 ("INFO: Calling CRT Output Routines from a GUI Application")
0
 
DaleStricklerAuthor Commented:
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.
0
 
jkrCommented:
>>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 http://msdn.microsoft.com/library/en-us/dllproc/base/allocconsole.asp ("AllocConsole function [Base]") and http://msdn.microsoft.com/library/en-us/dllproc/base/creation_of_a_console.asp ("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.
0
 
jkrCommented:
Oh, BTW, I see that you also need 'cin'. You could

void OpenConsole () {

  int hIn, hOut;

  AllocConsole();

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

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

void CloseConsole() {

    FreeConsole();
}
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now