Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Opening a Console Window from a DLL

1 view
Skip to first unread message

Michael Hesse

unread,
Feb 15, 2005, 2:36:04 PM2/15/05
to
Hi,

I have created a DLL in VC++ 6.0, which is used by several executables.

I would like to open a console window and be able to write information to
that window when the DLL is being used. Is there a simple way to open a
console window from within a DLL?

Thanks.


William DePalo [MVP VC++]

unread,
Feb 15, 2005, 2:55:45 PM2/15/05
to
"Michael Hesse" <mhe...@sbcglobal.net> wrote in message
news:EasQd.304$Pz7...@newssvr13.news.prodigy.com...

The short answer to your question:

Call AllocConsole() and use the handle in subsequent calls to
WriteConsole(). That assumes that the processes in question do not already
have consoles.

Longer answer:

Are these executables windowed or console applications or a mixture of both?

1) If they are all console applications and if they all use the same flavor
of DLL-based runtime, then there should not be anything special that you
have to do. I/O to the console should be possible from any executable
component.

2) If they are all windowed applications and if you want to be able to do
I/O from the applications as well as the DLL then you will have the problem
of how to communicate the handle of the console to them.

3) If they are all windowed applications and if you want to do I/O only from
the DLL, then this should work for you:

#include <afx.h>
#include <io.h>
#include <stdio.h>
#include <fstream.h>

filebuf *pfb = 0;
ostream_withassign console = 0;

ostream_withassign getConsole()
{
int fd;
FILE *fp;
HANDLE hCon;

if ( console == 0 )
{
// Allocate a console

AllocConsole();

// Make printf happy

hCon = GetStdHandle(STD_OUTPUT_HANDLE);

fd = _open_osfhandle(reinterpret_cast<long>(hCon), 0);
fp = _fdopen(fd, "w");

*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);

// Make cout happy

pfb = new filebuf(fd);
console = pfb;
}

return console;
}

void closeConsole(ostream_withassign ostr)
{
delete pfb;
console = 0;
FreeConsole();
}

It allocates a console, tries to "point" the C language standard output
device at the newly allocated console as well as to create an C++ output
stream similarly targeted.

4) If some of them are windowed and some console applications - good luck.
:-)

Regards,
Will


Victor Bazarov

unread,
Feb 15, 2005, 3:00:06 PM2/15/05
to

You mean, create a console, right? Read about AllocConsole and related
functions. There is a simple way to associate stdout and stderr with it
too, the examples are on Google.

Michael Hesse

unread,
Feb 15, 2005, 3:39:47 PM2/15/05
to
This was a VERY helpful answer.

Michael

"William DePalo [MVP VC++]" <willd....@mvps.org> wrote in message
news:%23VF1Xh5...@TK2MSFTNGP15.phx.gbl...

Michael Hesse

unread,
Feb 15, 2005, 3:39:22 PM2/15/05
to
Thanks. I'll give it a shot.

Michael

"Victor Bazarov" <v.Aba...@comAcast.net> wrote in message
news:u0ARzj5E...@TK2MSFTNGP14.phx.gbl...

0 new messages